1 7 8 20 21 package java.text; 22 23 import java.io.InvalidObjectException ; 24 import java.util.HashMap ; 25 import java.util.Locale ; 26 import java.util.Map ; 27 import java.util.ResourceBundle ; 28 import java.util.MissingResourceException ; 29 import java.util.TimeZone ; 30 import java.util.Calendar ; 31 import java.util.GregorianCalendar ; 32 import java.util.Date ; 33 import sun.text.resources.LocaleData; 34 35 124 public abstract class DateFormat extends Format { 125 126 133 protected Calendar calendar; 134 135 141 protected NumberFormat numberFormat; 142 143 147 public final static int ERA_FIELD = 0; 148 152 public final static int YEAR_FIELD = 1; 153 157 public final static int MONTH_FIELD = 2; 158 162 public final static int DATE_FIELD = 3; 163 169 public final static int HOUR_OF_DAY1_FIELD = 4; 170 176 public final static int HOUR_OF_DAY0_FIELD = 5; 177 181 public final static int MINUTE_FIELD = 6; 182 186 public final static int SECOND_FIELD = 7; 187 191 public final static int MILLISECOND_FIELD = 8; 192 196 public final static int DAY_OF_WEEK_FIELD = 9; 197 201 public final static int DAY_OF_YEAR_FIELD = 10; 202 206 public final static int DAY_OF_WEEK_IN_MONTH_FIELD = 11; 207 211 public final static int WEEK_OF_YEAR_FIELD = 12; 212 216 public final static int WEEK_OF_MONTH_FIELD = 13; 217 221 public final static int AM_PM_FIELD = 14; 222 228 public final static int HOUR1_FIELD = 15; 229 235 public final static int HOUR0_FIELD = 16; 236 240 public final static int TIMEZONE_FIELD = 17; 241 242 private static final long serialVersionUID = 7218322306649953788L; 244 245 270 public final StringBuffer format(Object obj, StringBuffer toAppendTo, 271 FieldPosition fieldPosition) 272 { 273 if (obj instanceof Date ) 274 return format( (Date )obj, toAppendTo, fieldPosition ); 275 else if (obj instanceof Number ) 276 return format( new Date (((Number )obj).longValue()), 277 toAppendTo, fieldPosition ); 278 else 279 throw new IllegalArgumentException ("Cannot format given Object as a Date"); 280 } 281 282 304 public abstract StringBuffer format(Date date, StringBuffer toAppendTo, 305 FieldPosition fieldPosition); 306 307 312 public final String format(Date date) 313 { 314 return format(date, new StringBuffer (), 315 DontCareFieldPosition.INSTANCE).toString(); 316 } 317 318 330 public Date parse(String source) throws ParseException 331 { 332 ParsePosition pos = new ParsePosition (0); 333 Date result = parse(source, pos); 334 if (pos.index == 0) 335 throw new ParseException ("Unparseable date: \"" + source + "\"" , 336 pos.errorIndex); 337 return result; 338 } 339 340 360 public abstract Date parse(String source, ParsePosition pos); 361 362 386 public Object parseObject(String source, ParsePosition pos) { 387 return parse(source, pos); 388 } 389 390 393 public static final int FULL = 0; 394 397 public static final int LONG = 1; 398 401 public static final int MEDIUM = 2; 402 405 public static final int SHORT = 3; 406 409 public static final int DEFAULT = MEDIUM; 410 411 416 public final static DateFormat getTimeInstance() 417 { 418 return get(DEFAULT, 0, 1, Locale.getDefault()); 419 } 420 421 428 public final static DateFormat getTimeInstance(int style) 429 { 430 return get(style, 0, 1, Locale.getDefault()); 431 } 432 433 441 public final static DateFormat getTimeInstance(int style, 442 Locale aLocale) 443 { 444 return get(style, 0, 1, aLocale); 445 } 446 447 452 public final static DateFormat getDateInstance() 453 { 454 return get(0, DEFAULT, 2, Locale.getDefault()); 455 } 456 457 464 public final static DateFormat getDateInstance(int style) 465 { 466 return get(0, style, 2, Locale.getDefault()); 467 } 468 469 477 public final static DateFormat getDateInstance(int style, 478 Locale aLocale) 479 { 480 return get(0, style, 2, aLocale); 481 } 482 483 488 public final static DateFormat getDateTimeInstance() 489 { 490 return get(DEFAULT, DEFAULT, 3, Locale.getDefault()); 491 } 492 493 502 public final static DateFormat getDateTimeInstance(int dateStyle, 503 int timeStyle) 504 { 505 return get(timeStyle, dateStyle, 3, Locale.getDefault()); 506 } 507 508 516 public final static DateFormat 517 getDateTimeInstance(int dateStyle, int timeStyle, Locale aLocale) 518 { 519 return get(timeStyle, dateStyle, 3, aLocale); 520 } 521 522 526 public final static DateFormat getInstance() { 527 return getDateTimeInstance(SHORT, SHORT); 528 } 529 530 540 public static Locale [] getAvailableLocales() 541 { 542 return LocaleData.getAvailableLocales("DateTimePatterns"); 543 } 544 545 550 public void setCalendar(Calendar newCalendar) 551 { 552 this.calendar = newCalendar; 553 } 554 555 559 public Calendar getCalendar() 560 { 561 return calendar; 562 } 563 564 568 public void setNumberFormat(NumberFormat newNumberFormat) 569 { 570 this.numberFormat = newNumberFormat; 571 } 572 573 578 public NumberFormat getNumberFormat() 579 { 580 return numberFormat; 581 } 582 583 587 public void setTimeZone(TimeZone zone) 588 { 589 calendar.setTimeZone(zone); 590 } 591 592 596 public TimeZone getTimeZone() 597 { 598 return calendar.getTimeZone(); 599 } 600 601 609 public void setLenient(boolean lenient) 610 { 611 calendar.setLenient(lenient); 612 } 613 614 617 public boolean isLenient() 618 { 619 return calendar.isLenient(); 620 } 621 622 625 public int hashCode() { 626 return numberFormat.hashCode(); 627 } 629 630 633 public boolean equals(Object obj) { 634 if (this == obj) return true; 635 if (obj == null || getClass() != obj.getClass()) return false; 636 DateFormat other = (DateFormat ) obj; 637 return ( calendar.getFirstDayOfWeek() == other.calendar.getFirstDayOfWeek() && 639 calendar.getMinimalDaysInFirstWeek() == other.calendar.getMinimalDaysInFirstWeek() && 640 calendar.isLenient() == other.calendar.isLenient() && 641 calendar.getTimeZone().equals(other.calendar.getTimeZone()) && 642 numberFormat.equals(other.numberFormat)); 643 } 644 645 648 public Object clone() 649 { 650 DateFormat other = (DateFormat ) super.clone(); 651 other.calendar = (Calendar ) calendar.clone(); 652 other.numberFormat = (NumberFormat ) numberFormat.clone(); 653 return other; 654 } 655 656 667 private static DateFormat get(int timeStyle, int dateStyle, 668 int flags, Locale loc) { 669 if ((flags & 1) != 0) { 670 if (timeStyle < 0 || timeStyle > 3) { 671 throw new IllegalArgumentException ("Illegal time style " + timeStyle); 672 } 673 } else { 674 timeStyle = -1; 675 } 676 if ((flags & 2) != 0) { 677 if (dateStyle < 0 || dateStyle > 3) { 678 throw new IllegalArgumentException ("Illegal date style " + dateStyle); 679 } 680 } else { 681 dateStyle = -1; 682 } 683 try { 684 return new SimpleDateFormat (timeStyle, dateStyle, loc); 685 686 } catch (MissingResourceException e) { 687 return new SimpleDateFormat ("M/d/yy h:mm a"); 688 } 689 } 690 691 694 protected DateFormat() {} 695 696 708 public static class Field extends Format.Field { 709 710 private static final long serialVersionUID = 7441350119349544720L; 712 713 private static final Map instanceMap = new HashMap (18); 715 private static final Field[] calendarToFieldMapping = 718 new Field[Calendar.FIELD_COUNT]; 719 720 721 private int calendarField; 722 723 735 public static Field ofCalendarField(int calendarField) { 736 if (calendarField < 0 || calendarField >= 737 calendarToFieldMapping.length) { 738 throw new IllegalArgumentException ("Unknown Calendar constant " 739 + calendarField); 740 } 741 return calendarToFieldMapping[calendarField]; 742 } 743 744 753 protected Field(String name, int calendarField) { 754 super(name); 755 this.calendarField = calendarField; 756 if (this.getClass() == DateFormat.Field .class) { 757 instanceMap.put(name, this); 758 if (calendarField >= 0) { 759 calendarToFieldMapping[calendarField] = this; 761 } 762 } 763 } 764 765 775 public int getCalendarField() { 776 return calendarField; 777 } 778 779 786 protected Object readResolve() throws InvalidObjectException { 787 if (this.getClass() != DateFormat.Field .class) { 788 throw new InvalidObjectException ("subclass didn't correctly implement readResolve"); 789 } 790 791 Object instance = instanceMap.get(getName()); 792 if (instance != null) { 793 return instance; 794 } else { 795 throw new InvalidObjectException ("unknown attribute name"); 796 } 797 } 798 799 803 806 public final static Field ERA = new Field("era", Calendar.ERA); 807 808 811 public final static Field YEAR = new Field("year", Calendar.YEAR); 812 813 816 public final static Field MONTH = new Field("month", Calendar.MONTH); 817 818 821 public final static Field DAY_OF_MONTH = new 822 Field("day of month", Calendar.DAY_OF_MONTH); 823 824 828 public final static Field HOUR_OF_DAY1 = new Field("hour of day 1",-1); 829 830 834 public final static Field HOUR_OF_DAY0 = new 835 Field("hour of day", Calendar.HOUR_OF_DAY); 836 837 840 public final static Field MINUTE =new Field("minute", Calendar.MINUTE); 841 842 845 public final static Field SECOND =new Field("second", Calendar.SECOND); 846 847 850 public final static Field MILLISECOND = new 851 Field("millisecond", Calendar.MILLISECOND); 852 853 856 public final static Field DAY_OF_WEEK = new 857 Field("day of week", Calendar.DAY_OF_WEEK); 858 859 862 public final static Field DAY_OF_YEAR = new 863 Field("day of year", Calendar.DAY_OF_YEAR); 864 865 868 public final static Field DAY_OF_WEEK_IN_MONTH = 869 new Field("day of week in month", 870 Calendar.DAY_OF_WEEK_IN_MONTH); 871 872 875 public final static Field WEEK_OF_YEAR = new 876 Field("week of year", Calendar.WEEK_OF_YEAR); 877 878 881 public final static Field WEEK_OF_MONTH = new 882 Field("week of month", Calendar.WEEK_OF_MONTH); 883 884 888 public final static Field AM_PM = new 889 Field("am pm", Calendar.AM_PM); 890 891 895 public final static Field HOUR1 = new Field("hour 1", -1); 896 897 901 public final static Field HOUR0 = new 902 Field("hour", Calendar.HOUR); 903 904 907 public final static Field TIME_ZONE = new Field("time zone", -1); 908 } 909 } 910 | Popular Tags |