import java.io.*; public class CalcDate { private int[] monthDays = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; private int year, month, date; private long pastDays; public CalcDate() { } public int getYear() { return year; } public int getMonth() { return month; } public int getDate() { return date; } public long getPastDays() { return pastDays; } public void setPastDays(long days) { pastDays = days; year = (int)(days / 365); long tmpDays = year * 365 + (year / 4) - (year / 100) + (year / 400); while (days <= tmpDays) { year--; tmpDays = year * 365 + (year / 4) - (year / 100) + (year / 400); } days -= tmpDays; year++; for (month = 0; days > 0; days -= monthDays[month++]) { if (month == 1 && isLeapYear(year)) { days--; } } date = (int)days + monthDays[month - 1]; if (month == 2 && isLeapYear(year)) { date++; } } public void set(int year, int month, int date) { this.year = year; this.month = month; this.date = date; pastDays = 0; pastDays += (year - 1) * 365 + (year - 1) / 4 - (year - 1) / 100 + (year - 1) /400; for (int i = 0; i < month - 1; i++) { pastDays += monthDays[i]; if (i == 1 && isLeapYear(year)) { pastDays++; } } pastDays += date; } private boolean isLeapYear(int year) { return (year % 4 == 0 && year % 100 != 0) || year % 400 == 0; } }