1 7 8 package java.util; 9 10 import java.text.DateFormat ; 11 import java.io.IOException ; 12 import java.io.ObjectOutputStream ; 13 import java.io.ObjectInputStream ; 14 import java.lang.ref.SoftReference ; 15 import sun.util.calendar.BaseCalendar; 16 import sun.util.calendar.CalendarDate; 17 import sun.util.calendar.CalendarSystem; 18 import sun.util.calendar.CalendarUtils; 19 import sun.util.calendar.Era; 20 import sun.util.calendar.Gregorian; 21 import sun.util.calendar.ZoneInfo; 22 23 112 public class Date 113 implements java.io.Serializable , Cloneable , Comparable <Date > 114 { 115 private static final BaseCalendar gcal = 116 CalendarSystem.getGregorianCalendar(); 117 private static BaseCalendar jcal; 118 119 private transient long fastTime; 120 121 127 private transient BaseCalendar.Date cdate; 128 129 private static int defaultCenturyStart; 131 132 136 private static final long serialVersionUID = 7523967970034938905L; 137 138 145 public Date() { 146 this(System.currentTimeMillis()); 147 } 148 149 158 public Date(long date) { 159 fastTime = date; 160 } 161 162 176 @Deprecated 177 public Date(int year, int month, int date) { 178 this(year, month, date, 0, 0, 0); 179 } 180 181 199 @Deprecated 200 public Date(int year, int month, int date, int hrs, int min) { 201 this(year, month, date, hrs, min, 0); 202 } 203 204 223 @Deprecated 224 public Date(int year, int month, int date, int hrs, int min, int sec) { 225 int y = year + 1900; 226 if (month >= 12) { 228 y += month / 12; 229 month %= 12; 230 } else if (month < 0) { 231 y += CalendarUtils.floorDivide(month, 12); 232 month = CalendarUtils.mod(month, 12); 233 } 234 BaseCalendar cal = getCalendarSystem(y); 235 cdate = (BaseCalendar.Date) cal.newCalendarDate(TimeZone.getDefaultRef()); 236 cdate.setNormalizedDate(y, month + 1, date).setTimeOfDay(hrs, min, sec, 0); 237 getTimeImpl(); 238 cdate = null; 239 } 240 241 253 @Deprecated 254 public Date(String s) { 255 this(parse(s)); 256 } 257 258 261 public Object clone() { 262 Date d = null; 263 try { 264 d = (Date )super.clone(); 265 if (cdate != null) { 266 d.cdate = (BaseCalendar.Date) cdate.clone(); 267 } 268 } catch (CloneNotSupportedException e) {} return d; 270 } 271 272 297 @Deprecated 298 public static long UTC(int year, int month, int date, 299 int hrs, int min, int sec) { 300 int y = year + 1900; 301 if (month >= 12) { 303 y += month / 12; 304 month %= 12; 305 } else if (month < 0) { 306 y += CalendarUtils.floorDivide(month, 12); 307 month = CalendarUtils.mod(month, 12); 308 } 309 int m = month + 1; 310 BaseCalendar cal = getCalendarSystem(y); 311 BaseCalendar.Date udate = (BaseCalendar.Date) cal.newCalendarDate(null); 312 udate.setNormalizedDate(y, m, date).setTimeOfDay(hrs, min, sec, 0); 313 314 Date d = new Date (0); 317 d.normalize(udate); 318 return d.fastTime; 319 } 320 321 434 @Deprecated 435 public static long parse(String s) { 436 int year = Integer.MIN_VALUE; 437 int mon = -1; 438 int mday = -1; 439 int hour = -1; 440 int min = -1; 441 int sec = -1; 442 int millis = -1; 443 int c = -1; 444 int i = 0; 445 int n = -1; 446 int wst = -1; 447 int tzoffset = -1; 448 int prevc = 0; 449 syntax: 450 { 451 if (s == null) 452 break syntax; 453 int limit = s.length(); 454 while (i < limit) { 455 c = s.charAt(i); 456 i++; 457 if (c <= ' ' || c == ',') 458 continue; 459 if (c == '(') { int depth = 1; 461 while (i < limit) { 462 c = s.charAt(i); 463 i++; 464 if (c == '(') depth++; 465 else if (c == ')') 466 if (--depth <= 0) 467 break; 468 } 469 continue; 470 } 471 if ('0' <= c && c <= '9') { 472 n = c - '0'; 473 while (i < limit && '0' <= (c = s.charAt(i)) && c <= '9') { 474 n = n * 10 + c - '0'; 475 i++; 476 } 477 if (prevc == '+' || prevc == '-' && year != Integer.MIN_VALUE) { 478 if (n < 24) 480 n = n * 60; else 482 n = n % 100 + n / 100 * 60; if (prevc == '+') n = -n; 485 if (tzoffset != 0 && tzoffset != -1) 486 break syntax; 487 tzoffset = n; 488 } else if (n >= 70) 489 if (year != Integer.MIN_VALUE) 490 break syntax; 491 else if (c <= ' ' || c == ',' || c == '/' || i >= limit) 492 year = n; 494 else 495 break syntax; 496 else if (c == ':') 497 if (hour < 0) 498 hour = (byte) n; 499 else if (min < 0) 500 min = (byte) n; 501 else 502 break syntax; 503 else if (c == '/') 504 if (mon < 0) 505 mon = (byte) (n - 1); 506 else if (mday < 0) 507 mday = (byte) n; 508 else 509 break syntax; 510 else if (i < limit && c != ',' && c > ' ' && c != '-') 511 break syntax; 512 else if (hour >= 0 && min < 0) 513 min = (byte) n; 514 else if (min >= 0 && sec < 0) 515 sec = (byte) n; 516 else if (mday < 0) 517 mday = (byte) n; 518 else if (year == Integer.MIN_VALUE && mon >= 0 && mday >= 0) 520 year = n; 521 else 522 break syntax; 523 prevc = 0; 524 } else if (c == '/' || c == ':' || c == '+' || c == '-') 525 prevc = c; 526 else { 527 int st = i - 1; 528 while (i < limit) { 529 c = s.charAt(i); 530 if (!('A' <= c && c <= 'Z' || 'a' <= c && c <= 'z')) 531 break; 532 i++; 533 } 534 if (i <= st + 1) 535 break syntax; 536 int k; 537 for (k = wtb.length; --k >= 0;) 538 if (wtb[k].regionMatches(true, 0, s, st, i - st)) { 539 int action = ttb[k]; 540 if (action != 0) { 541 if (action == 1) { if (hour > 12 || hour < 1) 543 break syntax; 544 else if (hour < 12) 545 hour += 12; 546 } else if (action == 14) { if (hour > 12 || hour < 1) 548 break syntax; 549 else if (hour == 12) 550 hour = 0; 551 } else if (action <= 13) { if (mon < 0) 553 mon = (byte) (action - 2); 554 else 555 break syntax; 556 } else { 557 tzoffset = action - 10000; 558 } 559 } 560 break; 561 } 562 if (k < 0) 563 break syntax; 564 prevc = 0; 565 } 566 } 567 if (year == Integer.MIN_VALUE || mon < 0 || mday < 0) 568 break syntax; 569 if (year < 100) { 571 synchronized (Date .class) { 572 if (defaultCenturyStart == 0) { 573 defaultCenturyStart = gcal.getCalendarDate().getYear() - 80; 574 } 575 } 576 year += (defaultCenturyStart / 100) * 100; 577 if (year < defaultCenturyStart) year += 100; 578 } 579 if (sec < 0) 580 sec = 0; 581 if (min < 0) 582 min = 0; 583 if (hour < 0) 584 hour = 0; 585 BaseCalendar cal = getCalendarSystem(year); 586 if (tzoffset == -1) { BaseCalendar.Date ldate = (BaseCalendar.Date) cal.newCalendarDate(TimeZone.getDefaultRef()); 588 ldate.setDate(year, mon + 1, mday); 589 ldate.setTimeOfDay(hour, min, sec, 0); 590 return cal.getTime(ldate); 591 } 592 BaseCalendar.Date udate = (BaseCalendar.Date) cal.newCalendarDate(null); udate.setDate(year, mon + 1, mday); 594 udate.setTimeOfDay(hour, min, sec, 0); 595 return cal.getTime(udate) + tzoffset * (60 * 1000); 596 } 597 throw new IllegalArgumentException (); 599 } 600 private final static String wtb[] = { 601 "am", "pm", 602 "monday", "tuesday", "wednesday", "thursday", "friday", 603 "saturday", "sunday", 604 "january", "february", "march", "april", "may", "june", 605 "july", "august", "september", "october", "november", "december", 606 "gmt", "ut", "utc", "est", "edt", "cst", "cdt", 607 "mst", "mdt", "pst", "pdt" 608 }; 609 private final static int ttb[] = { 610 14, 1, 0, 0, 0, 0, 0, 0, 0, 611 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 612 10000 + 0, 10000 + 0, 10000 + 0, 10000 + 5 * 60, 10000 + 4 * 60, 10000 + 6 * 60, 10000 + 5 * 60, 10000 + 7 * 60, 10000 + 6 * 60, 10000 + 8 * 60, 10000 + 7 * 60 }; 618 619 630 @Deprecated 631 public int getYear() { 632 return normalize().getYear() - 1900; 633 } 634 635 650 @Deprecated 651 public void setYear(int year) { 652 getCalendarDate().setNormalizedYear(year + 1900); 653 } 654 655 666 @Deprecated 667 public int getMonth() { 668 return normalize().getMonth() - 1; } 670 671 685 @Deprecated 686 public void setMonth(int month) { 687 int y = 0; 688 if (month >= 12) { 689 y = month / 12; 690 month %= 12; 691 } else if (month < 0) { 692 y = CalendarUtils.floorDivide(month, 12); 693 month = CalendarUtils.mod(month, 12); 694 } 695 BaseCalendar.Date d = getCalendarDate(); 696 if (y != 0) { 697 d.setNormalizedYear(d.getNormalizedYear() + y); 698 } 699 d.setMonth(month + 1); } 701 702 715 @Deprecated 716 public int getDate() { 717 return normalize().getDayOfMonth(); 718 } 719 720 735 @Deprecated 736 public void setDate(int date) { 737 getCalendarDate().setDayOfMonth(date); 738 } 739 740 754 @Deprecated 755 public int getDay() { 756 return normalize().getDayOfWeek() - gcal.SUNDAY; 757 } 758 759 771 @Deprecated 772 public int getHours() { 773 return normalize().getHours(); 774 } 775 776 788 @Deprecated 789 public void setHours(int hours) { 790 getCalendarDate().setHours(hours); 791 } 792 793 803 @Deprecated 804 public int getMinutes() { 805 return normalize().getMinutes(); 806 } 807 808 820 @Deprecated 821 public void setMinutes(int minutes) { 822 getCalendarDate().setMinutes(minutes); 823 } 824 825 836 @Deprecated 837 public int getSeconds() { 838 return normalize().getSeconds(); 839 } 840 841 853 @Deprecated 854 public void setSeconds(int seconds) { 855 getCalendarDate().setSeconds(seconds); 856 } 857 858 865 public long getTime() { 866 return getTimeImpl(); 867 } 868 869 private final long getTimeImpl() { 870 if (cdate != null && !cdate.isNormalized()) { 871 normalize(); 872 } 873 return fastTime; 874 } 875 876 882 public void setTime(long time) { 883 fastTime = time; 884 cdate = null; 885 } 886 887 897 public boolean before(Date when) { 898 return getMillisOf(this) < getMillisOf(when); 899 } 900 901 911 public boolean after(Date when) { 912 return getMillisOf(this) > getMillisOf(when); 913 } 914 915 930 public boolean equals(Object obj) { 931 return obj instanceof Date && getTime() == ((Date ) obj).getTime(); 932 } 933 934 938 static final long getMillisOf(Date date) { 939 if (date.cdate == null) { 940 return date.fastTime; 941 } 942 BaseCalendar.Date d = (BaseCalendar.Date) date.cdate.clone(); 943 return gcal.getTime(d); 944 } 945 946 957 public int compareTo(Date anotherDate) { 958 long thisTime = getMillisOf(this); 959 long anotherTime = getMillisOf(anotherDate); 960 return (thisTime<anotherTime ? -1 : (thisTime==anotherTime ? 0 : 1)); 961 } 962 963 973 public int hashCode() { 974 long ht = this.getTime(); 975 return (int) ht ^ (int) (ht >> 32); 976 } 977 978 1008 public String toString() { 1009 BaseCalendar.Date date = normalize(); 1011 StringBuilder sb = new StringBuilder (28); 1012 int index = date.getDayOfWeek(); 1013 if (index == gcal.SUNDAY) { 1014 index = 8; 1015 } 1016 convertToAbbr(sb, wtb[index]).append(' '); convertToAbbr(sb, wtb[date.getMonth() - 1 + 2 + 7]).append(' '); CalendarUtils.sprintf0d(sb, date.getDayOfMonth(), 2).append(' '); 1020 CalendarUtils.sprintf0d(sb, date.getHours(), 2).append(':'); CalendarUtils.sprintf0d(sb, date.getMinutes(), 2).append(':'); CalendarUtils.sprintf0d(sb, date.getSeconds(), 2).append(' '); TimeZone zi = date.getZone(); 1024 if (zi != null) { 1025 sb.append(zi.getDisplayName(date.isDaylightTime(), zi.SHORT, Locale.US)); } else { 1027 sb.append("GMT"); 1028 } 1029 sb.append(' ').append(date.getYear()); return sb.toString(); 1031 } 1032 1033 1038 private static final StringBuilder convertToAbbr(StringBuilder sb, String name) { 1039 sb.append(Character.toUpperCase(name.charAt(0))); 1040 sb.append(name.charAt(1)).append(name.charAt(2)); 1041 return sb; 1042 } 1043 1044 1060 @Deprecated 1061 public String toLocaleString() { 1062 DateFormat formatter = DateFormat.getDateTimeInstance(); 1063 return formatter.format(this); 1064 } 1065 1066 1097 @Deprecated 1098 public String toGMTString() { 1099 long t = getTime(); 1101 BaseCalendar cal = getCalendarSystem(t); 1102 BaseCalendar.Date date = 1103 (BaseCalendar.Date) cal.getCalendarDate(getTime(), (TimeZone )null); 1104 StringBuilder sb = new StringBuilder (32); 1105 CalendarUtils.sprintf0d(sb, date.getDayOfMonth(), 1).append(' '); convertToAbbr(sb, wtb[date.getMonth() - 1 + 2 + 7]).append(' '); sb.append(date.getYear()).append(' '); CalendarUtils.sprintf0d(sb, date.getHours(), 2).append(':'); CalendarUtils.sprintf0d(sb, date.getMinutes(), 2).append(':'); CalendarUtils.sprintf0d(sb, date.getSeconds(), 2); sb.append(" GMT"); return sb.toString(); 1113 } 1114 1115 1147 @Deprecated 1148 public int getTimezoneOffset() { 1149 int zoneOffset; 1150 if (cdate == null) { 1151 TimeZone tz = TimeZone.getDefaultRef(); 1152 if (tz instanceof ZoneInfo) { 1153 zoneOffset = ((ZoneInfo)tz).getOffsets(fastTime, null); 1154 } else { 1155 zoneOffset = tz.getOffset(fastTime); 1156 } 1157 } else { 1158 normalize(); 1159 zoneOffset = cdate.getZoneOffset(); 1160 } 1161 return -zoneOffset/60000; } 1163 1164 private final BaseCalendar.Date getCalendarDate() { 1165 if (cdate == null) { 1166 BaseCalendar cal = getCalendarSystem(fastTime); 1167 cdate = (BaseCalendar.Date) cal.getCalendarDate(fastTime, 1168 TimeZone.getDefaultRef()); 1169 } 1170 return cdate; 1171 } 1172 1173 private final BaseCalendar.Date normalize() { 1174 if (cdate == null) { 1175 BaseCalendar cal = getCalendarSystem(fastTime); 1176 cdate = (BaseCalendar.Date) cal.getCalendarDate(fastTime, 1177 TimeZone.getDefaultRef()); 1178 return cdate; 1179 } 1180 1181 if (!cdate.isNormalized()) { 1184 cdate = normalize(cdate); 1185 } 1186 1187 TimeZone tz = TimeZone.getDefaultRef(); 1190 if (tz != cdate.getZone()) { 1191 cdate.setZone(tz); 1192 CalendarSystem cal = getCalendarSystem(cdate); 1193 cal.getCalendarDate(fastTime, cdate); 1194 } 1195 return cdate; 1196 } 1197 1198 private final BaseCalendar.Date normalize(BaseCalendar.Date date) { 1200 int y = date.getNormalizedYear(); 1201 int m = date.getMonth(); 1202 int d = date.getDayOfMonth(); 1203 int hh = date.getHours(); 1204 int mm = date.getMinutes(); 1205 int ss = date.getSeconds(); 1206 int ms = date.getMillis(); 1207 TimeZone tz = date.getZone(); 1208 1209 if (y == 1582 || y > 280000000 || y < -280000000) { 1218 if (tz == null) { 1219 tz = TimeZone.getTimeZone("GMT"); 1220 } 1221 GregorianCalendar gc = new GregorianCalendar (tz); 1222 gc.clear(); 1223 gc.set(gc.MILLISECOND, ms); 1224 gc.set(y, m-1, d, hh, mm, ss); 1225 fastTime = gc.getTimeInMillis(); 1226 BaseCalendar cal = getCalendarSystem(fastTime); 1227 date = (BaseCalendar.Date) cal.getCalendarDate(fastTime, tz); 1228 return date; 1229 } 1230 1231 BaseCalendar cal = getCalendarSystem(y); 1232 if (cal != getCalendarSystem(date)) { 1233 date = (BaseCalendar.Date) cal.newCalendarDate(tz); 1234 date.setNormalizedDate(y, m, d).setTimeOfDay(hh, mm, ss, ms); 1235 } 1236 fastTime = cal.getTime(date); 1238 1239 BaseCalendar ncal = getCalendarSystem(fastTime); 1242 if (ncal != cal) { 1243 date = (BaseCalendar.Date) ncal.newCalendarDate(tz); 1244 date.setNormalizedDate(y, m, d).setTimeOfDay(hh, mm, ss, ms); 1245 fastTime = ncal.getTime(date); 1246 } 1247 return date; 1248 } 1249 1250 1257 private static final BaseCalendar getCalendarSystem(int year) { 1258 if (year >= 1582) { 1259 return gcal; 1260 } 1261 return getJulianCalendar(); 1262 } 1263 1264 private static final BaseCalendar getCalendarSystem(long t) { 1265 if (t >= GregorianCalendar.DEFAULT_GREGORIAN_CUTOVER) { 1266 return gcal; 1267 } 1268 return getJulianCalendar(); 1269 } 1270 1271 private static final BaseCalendar getCalendarSystem(BaseCalendar.Date cdate) { 1272 if (jcal == null) { 1273 return gcal; 1274 } 1275 if (cdate.getEra() != null) { 1276 return jcal; 1277 } 1278 return gcal; 1279 } 1280 1281 synchronized private static final BaseCalendar getJulianCalendar() { 1282 if (jcal == null) { 1283 jcal = (BaseCalendar) CalendarSystem.forName("julian"); 1284 } 1285 return jcal; 1286 } 1287 1288 1295 private void writeObject(ObjectOutputStream s) 1296 throws IOException 1297 { 1298 s.writeLong(getTimeImpl()); 1299 } 1300 1301 1304 private void readObject(ObjectInputStream s) 1305 throws IOException , ClassNotFoundException 1306 { 1307 fastTime = s.readLong(); 1308 } 1309} 1310 | Popular Tags |