Implement Calendar#getTime

Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
This commit is contained in:
Johannes Schindelin 2013-10-17 17:26:33 -05:00
parent b3d4f33522
commit dcfcd193be

View File

@ -55,6 +55,10 @@ public abstract class Calendar {
time = date.getTime();
}
public Date getTime() {
return new Date(time);
}
public abstract void roll(int field, boolean up);
public abstract void add(int field, int amount);
@ -102,6 +106,23 @@ public abstract class Calendar {
parseIntoFields(this.time);
}
public Date getTime() {
long days = fields[DAY_OF_MONTH] - 1;
long years = fields[YEAR] - EPOCH_LEAP_YEAR;
days += years * 365 + years / 4 + 1 - DAYS_TO_EPOCH;
for (int month = 0; month < fields[MONTH]; month++) {
days += DAYS_IN_MONTH[0][month];
}
if (fields[MONTH] < 2 && isLeapYear(fields[YEAR])) {
days--;
}
long time = MILLIS_PER_DAY * days
+ MILLIS_PER_HOUR * fields[HOUR_OF_DAY]
+ MILLIS_PER_MINUTE * fields[MINUTE]
+ MILLIS_PER_SECOND * fields[SECOND];
return new Date(time);
}
private static boolean isLeapYear(int year) {
return (year%4 == 0) && (year%100 != 0) || (year%400 == 0);
}