/** * */ /** * @author Musard Balliu * * @date Sep 20, 2015 * */ public class Date { // State of a Date object int day; int month; // between 1 and 12 int year; // from 1900 on // global variable, not part of the state private static String[] monthInEnglish = { "january", "february", "march", "april", "may", "june", "july", "august", "september", "october", "november", "december" }; public Date(int y, int m, int d) { year = y; month = m; day = d; checkData(); } public Date(int m, int d) { this(2008, m, d); } public Date(int d) { this(1, d); } public Date() { this(1985, 1, 13); } private void checkData() { int[] daysOfMonth = getDaysOfMonth(); if (year < 1900 || month < 1 || month > 12 || day < 1 || day > daysOfMonth[month - 1]) System.out.println("Illegal date"); } private boolean isLeapYear(int year) { return year % 400 == 0 || (year % 4 == 0 && year % 100 != 0); } public String toString() { return day + " " + monthInEnglish[month - 1] + " " + year; } private int[] getDaysOfMonth() { int[] daysOfMonth = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; if (isLeapYear(year)) daysOfMonth[1] = 29; return daysOfMonth; } public boolean precede(Date otherDate) { return this.daysPassedSinceYearStart() < otherDate.daysPassedSinceYearStart(); } public int daysPassedSinceYearStart() { int result = day; int[] daysOfMonth = getDaysOfMonth(); for (int pos = 0; pos < month - 1; pos++) result += daysOfMonth[pos]; return result; } public int daysPassedSinceVeryStart() { int result = daysPassedSinceYearStart(); for (int pos = 1900; pos < year - 1; pos++) result += isLeapYear(pos) ? 366 : 365; return result; } }