1 5 6 package com.ibm.icu.text; 7 8 import com.ibm.icu.util.Calendar; 10 import com.ibm.icu.util.TimeZone; 11 import com.ibm.icu.util.ULocale; 12 import com.ibm.icu.impl.ICUResourceBundle; 13 import com.ibm.icu.text.UFormat; 14 15 import java.text.FieldPosition ; 16 import java.text.ParseException ; 17 import java.text.ParsePosition ; 18 import java.util.Date ; 19 import java.util.Locale ; 20 import java.util.MissingResourceException ; 21 22 109 public abstract class DateFormat extends UFormat { 110 111 119 protected Calendar calendar; 120 121 128 protected NumberFormat numberFormat; 129 130 135 public final static int ERA_FIELD = 0; 136 137 142 public final static int YEAR_FIELD = 1; 143 144 149 public final static int MONTH_FIELD = 2; 150 151 156 public final static int DATE_FIELD = 3; 157 158 165 public final static int HOUR_OF_DAY1_FIELD = 4; 166 167 174 public final static int HOUR_OF_DAY0_FIELD = 5; 175 176 181 public final static int MINUTE_FIELD = 6; 182 183 188 public final static int SECOND_FIELD = 7; 189 190 195 public final static int FRACTIONAL_SECOND_FIELD = 8; 196 197 201 public final static int MILLISECOND_FIELD = FRACTIONAL_SECOND_FIELD; 202 203 208 public final static int DAY_OF_WEEK_FIELD = 9; 209 210 215 public final static int DAY_OF_YEAR_FIELD = 10; 216 217 222 public final static int DAY_OF_WEEK_IN_MONTH_FIELD = 11; 223 224 229 public final static int WEEK_OF_YEAR_FIELD = 12; 230 231 236 public final static int WEEK_OF_MONTH_FIELD = 13; 237 238 243 public final static int AM_PM_FIELD = 14; 244 245 252 public final static int HOUR1_FIELD = 15; 253 254 261 public final static int HOUR0_FIELD = 16; 262 263 269 public final static int TIMEZONE_FIELD = 17; 270 271 276 public final static int YEAR_WOY_FIELD = 18; 277 278 283 public final static int DOW_LOCAL_FIELD = 19; 284 285 290 public final static int EXTENDED_YEAR_FIELD = 20; 291 292 297 public final static int JULIAN_DAY_FIELD = 21; 298 299 304 public final static int MILLISECONDS_IN_DAY_FIELD = 22; 305 306 312 public final static int TIMEZONE_RFC_FIELD = 23; 313 314 322 public final static int TIMEZONE_GENERIC_FIELD = 24; 323 324 325 326 333 public final static int STANDALONE_DAY_FIELD = 25; 334 335 342 public final static int STANDALONE_MONTH_FIELD = 26; 343 344 351 public final static int QUARTER_FIELD = 27; 352 353 360 public final static int STANDALONE_QUARTER_FIELD = 28; 361 362 367 public final static int FIELD_COUNT = 29; 369 private static final long serialVersionUID = 7218322306649953788L; 371 372 398 public final StringBuffer format(Object obj, StringBuffer toAppendTo, 399 FieldPosition fieldPosition) 400 { 401 if (obj instanceof Calendar) 402 return format( (Calendar)obj, toAppendTo, fieldPosition ); 403 else if (obj instanceof Date ) 404 return format( (Date )obj, toAppendTo, fieldPosition ); 405 else if (obj instanceof Number ) 406 return format( new Date (((Number )obj).longValue()), 407 toAppendTo, fieldPosition ); 408 else 409 throw new IllegalArgumentException ("Cannot format given Object as a Date"); 410 } 411 412 436 public abstract StringBuffer format(Calendar cal, StringBuffer toAppendTo, 437 FieldPosition fieldPosition); 438 439 462 public StringBuffer format(Date date, StringBuffer toAppendTo, 463 FieldPosition fieldPosition) { 464 calendar.setTime(date); 466 return format(calendar, toAppendTo, fieldPosition); 467 } 468 469 475 public final String format(Date date) 476 { 477 return format(date, new StringBuffer (64),new FieldPosition (0)).toString(); 478 } 479 480 492 public Date parse(String text) throws ParseException 493 { 494 ParsePosition pos = new ParsePosition (0); 495 Date result = parse(text, pos); 496 if (pos.getIndex() == 0) throw new ParseException ("Unparseable date: \"" + text + "\"" , 498 pos.getErrorIndex()); return result; 500 } 501 502 528 public abstract void parse(String text, Calendar cal, ParsePosition pos); 529 530 551 public Date parse(String text, ParsePosition pos) { 552 int start = pos.getIndex(); 553 calendar.clear(); 554 parse(text, calendar, pos); 555 if (pos.getIndex() != start) { 556 try { 557 return calendar.getTime(); 558 } catch (IllegalArgumentException e) { 559 pos.setIndex(start); 563 pos.setErrorIndex(start); 564 } 565 } 566 return null; 567 } 568 569 576 public Object parseObject (String source, ParsePosition pos) 577 { 578 return parse(source, pos); 579 } 580 581 585 public static final int FULL = 0; 586 587 591 public static final int LONG = 1; 592 593 597 public static final int MEDIUM = 2; 598 599 603 public static final int SHORT = 3; 604 605 609 public static final int DEFAULT = MEDIUM; 610 611 617 public final static DateFormat getTimeInstance() 618 { 619 return get(-1, DEFAULT, ULocale.getDefault()); 620 } 621 622 630 public final static DateFormat getTimeInstance(int style) 631 { 632 return get(-1, style, ULocale.getDefault()); 633 } 634 635 644 public final static DateFormat getTimeInstance(int style, 645 Locale aLocale) 646 { 647 return get(-1, style, ULocale.forLocale(aLocale)); 648 } 649 650 660 public final static DateFormat getTimeInstance(int style, 661 ULocale locale) 662 { 663 return get(-1, style, locale); 664 } 665 666 672 public final static DateFormat getDateInstance() 673 { 674 return get(DEFAULT, -1, ULocale.getDefault()); 675 } 676 677 685 public final static DateFormat getDateInstance(int style) 686 { 687 return get(style, -1, ULocale.getDefault()); 688 } 689 690 699 public final static DateFormat getDateInstance(int style, 700 Locale aLocale) 701 { 702 return get(style, -1, ULocale.forLocale(aLocale)); 703 } 704 705 715 public final static DateFormat getDateInstance(int style, 716 ULocale locale) 717 { 718 return get(style, -1, locale); 719 } 720 721 727 public final static DateFormat getDateTimeInstance() 728 { 729 return get(DEFAULT, DEFAULT, ULocale.getDefault()); 730 } 731 732 742 public final static DateFormat getDateTimeInstance(int dateStyle, 743 int timeStyle) 744 { 745 return get(dateStyle, timeStyle, ULocale.getDefault()); 746 } 747 748 757 public final static DateFormat 758 getDateTimeInstance(int dateStyle, int timeStyle, Locale aLocale) 759 { 760 return get(dateStyle, timeStyle, ULocale.forLocale(aLocale)); 761 } 762 763 773 public final static DateFormat 774 getDateTimeInstance(int dateStyle, int timeStyle, ULocale locale) 775 { 776 return get(dateStyle, timeStyle, locale); 777 } 778 779 784 public final static DateFormat getInstance() { 785 return getDateTimeInstance(SHORT, SHORT); 786 } 787 788 793 public static Locale[] getAvailableLocales() 794 { 795 return ICUResourceBundle.getAvailableLocales(ICUResourceBundle.ICU_BASE_NAME); 796 } 797 798 804 public static ULocale[] getAvailableULocales() 805 { 806 return ICUResourceBundle.getAvailableULocales(ICUResourceBundle.ICU_BASE_NAME); 807 } 808 809 815 public void setCalendar(Calendar newCalendar) 816 { 817 this.calendar = newCalendar; 818 } 819 820 825 public Calendar getCalendar() 826 { 827 return calendar; 828 } 829 830 835 public void setNumberFormat(NumberFormat newNumberFormat) 836 { 837 this.numberFormat = newNumberFormat; 838 841 this.numberFormat.setParseIntegerOnly(true); 842 } 843 844 850 public NumberFormat getNumberFormat() 851 { 852 return numberFormat; 853 } 854 855 860 public void setTimeZone(TimeZone zone) 861 { 862 calendar.setTimeZone(zone); 863 } 864 865 870 public TimeZone getTimeZone() 871 { 872 return calendar.getTimeZone(); 873 } 874 875 884 public void setLenient(boolean lenient) 885 { 886 calendar.setLenient(lenient); 887 } 888 889 893 public boolean isLenient() 894 { 895 return calendar.isLenient(); 896 } 897 898 902 public int hashCode() { 905 return numberFormat.hashCode(); 906 } 908 910 914 public boolean equals(Object obj) { 915 if (this == obj) return true; 916 if (obj == null || getClass() != obj.getClass()) return false; 917 DateFormat other = (DateFormat) obj; 918 return (calendar.isEquivalentTo(other.calendar) && 919 numberFormat.equals(other.numberFormat)); 920 } 921 922 926 public Object clone() 927 { 928 DateFormat other = (DateFormat) super.clone(); 929 other.calendar = (Calendar) calendar.clone(); 930 other.numberFormat = (NumberFormat) numberFormat.clone(); 931 return other; 932 } 933 934 943 private static DateFormat get(int dateStyle, int timeStyle, ULocale loc) { 944 if (timeStyle < -1 || timeStyle > 3) { 945 throw new IllegalArgumentException ("Illegal time style " + timeStyle); 946 } 947 if (dateStyle < -1 || dateStyle > 3) { 948 throw new IllegalArgumentException ("Illegal date style " + dateStyle); 949 } 950 try { 951 Calendar cal = Calendar.getInstance(loc); 952 DateFormat result = cal.getDateTimeFormat(dateStyle, timeStyle, loc); 953 result.setLocale(cal.getLocale(ULocale.VALID_LOCALE), 954 cal.getLocale(ULocale.ACTUAL_LOCALE)); 955 return result; 956 } catch (MissingResourceException e) { 957 return new SimpleDateFormat("M/d/yy h:mm a"); 960 } 962 } 963 964 968 protected DateFormat() {} 969 970 972 977 990 static final public DateFormat getDateInstance(Calendar cal, int dateStyle, Locale locale) 991 { 992 return getDateTimeInstance(cal, dateStyle, -1, ULocale.forLocale(locale)); 993 } 994 995 1009 static final public DateFormat getDateInstance(Calendar cal, int dateStyle, ULocale locale) 1010 { 1011 return getDateTimeInstance(cal, dateStyle, -1, locale); 1012 } 1013 1014 1032 static final public DateFormat getTimeInstance(Calendar cal, int timeStyle, Locale locale) 1033 { 1034 return getDateTimeInstance(cal, -1, timeStyle, ULocale.forLocale(locale)); 1035 } 1036 1037 1056 static final public DateFormat getTimeInstance(Calendar cal, int timeStyle, ULocale locale) 1057 { 1058 return getDateTimeInstance(cal, -1, timeStyle, locale); 1059 } 1060 1061 1083 static final public DateFormat getDateTimeInstance(Calendar cal, int dateStyle, 1084 int timeStyle, Locale locale) 1085 { 1086 return cal.getDateTimeFormat(dateStyle, timeStyle, ULocale.forLocale(locale)); 1087 } 1088 1089 1112 static final public DateFormat getDateTimeInstance(Calendar cal, int dateStyle, 1113 int timeStyle, ULocale locale) 1114 { 1115 return cal.getDateTimeFormat(dateStyle, timeStyle, locale); 1116 } 1117 1118 1122 static final public DateFormat getInstance(Calendar cal, Locale locale) { 1123 return getDateTimeInstance(cal, SHORT, SHORT, ULocale.forLocale(locale)); 1124 } 1125 1126 1131 static final public DateFormat getInstance(Calendar cal, ULocale locale) { 1132 return getDateTimeInstance(cal, SHORT, SHORT, locale); 1133 } 1134 1135 1139 static final public DateFormat getInstance(Calendar cal) { 1140 return getInstance(cal, ULocale.getDefault()); 1141 } 1142 1143 1147 static final public DateFormat getDateInstance(Calendar cal, int dateStyle) { 1148 return getDateInstance(cal, dateStyle, ULocale.getDefault()); 1149 } 1150 1151 1155 static final public DateFormat getTimeInstance(Calendar cal, int timeStyle) { 1156 return getTimeInstance(cal, timeStyle, ULocale.getDefault()); 1157 } 1158 1159 1163 static final public DateFormat getDateTimeInstance(Calendar cal, int dateStyle, int timeStyle) { 1164 return getDateTimeInstance(cal, dateStyle, timeStyle, ULocale.getDefault()); 1165 } 1166} 1167 | Popular Tags |