| 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 @Deprecat
|