diff --git a/src/org/hwo/datetime/Date.java b/src/org/hwo/datetime/Date.java index 82b08d6..e7527e4 100644 --- a/src/org/hwo/datetime/Date.java +++ b/src/org/hwo/datetime/Date.java @@ -19,6 +19,12 @@ public class Date { day = date.day; } + public Date(int year,int month,int day){ + setYear(year); + setMonth(month); + setDay(day); + } + public Date Clone(){ Date d = new Date(); d.year = year; @@ -27,6 +33,22 @@ public class Date { return d; } + public int compare(Date comp){ + if (this.year < comp.year) + return -1; + if (this.year > comp.year) + return 1; + if (this.month < comp.month) + return -1; + if (this.month > comp.month) + return 1; + if (this.day < comp.day) + return -1; + if (this.day > comp.day) + return 1; + return 0; + } + public int getYear() { return year; } @@ -96,10 +118,41 @@ public class Date { private static int[] daysInMonth = { 31,28,31,30,31,30,31,31,30,31,30,31 }; public int getDaysInMonth(){ - int d = daysInMonth[ month ]; - if (month == 2) + int d = daysInMonth[ month - 1]; + if (isLeapYear() && (month == 2)) d += 1; return d; } + + private static int[] Nmonth = {0,3,3,6,1,4,6,2,5,0,3,5}; + + private int getNday(int day){ + return day % 7; + } + private int getNmonth(int month){ + return Nmonth[month - 1]; + } + private int getNyear(int year){ + int ny = year % 100; + ny = (ny + (ny / 4)) % 7; + + int nh = year / 100; + nh = (3 - (nh % 4)) * 2; + + return ny + nh; + } + + public static String[] weekdays = {"Son","Mon","Die","Mit","Don","Fre","Sam"}; + public int getWeekDay(){ + int ns = getNday(day) + getNmonth(month) + getNyear(year); + + if ((month < 3) && isLeapYear()) + ns += 6; + + return ns % 7; + } + + + } diff --git a/src/org/hwo/datetime/DateTime.java b/src/org/hwo/datetime/DateTime.java index 31adb6e..8cdb6ee 100644 --- a/src/org/hwo/datetime/DateTime.java +++ b/src/org/hwo/datetime/DateTime.java @@ -25,6 +25,29 @@ public class DateTime { this.setDate(new Date()); setTimeStamp(timestamp); } + + public DateTime(DateTime source){ + this.date = source.getDate().Clone(); + this.time = new TimeOfDay(source.getTime()); + } + + public int compare(DateTime comp){ + int dc = this.date.compare(comp.getDate()); + if (dc != 0) + return dc; + return this.time.compare(comp.getTime()); + } + + public boolean isBefore(DateTime comp){ + return (compare(comp)<0); + } + public boolean isAfter(DateTime comp){ + return (compare(comp)>0); + } + public boolean isEqual(DateTime comp){ + return (compare(comp)==0); + } + public Date getDate() { return date; @@ -59,6 +82,9 @@ public class DateTime { this.time.setSeconds(timestamp.getSeconds()); } + + + @Override public String toString() { return getSQLDateTime(); diff --git a/src/org/hwo/datetime/TimeOfDay.java b/src/org/hwo/datetime/TimeOfDay.java index 4d0c4d0..26fa663 100644 --- a/src/org/hwo/datetime/TimeOfDay.java +++ b/src/org/hwo/datetime/TimeOfDay.java @@ -11,6 +11,14 @@ public class TimeOfDay { minutes, seconds; + public static TimeOfDay beforeMidnight(){ + TimeOfDay t = new TimeOfDay(); + t.setHours(23); + t.setMinutes(59); + t.setSeconds(59); + return t; + } + public TimeOfDay() { setHours(0); setMinutes(0); @@ -31,6 +39,14 @@ public class TimeOfDay { this.setSeconds(seconds); } + public int compare(TimeOfDay comp){ + if (getSecondsOfTheDay() < comp.getSecondsOfTheDay()) + return -1; + if (getSecondsOfTheDay() > comp.getSecondsOfTheDay()) + return 1; + return 0; + } + public int getHours() { return hours; }