import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
public class DurationValue {
private int year;
private int month;
private int hour;
private int day;
private int min;
private int sec;
private String value;
public DurationValue(String value) throws Exception {
this.value = value;
fillValues();
}
private void fillValues() throws Exception {
StringBuffer sb = new StringBuffer(value);
int pointer = 0;
for (int i = 0; i < sb.length(); i++) {
if (sb.charAt(i) == 'Y') {
year = parseCharToInt(value.substring(pointer, i));
pointer = i + 1;
} else if (sb.charAt(i) == 'M') {
month = parseCharToInt(value.substring(pointer, i));
pointer = i + 1;
} else if (sb.charAt(i) == 'D') {
day = parseCharToInt(value.substring(pointer, i));
pointer = i + 1;
} else if (sb.charAt(i) == 'H') {
hour = parseCharToInt(value.substring(pointer, i));
pointer = i + 1;
} else if (sb.charAt(i) == 'M') {
min = parseCharToInt(value.substring(pointer, i));
pointer = i + 1;
} else if (sb.charAt(i) == 'S') {
sec = parseCharToInt(value.substring(pointer, i));
pointer = i + 1;
}
}
}
private int parseCharToInt(String substring) throws NumberFormatException {
int out = Integer.parseInt(substring);
return out;
}
private Date getTodayPlusDuration() {
Calendar cal = new GregorianCalendar();
// hier addiere ich nur mal die jahre, monate und tage
cal.add(Calendar.YEAR, year);
cal.add(Calendar.MONTH, month);
cal.add(Calendar.DAY_OF_YEAR, day);
return cal.getTime();
}
private Date getTodayMinusDuration() {
return null;
}
public int getHour() {
return hour;
}
public int getMonth() {
return month;
}
public int getYear() {
return year;
}
public int getDay() {
return day;
}
public int getSec() {
return sec;
}
public int getMin() {
return min;
}
public static void main(String[] args) {
try {
DurationValue dv = new DurationValue("1Y4M15D22H52S");
System.out.println(dv.getYear());
System.out.println(dv.getMonth());
System.out.println(dv.getDay());
System.out.println(dv.getHour());
System.out.println(dv.getMin());
System.out.println(dv.getSec());
System.out.println(dv.getTodayPlusDuration());
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}