home *** CD-ROM | disk | FTP | other *** search
- package java.util;
-
- public class Date {
- private long value;
- private boolean valueValid;
- private boolean expanded;
- private short tm_millis;
- private byte tm_sec;
- private byte tm_min;
- private byte tm_hour;
- private byte tm_mday;
- private byte tm_mon;
- private byte tm_wday;
- private short tm_yday;
- private int tm_year;
- private int tm_isdst;
- private static short[] monthOffset = new short[]{0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334};
- private static final String[] wtb = new String[]{"am", "pm", "monday", "tuesday", "wednesday", "thursday", "friday", "saturday", "sunday", "january", "february", "march", "april", "may", "june", "july", "august", "september", "october", "november", "december", "gmt", "ut", "utc", "est", "edt", "cst", "cdt", "mst", "mdt", "pst", "pdt"};
- private static final int[] ttb = new int[]{0, 1, 0, 0, 0, 0, 0, 0, 0, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 10000, 10000, 10000, 10300, 10240, 10360, 10300, 10420, 10360, 10480, 10420};
-
- public Date() {
- this(System.currentTimeMillis());
- }
-
- public Date(long date) {
- this.value = date;
- this.valueValid = true;
- this.expanded = false;
- }
-
- public Date(int year, int month, int date) {
- this(year, month, date, 0, 0, 0);
- }
-
- public Date(int year, int month, int date, int hrs, int min) {
- this(year, month, date, hrs, min, 0);
- }
-
- public Date(int year, int month, int date, int hrs, int min, int sec) {
- this.expanded = true;
- this.valueValid = false;
- this.tm_millis = 0;
- this.tm_sec = (byte)sec;
- this.tm_min = (byte)min;
- this.tm_hour = (byte)hrs;
- this.tm_mday = (byte)date;
- this.tm_mon = (byte)month;
- this.tm_wday = 0;
- this.tm_yday = 0;
- this.tm_year = year;
- this.computeValue();
- this.expand();
- }
-
- public Date(String s) {
- this(parse(s));
- }
-
- public static long UTC(int year, int month, int date, int hrs, int min, int sec) {
- long day = (long)(date + monthOffset[month] + ((year & 3) == 0 && (year % 100 != 0 || (year + 300) % 400 == 0) && month >= 2 ? 0 : -1)) + (long)(year - 70) * 365L + (long)((year - 69) / 4) - (long)((year - 1) / 100) + (long)((year + 299) / 400);
- return (long)((sec + 60 * (min + 60 * hrs)) * 1000) + 86400000L * day;
- }
-
- public static long parse(String s) {
- int year = -1;
- int mon = -1;
- int mday = -1;
- int hour = -1;
- int min = -1;
- int sec = -1;
- int c = -1;
- int i = 0;
- int n = -1;
- int tzoffset = -1;
- int prevc = 0;
- if (s != null) {
- int limit = s.length();
-
- while(true) {
- if (i >= limit) {
- if (year >= 0 && mon >= 0 && mday >= 0) {
- if (sec < 0) {
- sec = 0;
- }
-
- if (min < 0) {
- min = 0;
- }
-
- if (hour < 0) {
- hour = 0;
- }
-
- if (tzoffset == -1) {
- return (new Date(year, mon, mday, hour, min, sec)).getTime();
- }
-
- return UTC(year, mon, mday, hour, min, sec) + (long)(tzoffset * '\uea60');
- }
- break;
- }
-
- c = s.charAt(i);
- ++i;
- if (c > 32 && c != 44 && c != 45) {
- if (c == 40) {
- int depth = 1;
-
- while(i < limit) {
- c = s.charAt(i);
- ++i;
- if (c == 40) {
- ++depth;
- } else if (c == 41) {
- --depth;
- if (depth <= 0) {
- break;
- }
- }
- }
- } else if (c >= 48 && c <= 57) {
- for(n = c - 48; i < limit && (c = s.charAt(i)) >= 48 && c <= 57; ++i) {
- n = n * 10 + c - 48;
- }
-
- if (prevc != 43 && (prevc != 45 || year < 0)) {
- if (n >= 70) {
- if (year >= 0 || c > 32 && c != 44 && c != 47 && i < limit) {
- break;
- }
-
- year = n < 1900 ? n : n - 1900;
- } else if (c == 58) {
- if (hour < 0) {
- hour = (byte)n;
- } else {
- if (min >= 0) {
- break;
- }
-
- min = (byte)n;
- }
- } else if (c == 47) {
- if (mon < 0) {
- mon = (byte)n - 1;
- } else {
- if (mday >= 0) {
- break;
- }
-
- mday = (byte)n;
- }
- } else {
- if (i < limit && c != 44 && c > 32 && c != 45) {
- break;
- }
-
- if (hour >= 0 && min < 0) {
- min = (byte)n;
- } else if (min >= 0 && sec < 0) {
- sec = (byte)n;
- } else {
- if (mday >= 0) {
- break;
- }
-
- mday = (byte)n;
- }
- }
- } else {
- if (n < 24) {
- n *= 60;
- } else {
- n = n % 100 + n / 100 * 60;
- }
-
- if (prevc == 43) {
- n = -n;
- }
-
- if (tzoffset != 0 && tzoffset != -1) {
- break;
- }
-
- tzoffset = n;
- }
-
- prevc = 0;
- } else if (c != 47 && c != 58 && c != 43 && c != 45) {
- int st;
- for(st = i - 1; i < limit; ++i) {
- c = s.charAt(i);
- if ((c < 65 || c > 90) && (c < 97 || c > 122)) {
- break;
- }
- }
-
- if (i <= st + 1) {
- break;
- }
-
- int k = wtb.length;
-
- while(true) {
- --k;
- if (k < 0) {
- break;
- }
-
- if (wtb[k].regionMatches(true, 0, s, st, i - st)) {
- int action = ttb[k];
- if (action != 0) {
- if (action == 1) {
- if (hour > 12 || hour < 0) {
- throw new IllegalArgumentException();
- }
-
- hour += 12;
- } else if (action <= 13) {
- if (mon >= 0) {
- throw new IllegalArgumentException();
- }
-
- mon = (byte)(action - 2);
- } else {
- tzoffset = action - 10000;
- }
- }
- break;
- }
- }
-
- if (k < 0) {
- break;
- }
-
- prevc = 0;
- } else {
- prevc = c;
- }
- }
- }
- }
-
- throw new IllegalArgumentException();
- }
-
- public int getYear() {
- if (!this.expanded) {
- this.expand();
- }
-
- return this.tm_year;
- }
-
- public void setYear(int year) {
- if (!this.expanded) {
- this.expand();
- }
-
- this.tm_year = year;
- this.valueValid = false;
- }
-
- public int getMonth() {
- if (!this.expanded) {
- this.expand();
- }
-
- return this.tm_mon;
- }
-
- public void setMonth(int month) {
- if (!this.expanded) {
- this.expand();
- }
-
- this.tm_mon = (byte)month;
- this.valueValid = false;
- }
-
- public int getDate() {
- if (!this.expanded) {
- this.expand();
- }
-
- return this.tm_mday;
- }
-
- public void setDate(int date) {
- if (!this.expanded) {
- this.expand();
- }
-
- this.tm_mday = (byte)date;
- this.valueValid = false;
- }
-
- public int getDay() {
- if (!this.expanded) {
- this.expand();
- } else if (this.tm_wday < 0 || !this.valueValid) {
- this.computeValue();
- this.expand();
- }
-
- return this.tm_wday;
- }
-
- public int getHours() {
- if (!this.expanded) {
- this.expand();
- }
-
- return this.tm_hour;
- }
-
- public void setHours(int hours) {
- if (!this.expanded) {
- this.expand();
- }
-
- this.tm_hour = (byte)hours;
- this.valueValid = false;
- }
-
- public int getMinutes() {
- if (!this.expanded) {
- this.expand();
- }
-
- return this.tm_min;
- }
-
- public void setMinutes(int minutes) {
- if (!this.expanded) {
- this.expand();
- }
-
- this.tm_min = (byte)minutes;
- this.valueValid = false;
- }
-
- public int getSeconds() {
- if (!this.expanded) {
- this.expand();
- }
-
- return this.tm_sec;
- }
-
- public void setSeconds(int seconds) {
- if (!this.expanded) {
- this.expand();
- }
-
- this.tm_sec = (byte)seconds;
- this.valueValid = false;
- }
-
- public long getTime() {
- if (!this.valueValid) {
- this.computeValue();
- }
-
- return this.value;
- }
-
- public void setTime(long time) {
- this.value = time;
- this.valueValid = true;
- this.expanded = false;
- }
-
- public boolean before(Date when) {
- return this.getTime() < when.getTime();
- }
-
- public boolean after(Date when) {
- return this.getTime() > when.getTime();
- }
-
- public boolean equals(Object obj) {
- return obj != null && obj instanceof Date && this.getTime() == ((Date)obj).getTime();
- }
-
- public int hashCode() {
- long ht = this.getTime();
- return (int)ht ^ (int)(ht >> 32);
- }
-
- public native String toString();
-
- public native String toLocaleString();
-
- public native String toGMTString();
-
- public int getTimezoneOffset() {
- if (!this.expanded) {
- this.expand();
- }
-
- return (int)((this.getTime() - UTC(this.tm_year, this.tm_mon, this.tm_mday, this.tm_hour, this.tm_min, this.tm_sec)) / 60000L);
- }
-
- private native void expand();
-
- private native void computeValue();
- }
-