0001: /*
0002: * Copyright (C) 1996-2006, International Business Machines
0003: * Corporation and others. All Rights Reserved.
0004: */
0005:
0006: package com.ibm.icu.text;
0007:
0008: //import com.ibm.icu.impl.ICULocaleData;
0009: import com.ibm.icu.util.Calendar;
0010: import com.ibm.icu.util.TimeZone;
0011: import com.ibm.icu.util.ULocale;
0012: import com.ibm.icu.impl.ICUResourceBundle;
0013: import com.ibm.icu.text.UFormat;
0014:
0015: import java.text.FieldPosition;
0016: import java.text.ParseException;
0017: import java.text.ParsePosition;
0018: import java.util.Date;
0019: import java.util.Locale;
0020: import java.util.MissingResourceException;
0021:
0022: /**
0023: * DateFormat is an abstract class for date/time formatting subclasses which
0024: * formats and parses dates or time in a language-independent manner.
0025: * The date/time formatting subclass, such as SimpleDateFormat, allows for
0026: * formatting (i.e., date -> text), parsing (text -> date), and
0027: * normalization. The date is represented as a <code>Date</code> object or
0028: * as the milliseconds since January 1, 1970, 00:00:00 GMT.
0029: *
0030: * <p>DateFormat provides many class methods for obtaining default date/time
0031: * formatters based on the default or a given loacle and a number of formatting
0032: * styles. The formatting styles include FULL, LONG, MEDIUM, and SHORT. More
0033: * detail and examples of using these styles are provided in the method
0034: * descriptions.
0035: *
0036: * <p>DateFormat helps you to format and parse dates for any locale.
0037: * Your code can be completely independent of the locale conventions for
0038: * months, days of the week, or even the calendar format: lunar vs. solar.
0039: *
0040: * <p>To format a date for the current Locale, use one of the
0041: * static factory methods:
0042: * <pre>
0043: * myString = DateFormat.getDateInstance().format(myDate);
0044: * </pre>
0045: * <p>If you are formatting multiple numbers, it is
0046: * more efficient to get the format and use it multiple times so that
0047: * the system doesn't have to fetch the information about the local
0048: * language and country conventions multiple times.
0049: * <pre>
0050: * DateFormat df = DateFormat.getDateInstance();
0051: * for (int i = 0; i < a.length; ++i) {
0052: * output.println(df.format(myDate[i]) + "; ");
0053: * }
0054: * </pre>
0055: * <p>To format a number for a different Locale, specify it in the
0056: * call to getDateInstance().
0057: * <pre>
0058: * DateFormat df = DateFormat.getDateInstance(DateFormat.LONG, Locale.FRANCE);
0059: * </pre>
0060: * <p>You can use a DateFormat to parse also.
0061: * <pre>
0062: * myDate = df.parse(myString);
0063: * </pre>
0064: * <p>Use getDateInstance to get the normal date format for that country.
0065: * There are other static factory methods available.
0066: * Use getTimeInstance to get the time format for that country.
0067: * Use getDateTimeInstance to get a date and time format. You can pass in
0068: * different options to these factory methods to control the length of the
0069: * result; from SHORT to MEDIUM to LONG to FULL. The exact result depends
0070: * on the locale, but generally:
0071: * <ul><li>SHORT is completely numeric, such as 12.13.52 or 3:30pm
0072: * <li>MEDIUM is longer, such as Jan 12, 1952
0073: * <li>LONG is longer, such as January 12, 1952 or 3:30:32pm
0074: * <li>FULL is pretty completely specified, such as
0075: * Tuesday, April 12, 1952 AD or 3:30:42pm PST.
0076: * </ul>
0077: *
0078: * <p>You can also set the time zone on the format if you wish.
0079: * If you want even more control over the format or parsing,
0080: * (or want to give your users more control),
0081: * you can try casting the DateFormat you get from the factory methods
0082: * to a SimpleDateFormat. This will work for the majority
0083: * of countries; just remember to put it in a try block in case you
0084: * encounter an unusual one.
0085: *
0086: * <p>You can also use forms of the parse and format methods with
0087: * ParsePosition and FieldPosition to
0088: * allow you to
0089: * <ul><li>progressively parse through pieces of a string.
0090: * <li>align any particular field, or find out where it is for selection
0091: * on the screen.
0092: * </ul>
0093: *
0094: * <h4>Synchronization</h4>
0095: *
0096: * Date formats are not synchronized. It is recommended to create separate
0097: * format instances for each thread. If multiple threads access a format
0098: * concurrently, it must be synchronized externally.
0099: *
0100: * @see UFormat
0101: * @see NumberFormat
0102: * @see SimpleDateFormat
0103: * @see com.ibm.icu.util.Calendar
0104: * @see com.ibm.icu.util.GregorianCalendar
0105: * @see com.ibm.icu.util.TimeZone
0106: * @author Mark Davis, Chen-Lieh Huang, Alan Liu
0107: * @stable ICU 2.0
0108: */
0109: public abstract class DateFormat extends UFormat {
0110:
0111: /**
0112: * The calendar that <code>DateFormat</code> uses to produce the time field
0113: * values needed to implement date and time formatting. Subclasses should
0114: * initialize this to a calendar appropriate for the locale associated with
0115: * this <code>DateFormat</code>.
0116: * @serial
0117: * @stable ICU 2.0
0118: */
0119: protected Calendar calendar;
0120:
0121: /**
0122: * The number formatter that <code>DateFormat</code> uses to format numbers
0123: * in dates and times. Subclasses should initialize this to a number format
0124: * appropriate for the locale associated with this <code>DateFormat</code>.
0125: * @serial
0126: * @stable ICU 2.0
0127: */
0128: protected NumberFormat numberFormat;
0129:
0130: /**
0131: * FieldPosition selector for 'G' field alignment,
0132: * corresponding to the {@link Calendar#ERA} field.
0133: * @stable ICU 2.0
0134: */
0135: public final static int ERA_FIELD = 0;
0136:
0137: /**
0138: * FieldPosition selector for 'y' field alignment,
0139: * corresponding to the {@link Calendar#YEAR} field.
0140: * @stable ICU 2.0
0141: */
0142: public final static int YEAR_FIELD = 1;
0143:
0144: /**
0145: * FieldPosition selector for 'M' field alignment,
0146: * corresponding to the {@link Calendar#MONTH} field.
0147: * @stable ICU 2.0
0148: */
0149: public final static int MONTH_FIELD = 2;
0150:
0151: /**
0152: * FieldPosition selector for 'd' field alignment,
0153: * corresponding to the {@link Calendar#DATE} field.
0154: * @stable ICU 2.0
0155: */
0156: public final static int DATE_FIELD = 3;
0157:
0158: /**
0159: * FieldPosition selector for 'k' field alignment,
0160: * corresponding to the {@link Calendar#HOUR_OF_DAY} field.
0161: * HOUR_OF_DAY1_FIELD is used for the one-based 24-hour clock.
0162: * For example, 23:59 + 01:00 results in 24:59.
0163: * @stable ICU 2.0
0164: */
0165: public final static int HOUR_OF_DAY1_FIELD = 4;
0166:
0167: /**
0168: * FieldPosition selector for 'H' field alignment,
0169: * corresponding to the {@link Calendar#HOUR_OF_DAY} field.
0170: * HOUR_OF_DAY0_FIELD is used for the zero-based 24-hour clock.
0171: * For example, 23:59 + 01:00 results in 00:59.
0172: * @stable ICU 2.0
0173: */
0174: public final static int HOUR_OF_DAY0_FIELD = 5;
0175:
0176: /**
0177: * FieldPosition selector for 'm' field alignment,
0178: * corresponding to the {@link Calendar#MINUTE} field.
0179: * @stable ICU 2.0
0180: */
0181: public final static int MINUTE_FIELD = 6;
0182:
0183: /**
0184: * FieldPosition selector for 's' field alignment,
0185: * corresponding to the {@link Calendar#SECOND} field.
0186: * @stable ICU 2.0
0187: */
0188: public final static int SECOND_FIELD = 7;
0189:
0190: /**
0191: * FieldPosition selector for 'S' field alignment,
0192: * corresponding to the {@link Calendar#MILLISECOND} field.
0193: * @stable ICU 3.0
0194: */
0195: public final static int FRACTIONAL_SECOND_FIELD = 8;
0196:
0197: /**
0198: * Alias for FRACTIONAL_SECOND_FIELD.
0199: * @deprecated ICU 3.0 use FRACTIONAL_SECOND_FIELD.
0200: */
0201: public final static int MILLISECOND_FIELD = FRACTIONAL_SECOND_FIELD;
0202:
0203: /**
0204: * FieldPosition selector for 'E' field alignment,
0205: * corresponding to the {@link Calendar#DAY_OF_WEEK} field.
0206: * @stable ICU 2.0
0207: */
0208: public final static int DAY_OF_WEEK_FIELD = 9;
0209:
0210: /**
0211: * FieldPosition selector for 'D' field alignment,
0212: * corresponding to the {@link Calendar#DAY_OF_YEAR} field.
0213: * @stable ICU 2.0
0214: */
0215: public final static int DAY_OF_YEAR_FIELD = 10;
0216:
0217: /**
0218: * FieldPosition selector for 'F' field alignment,
0219: * corresponding to the {@link Calendar#DAY_OF_WEEK_IN_MONTH} field.
0220: * @stable ICU 2.0
0221: */
0222: public final static int DAY_OF_WEEK_IN_MONTH_FIELD = 11;
0223:
0224: /**
0225: * FieldPosition selector for 'w' field alignment,
0226: * corresponding to the {@link Calendar#WEEK_OF_YEAR} field.
0227: * @stable ICU 2.0
0228: */
0229: public final static int WEEK_OF_YEAR_FIELD = 12;
0230:
0231: /**
0232: * FieldPosition selector for 'W' field alignment,
0233: * corresponding to the {@link Calendar#WEEK_OF_MONTH} field.
0234: * @stable ICU 2.0
0235: */
0236: public final static int WEEK_OF_MONTH_FIELD = 13;
0237:
0238: /**
0239: * FieldPosition selector for 'a' field alignment,
0240: * corresponding to the {@link Calendar#AM_PM} field.
0241: * @stable ICU 2.0
0242: */
0243: public final static int AM_PM_FIELD = 14;
0244:
0245: /**
0246: * FieldPosition selector for 'h' field alignment,
0247: * corresponding to the {@link Calendar#HOUR} field.
0248: * HOUR1_FIELD is used for the one-based 12-hour clock.
0249: * For example, 11:30 PM + 1 hour results in 12:30 AM.
0250: * @stable ICU 2.0
0251: */
0252: public final static int HOUR1_FIELD = 15;
0253:
0254: /**
0255: * FieldPosition selector for 'K' field alignment,
0256: * corresponding to the {@link Calendar#HOUR} field.
0257: * HOUR0_FIELD is used for the zero-based 12-hour clock.
0258: * For example, 11:30 PM + 1 hour results in 00:30 AM.
0259: * @stable ICU 2.0
0260: */
0261: public final static int HOUR0_FIELD = 16;
0262:
0263: /**
0264: * FieldPosition selector for 'z' field alignment,
0265: * corresponding to the {@link Calendar#ZONE_OFFSET} and
0266: * {@link Calendar#DST_OFFSET} fields.
0267: * @stable ICU 2.0
0268: */
0269: public final static int TIMEZONE_FIELD = 17;
0270:
0271: /**
0272: * FieldPosition selector for 'Y' field alignment,
0273: * corresponding to the {@link Calendar#YEAR_WOY} field.
0274: * @stable ICU 3.0
0275: */
0276: public final static int YEAR_WOY_FIELD = 18;
0277:
0278: /**
0279: * FieldPosition selector for 'e' field alignment,
0280: * corresponding to the {@link Calendar#DOW_LOCAL} field.
0281: * @stable ICU 3.0
0282: */
0283: public final static int DOW_LOCAL_FIELD = 19;
0284:
0285: /**
0286: * FieldPosition selector for 'u' field alignment,
0287: * corresponding to the {@link Calendar#EXTENDED_YEAR} field.
0288: * @stable ICU 3.0
0289: */
0290: public final static int EXTENDED_YEAR_FIELD = 20;
0291:
0292: /**
0293: * FieldPosition selector for 'g' field alignment,
0294: * corresponding to the {@link Calendar#JULIAN_DAY} field.
0295: * @stable ICU 3.0
0296: */
0297: public final static int JULIAN_DAY_FIELD = 21;
0298:
0299: /**
0300: * FieldPosition selector for 'A' field alignment,
0301: * corresponding to the {@link Calendar#MILLISECONDS_IN_DAY} field.
0302: * @stable ICU 3.0
0303: */
0304: public final static int MILLISECONDS_IN_DAY_FIELD = 22;
0305:
0306: /**
0307: * FieldPosition selector for 'Z' field alignment,
0308: * corresponding to the {@link Calendar#ZONE_OFFSET} and
0309: * {@link Calendar#DST_OFFSET} fields.
0310: * @stable ICU 3.0
0311: */
0312: public final static int TIMEZONE_RFC_FIELD = 23;
0313:
0314: /**
0315: * FieldPosition selector for 'v' field alignment,
0316: * corresponding to the {@link Calendar#ZONE_OFFSET} and
0317: * {@link Calendar#DST_OFFSET} fields. This displays the generic zone
0318: * name, if available.
0319: * @draft ICU 3.4
0320: * @provisional This API might change or be removed in a future release.
0321: */
0322: public final static int TIMEZONE_GENERIC_FIELD = 24;
0323:
0324: /**
0325: * FieldPosition selector for 'c' field alignment,
0326: * corresponding to the {@link Calendar#DAY_OF_WEEK} field.
0327: * This displays the stand alone day name, if available.
0328: * @draft ICU 3.4
0329: * @provisional This API might change or be removed in a future release.
0330: */
0331: public final static int STANDALONE_DAY_FIELD = 25;
0332:
0333: /**
0334: * FieldPosition selector for 'L' field alignment,
0335: * corresponding to the {@link Calendar#MONTH} field.
0336: * This displays the stand alone month name, if available.
0337: * @draft ICU 3.4
0338: * @provisional This API might change or be removed in a future release.
0339: */
0340: public final static int STANDALONE_MONTH_FIELD = 26;
0341:
0342: /**
0343: * FieldPosition selector for 'Q' field alignment,
0344: * corresponding to the {@link Calendar#MONTH} field.
0345: * This displays the quarter.
0346: * @draft ICU 3.6
0347: * @provisional This API might change or be removed in a future release.
0348: */
0349: public final static int QUARTER_FIELD = 27;
0350:
0351: /**
0352: * FieldPosition selector for 'q' field alignment,
0353: * corresponding to the {@link Calendar#MONTH} field.
0354: * This displays the stand alone quarter, if available.
0355: * @draft ICU 3.6
0356: * @provisional This API might change or be removed in a future release.
0357: */
0358: public final static int STANDALONE_QUARTER_FIELD = 28;
0359:
0360: /**
0361: * Number of FieldPosition selectors for DateFormat.
0362: * Valid selectors range from 0 to FIELD_COUNT-1.
0363: * @stable ICU 3.0
0364: */
0365: public final static int FIELD_COUNT = 29; // must == DateFormatSymbols.patternChars.length()
0366:
0367: // Proclaim serial compatibility with 1.1 FCS
0368: private static final long serialVersionUID = 7218322306649953788L;
0369:
0370: /**
0371: * Overrides Format.
0372: * Formats a time object into a time string. Examples of time objects
0373: * are a time value expressed in milliseconds and a Date object.
0374: * @param obj must be a Number or a Date or a Calendar.
0375: * @param toAppendTo the string buffer for the returning time string.
0376: * @return the formatted time string.
0377: * @param fieldPosition keeps track of the position of the field
0378: * within the returned string.
0379: * On input: an alignment field,
0380: * if desired. On output: the offsets of the alignment field. For
0381: * example, given a time text "1996.07.10 AD at 15:08:56 PDT",
0382: * if the given fieldPosition is DateFormat.YEAR_FIELD, the
0383: * begin index and end index of fieldPosition will be set to
0384: * 0 and 4, respectively.
0385: * Notice that if the same time field appears
0386: * more than once in a pattern, the fieldPosition will be set for the first
0387: * occurence of that time field. For instance, formatting a Date to
0388: * the time string "1 PM PDT (Pacific Daylight Time)" using the pattern
0389: * "h a z (zzzz)" and the alignment field DateFormat.TIMEZONE_FIELD,
0390: * the begin index and end index of fieldPosition will be set to
0391: * 5 and 8, respectively, for the first occurence of the timezone
0392: * pattern character 'z'.
0393: * @see java.text.Format
0394: * @stable ICU 2.0
0395: */
0396: public final StringBuffer format(Object obj,
0397: StringBuffer toAppendTo, FieldPosition fieldPosition) {
0398: if (obj instanceof Calendar)
0399: return format((Calendar) obj, toAppendTo, fieldPosition);
0400: else if (obj instanceof Date)
0401: return format((Date) obj, toAppendTo, fieldPosition);
0402: else if (obj instanceof Number)
0403: return format(new Date(((Number) obj).longValue()),
0404: toAppendTo, fieldPosition);
0405: else
0406: throw new IllegalArgumentException(
0407: "Cannot format given Object as a Date");
0408: }
0409:
0410: /**
0411: * Formats a date into a date/time string.
0412: * @param cal a Calendar set to the date and time to be formatted
0413: * into a date/time string.
0414: * @param toAppendTo the string buffer for the returning date/time string.
0415: * @param fieldPosition keeps track of the position of the field
0416: * within the returned string.
0417: * On input: an alignment field,
0418: * if desired. On output: the offsets of the alignment field. For
0419: * example, given a time text "1996.07.10 AD at 15:08:56 PDT",
0420: * if the given fieldPosition is DateFormat.YEAR_FIELD, the
0421: * begin index and end index of fieldPosition will be set to
0422: * 0 and 4, respectively.
0423: * Notice that if the same time field appears
0424: * more than once in a pattern, the fieldPosition will be set for the first
0425: * occurence of that time field. For instance, formatting a Date to
0426: * the time string "1 PM PDT (Pacific Daylight Time)" using the pattern
0427: * "h a z (zzzz)" and the alignment field DateFormat.TIMEZONE_FIELD,
0428: * the begin index and end index of fieldPosition will be set to
0429: * 5 and 8, respectively, for the first occurence of the timezone
0430: * pattern character 'z'.
0431: * @return the formatted date/time string.
0432: * @stable ICU 2.0
0433: */
0434: public abstract StringBuffer format(Calendar cal,
0435: StringBuffer toAppendTo, FieldPosition fieldPosition);
0436:
0437: /**
0438: * Formats a Date into a date/time string.
0439: * @param date a Date to be formatted into a date/time string.
0440: * @param toAppendTo the string buffer for the returning date/time string.
0441: * @param fieldPosition keeps track of the position of the field
0442: * within the returned string.
0443: * On input: an alignment field,
0444: * if desired. On output: the offsets of the alignment field. For
0445: * example, given a time text "1996.07.10 AD at 15:08:56 PDT",
0446: * if the given fieldPosition is DateFormat.YEAR_FIELD, the
0447: * begin index and end index of fieldPosition will be set to
0448: * 0 and 4, respectively.
0449: * Notice that if the same time field appears
0450: * more than once in a pattern, the fieldPosition will be set for the first
0451: * occurence of that time field. For instance, formatting a Date to
0452: * the time string "1 PM PDT (Pacific Daylight Time)" using the pattern
0453: * "h a z (zzzz)" and the alignment field DateFormat.TIMEZONE_FIELD,
0454: * the begin index and end index of fieldPosition will be set to
0455: * 5 and 8, respectively, for the first occurence of the timezone
0456: * pattern character 'z'.
0457: * @return the formatted date/time string.
0458: * @stable ICU 2.0
0459: */
0460: public StringBuffer format(Date date, StringBuffer toAppendTo,
0461: FieldPosition fieldPosition) {
0462: // Use our Calendar object
0463: calendar.setTime(date);
0464: return format(calendar, toAppendTo, fieldPosition);
0465: }
0466:
0467: /**
0468: * Formats a Date into a date/time string.
0469: * @param date the time value to be formatted into a time string.
0470: * @return the formatted time string.
0471: * @stable ICU 2.0
0472: */
0473: public final String format(Date date) {
0474: return format(date, new StringBuffer(64), new FieldPosition(0))
0475: .toString();
0476: }
0477:
0478: /**
0479: * Parse a date/time string.
0480: *
0481: * @param text The date/time string to be parsed
0482: *
0483: * @return A Date, or null if the input could not be parsed
0484: *
0485: * @exception ParseException If the given string cannot be parsed as a date.
0486: *
0487: * @see #parse(String, ParsePosition)
0488: * @stable ICU 2.0
0489: */
0490: public Date parse(String text) throws ParseException {
0491: ParsePosition pos = new ParsePosition(0);
0492: Date result = parse(text, pos);
0493: if (pos.getIndex() == 0) // ICU4J
0494: throw new ParseException("Unparseable date: \"" + text
0495: + "\"", pos.getErrorIndex()); // ICU4J
0496: return result;
0497: }
0498:
0499: /**
0500: * Parse a date/time string according to the given parse position.
0501: * For example, a time text "07/10/96 4:5 PM, PDT" will be parsed
0502: * into a Calendar that is equivalent to Date(837039928046). The
0503: * caller should clear the calendar before calling this method,
0504: * unless existing field information is to be kept.
0505: *
0506: * <p> By default, parsing is lenient: If the input is not in the form used
0507: * by this object's format method but can still be parsed as a date, then
0508: * the parse succeeds. Clients may insist on strict adherence to the
0509: * format by calling setLenient(false).
0510: *
0511: * @see #setLenient(boolean)
0512: *
0513: * @param text The date/time string to be parsed
0514: *
0515: * @param cal The calendar into which parsed data will be stored.
0516: * In general, this should be cleared before calling this
0517: * method. If this parse fails, the calendar may still
0518: * have been modified.
0519: *
0520: * @param pos On input, the position at which to start parsing; on
0521: * output, the position at which parsing terminated, or the
0522: * start position if the parse failed.
0523: * @stable ICU 2.0
0524: */
0525: public abstract void parse(String text, Calendar cal,
0526: ParsePosition pos);
0527:
0528: /**
0529: * Parse a date/time string according to the given parse position. For
0530: * example, a time text "07/10/96 4:5 PM, PDT" will be parsed into a Date
0531: * that is equivalent to Date(837039928046).
0532: *
0533: * <p> By default, parsing is lenient: If the input is not in the form used
0534: * by this object's format method but can still be parsed as a date, then
0535: * the parse succeeds. Clients may insist on strict adherence to the
0536: * format by calling setLenient(false).
0537: *
0538: * @see #setLenient(boolean)
0539: *
0540: * @param text The date/time string to be parsed
0541: *
0542: * @param pos On input, the position at which to start parsing; on
0543: * output, the position at which parsing terminated, or the
0544: * start position if the parse failed.
0545: *
0546: * @return A Date, or null if the input could not be parsed
0547: * @stable ICU 2.0
0548: */
0549: public Date parse(String text, ParsePosition pos) {
0550: int start = pos.getIndex();
0551: calendar.clear();
0552: parse(text, calendar, pos);
0553: if (pos.getIndex() != start) {
0554: try {
0555: return calendar.getTime();
0556: } catch (IllegalArgumentException e) {
0557: // This occurs if the calendar is non-lenient and there is
0558: // an out-of-range field. We don't know which field was
0559: // illegal so we set the error index to the start.
0560: pos.setIndex(start);
0561: pos.setErrorIndex(start);
0562: }
0563: }
0564: return null;
0565: }
0566:
0567: /**
0568: * Parse a date/time string into an Object. This convenience method simply
0569: * calls parse(String, ParsePosition).
0570: *
0571: * @see #parse(String, ParsePosition)
0572: * @stable ICU 2.0
0573: */
0574: public Object parseObject(String source, ParsePosition pos) {
0575: return parse(source, pos);
0576: }
0577:
0578: /**
0579: * Constant for full style pattern.
0580: * @stable ICU 2.0
0581: */
0582: public static final int FULL = 0;
0583:
0584: /**
0585: * Constant for long style pattern.
0586: * @stable ICU 2.0
0587: */
0588: public static final int LONG = 1;
0589:
0590: /**
0591: * Constant for medium style pattern.
0592: * @stable ICU 2.0
0593: */
0594: public static final int MEDIUM = 2;
0595:
0596: /**
0597: * Constant for short style pattern.
0598: * @stable ICU 2.0
0599: */
0600: public static final int SHORT = 3;
0601:
0602: /**
0603: * Constant for default style pattern. Its value is MEDIUM.
0604: * @stable ICU 2.0
0605: */
0606: public static final int DEFAULT = MEDIUM;
0607:
0608: /**
0609: * Gets the time formatter with the default formatting style
0610: * for the default locale.
0611: * @return a time formatter.
0612: * @stable ICU 2.0
0613: */
0614: public final static DateFormat getTimeInstance() {
0615: return get(-1, DEFAULT, ULocale.getDefault());
0616: }
0617:
0618: /**
0619: * Gets the time formatter with the given formatting style
0620: * for the default locale.
0621: * @param style the given formatting style. For example,
0622: * SHORT for "h:mm a" in the US locale.
0623: * @return a time formatter.
0624: * @stable ICU 2.0
0625: */
0626: public final static DateFormat getTimeInstance(int style) {
0627: return get(-1, style, ULocale.getDefault());
0628: }
0629:
0630: /**
0631: * Gets the time formatter with the given formatting style
0632: * for the given locale.
0633: * @param style the given formatting style. For example,
0634: * SHORT for "h:mm a" in the US locale.
0635: * @param aLocale the given locale.
0636: * @return a time formatter.
0637: * @stable ICU 2.0
0638: */
0639: public final static DateFormat getTimeInstance(int style,
0640: Locale aLocale) {
0641: return get(-1, style, ULocale.forLocale(aLocale));
0642: }
0643:
0644: /**
0645: * Gets the time formatter with the given formatting style
0646: * for the given locale.
0647: * @param style the given formatting style. For example,
0648: * SHORT for "h:mm a" in the US locale.
0649: * @param locale the given ulocale.
0650: * @return a time formatter.
0651: * @draft ICU 3.2
0652: * @provisional This API might change or be removed in a future release.
0653: */
0654: public final static DateFormat getTimeInstance(int style,
0655: ULocale locale) {
0656: return get(-1, style, locale);
0657: }
0658:
0659: /**
0660: * Gets the date formatter with the default formatting style
0661: * for the default locale.
0662: * @return a date formatter.
0663: * @stable ICU 2.0
0664: */
0665: public final static DateFormat getDateInstance() {
0666: return get(DEFAULT, -1, ULocale.getDefault());
0667: }
0668:
0669: /**
0670: * Gets the date formatter with the given formatting style
0671: * for the default locale.
0672: * @param style the given formatting style. For example,
0673: * SHORT for "M/d/yy" in the US locale.
0674: * @return a date formatter.
0675: * @stable ICU 2.0
0676: */
0677: public final static DateFormat getDateInstance(int style) {
0678: return get(style, -1, ULocale.getDefault());
0679: }
0680:
0681: /**
0682: * Gets the date formatter with the given formatting style
0683: * for the given locale.
0684: * @param style the given formatting style. For example,
0685: * SHORT for "M/d/yy" in the US locale.
0686: * @param aLocale the given locale.
0687: * @return a date formatter.
0688: * @stable ICU 2.0
0689: */
0690: public final static DateFormat getDateInstance(int style,
0691: Locale aLocale) {
0692: return get(style, -1, ULocale.forLocale(aLocale));
0693: }
0694:
0695: /**
0696: * Gets the date formatter with the given formatting style
0697: * for the given locale.
0698: * @param style the given formatting style. For example,
0699: * SHORT for "M/d/yy" in the US locale.
0700: * @param locale the given ulocale.
0701: * @return a date formatter.
0702: * @draft ICU 3.2
0703: * @provisional This API might change or be removed in a future release.
0704: */
0705: public final static DateFormat getDateInstance(int style,
0706: ULocale locale) {
0707: return get(style, -1, locale);
0708: }
0709:
0710: /**
0711: * Gets the date/time formatter with the default formatting style
0712: * for the default locale.
0713: * @return a date/time formatter.
0714: * @stable ICU 2.0
0715: */
0716: public final static DateFormat getDateTimeInstance() {
0717: return get(DEFAULT, DEFAULT, ULocale.getDefault());
0718: }
0719:
0720: /**
0721: * Gets the date/time formatter with the given date and time
0722: * formatting styles for the default locale.
0723: * @param dateStyle the given date formatting style. For example,
0724: * SHORT for "M/d/yy" in the US locale.
0725: * @param timeStyle the given time formatting style. For example,
0726: * SHORT for "h:mm a" in the US locale.
0727: * @return a date/time formatter.
0728: * @stable ICU 2.0
0729: */
0730: public final static DateFormat getDateTimeInstance(int dateStyle,
0731: int timeStyle) {
0732: return get(dateStyle, timeStyle, ULocale.getDefault());
0733: }
0734:
0735: /**
0736: * Gets the date/time formatter with the given formatting styles
0737: * for the given locale.
0738: * @param dateStyle the given date formatting style.
0739: * @param timeStyle the given time formatting style.
0740: * @param aLocale the given locale.
0741: * @return a date/time formatter.
0742: * @stable ICU 2.0
0743: */
0744: public final static DateFormat getDateTimeInstance(int dateStyle,
0745: int timeStyle, Locale aLocale) {
0746: return get(dateStyle, timeStyle, ULocale.forLocale(aLocale));
0747: }
0748:
0749: /**
0750: * Gets the date/time formatter with the given formatting styles
0751: * for the given locale.
0752: * @param dateStyle the given date formatting style.
0753: * @param timeStyle the given time formatting style.
0754: * @param locale the given ulocale.
0755: * @return a date/time formatter.
0756: * @draft ICU 3.2
0757: * @provisional This API might change or be removed in a future release.
0758: */
0759: public final static DateFormat getDateTimeInstance(int dateStyle,
0760: int timeStyle, ULocale locale) {
0761: return get(dateStyle, timeStyle, locale);
0762: }
0763:
0764: /**
0765: * Get a default date/time formatter that uses the SHORT style for both the
0766: * date and the time.
0767: * @stable ICU 2.0
0768: */
0769: public final static DateFormat getInstance() {
0770: return getDateTimeInstance(SHORT, SHORT);
0771: }
0772:
0773: /**
0774: * Gets the set of locales for which DateFormats are installed.
0775: * @return the set of locales for which DateFormats are installed.
0776: * @stable ICU 2.0
0777: */
0778: public static Locale[] getAvailableLocales() {
0779: return ICUResourceBundle
0780: .getAvailableLocales(ICUResourceBundle.ICU_BASE_NAME);
0781: }
0782:
0783: /**
0784: * Gets the set of locales for which DateFormats are installed.
0785: * @return the set of locales for which DateFormats are installed.
0786: * @draft ICU 3.2
0787: * @provisional This API might change or be removed in a future release.
0788: */
0789: public static ULocale[] getAvailableULocales() {
0790: return ICUResourceBundle
0791: .getAvailableULocales(ICUResourceBundle.ICU_BASE_NAME);
0792: }
0793:
0794: /**
0795: * Set the calendar to be used by this date format. Initially, the default
0796: * calendar for the specified or default locale is used.
0797: * @param newCalendar the new Calendar to be used by the date format
0798: * @stable ICU 2.0
0799: */
0800: public void setCalendar(Calendar newCalendar) {
0801: this .calendar = newCalendar;
0802: }
0803:
0804: /**
0805: * Gets the calendar associated with this date/time formatter.
0806: * @return the calendar associated with this date/time formatter.
0807: * @stable ICU 2.0
0808: */
0809: public Calendar getCalendar() {
0810: return calendar;
0811: }
0812:
0813: /**
0814: * Allows you to set the number formatter.
0815: * @param newNumberFormat the given new NumberFormat.
0816: * @stable ICU 2.0
0817: */
0818: public void setNumberFormat(NumberFormat newNumberFormat) {
0819: this .numberFormat = newNumberFormat;
0820: /*In order to parse String like "11.10.2001" to DateTime correctly
0821: in Locale("fr","CH") [Richard/GCL]
0822: */
0823: this .numberFormat.setParseIntegerOnly(true);
0824: }
0825:
0826: /**
0827: * Gets the number formatter which this date/time formatter uses to
0828: * format and parse a time.
0829: * @return the number formatter which this date/time formatter uses.
0830: * @stable ICU 2.0
0831: */
0832: public NumberFormat getNumberFormat() {
0833: return numberFormat;
0834: }
0835:
0836: /**
0837: * Sets the time zone for the calendar of this DateFormat object.
0838: * @param zone the given new time zone.
0839: * @stable ICU 2.0
0840: */
0841: public void setTimeZone(TimeZone zone) {
0842: calendar.setTimeZone(zone);
0843: }
0844:
0845: /**
0846: * Gets the time zone.
0847: * @return the time zone associated with the calendar of DateFormat.
0848: * @stable ICU 2.0
0849: */
0850: public TimeZone getTimeZone() {
0851: return calendar.getTimeZone();
0852: }
0853:
0854: /**
0855: * Specify whether or not date/time parsing is to be lenient. With
0856: * lenient parsing, the parser may use heuristics to interpret inputs that
0857: * do not precisely match this object's format. With strict parsing,
0858: * inputs must match this object's format.
0859: * @param lenient when true, parsing is lenient
0860: * @see com.ibm.icu.util.Calendar#setLenient
0861: * @stable ICU 2.0
0862: */
0863: public void setLenient(boolean lenient) {
0864: calendar.setLenient(lenient);
0865: }
0866:
0867: /**
0868: * Tell whether date/time parsing is to be lenient.
0869: * @stable ICU 2.0
0870: */
0871: public boolean isLenient() {
0872: return calendar.isLenient();
0873: }
0874:
0875: /**
0876: * Overrides hashCode
0877: * @stable ICU 2.0
0878: */
0879: ///CLOVER:OFF
0880: // turn off code coverage since all subclasses override this
0881: public int hashCode() {
0882: return numberFormat.hashCode();
0883: // just enough fields for a reasonable distribution
0884: }
0885:
0886: ///CLOVER:ON
0887:
0888: /**
0889: * Overrides equals
0890: * @stable ICU 2.0
0891: */
0892: public boolean equals(Object obj) {
0893: if (this == obj)
0894: return true;
0895: if (obj == null || getClass() != obj.getClass())
0896: return false;
0897: DateFormat other = (DateFormat) obj;
0898: return (calendar.isEquivalentTo(other.calendar) && numberFormat
0899: .equals(other.numberFormat));
0900: }
0901:
0902: /**
0903: * Overrides Cloneable
0904: * @stable ICU 2.0
0905: */
0906: public Object clone() {
0907: DateFormat other = (DateFormat) super .clone();
0908: other.calendar = (Calendar) calendar.clone();
0909: other.numberFormat = (NumberFormat) numberFormat.clone();
0910: return other;
0911: }
0912:
0913: /**
0914: * Creates a DateFormat with the given time and/or date style in the given
0915: * locale.
0916: * @param dateStyle a value from 0 to 3 indicating the time format,
0917: * or -1 to indicate no date
0918: * @param timeStyle a value from 0 to 3 indicating the time format,
0919: * or -1 to indicate no time
0920: * @param loc the locale for the format
0921: */
0922: private static DateFormat get(int dateStyle, int timeStyle,
0923: ULocale loc) {
0924: if (timeStyle < -1 || timeStyle > 3) {
0925: throw new IllegalArgumentException("Illegal time style "
0926: + timeStyle);
0927: }
0928: if (dateStyle < -1 || dateStyle > 3) {
0929: throw new IllegalArgumentException("Illegal date style "
0930: + dateStyle);
0931: }
0932: try {
0933: Calendar cal = Calendar.getInstance(loc);
0934: DateFormat result = cal.getDateTimeFormat(dateStyle,
0935: timeStyle, loc);
0936: result.setLocale(cal.getLocale(ULocale.VALID_LOCALE), cal
0937: .getLocale(ULocale.ACTUAL_LOCALE));
0938: return result;
0939: } catch (MissingResourceException e) {
0940: ///CLOVER:OFF
0941: // coverage requires separate run with no data, so skip
0942: return new SimpleDateFormat("M/d/yy h:mm a");
0943: ///CLOVER:ON
0944: }
0945: }
0946:
0947: /**
0948: * Create a new date format.
0949: * @stable ICU 2.0
0950: */
0951: protected DateFormat() {
0952: }
0953:
0954: //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
0955:
0956: //-------------------------------------------------------------------------
0957: // Public static interface for creating custon DateFormats for different
0958: // types of Calendars.
0959: //-------------------------------------------------------------------------
0960:
0961: /**
0962: * Create a {@link DateFormat} object that can be used to format dates in
0963: * the calendar system specified by <code>cal</code>.
0964: * <p>
0965: * @param cal The calendar system for which a date format is desired.
0966: *
0967: * @param dateStyle The type of date format desired. This can be
0968: * {@link DateFormat#SHORT}, {@link DateFormat#MEDIUM},
0969: * etc.
0970: *
0971: * @param locale The locale for which the date format is desired.
0972: * @stable ICU 2.0
0973: */
0974: static final public DateFormat getDateInstance(Calendar cal,
0975: int dateStyle, Locale locale) {
0976: return getDateTimeInstance(cal, dateStyle, -1, ULocale
0977: .forLocale(locale));
0978: }
0979:
0980: /**
0981: * Create a {@link DateFormat} object that can be used to format dates in
0982: * the calendar system specified by <code>cal</code>.
0983: * <p>
0984: * @param cal The calendar system for which a date format is desired.
0985: *
0986: * @param dateStyle The type of date format desired. This can be
0987: * {@link DateFormat#SHORT}, {@link DateFormat#MEDIUM},
0988: * etc.
0989: *
0990: * @param locale The locale for which the date format is desired.
0991: * @draft ICU 3.2
0992: * @provisional This API might change or be removed in a future release.
0993: */
0994: static final public DateFormat getDateInstance(Calendar cal,
0995: int dateStyle, ULocale locale) {
0996: return getDateTimeInstance(cal, dateStyle, -1, locale);
0997: }
0998:
0999: /**
1000: * Create a {@link DateFormat} object that can be used to format times in
1001: * the calendar system specified by <code>cal</code>.
1002: * <p>
1003: * <b>Note:</b> When this functionality is moved into the core JDK, this method
1004: * will probably be replaced by a new overload of {@link DateFormat#getInstance}.
1005: * <p>
1006: * @param cal The calendar system for which a time format is desired.
1007: *
1008: * @param timeStyle The type of time format desired. This can be
1009: * {@link DateFormat#SHORT}, {@link DateFormat#MEDIUM},
1010: * etc.
1011: *
1012: * @param locale The locale for which the time format is desired.
1013: *
1014: * @see DateFormat#getTimeInstance
1015: * @stable ICU 2.0
1016: */
1017: static final public DateFormat getTimeInstance(Calendar cal,
1018: int timeStyle, Locale locale) {
1019: return getDateTimeInstance(cal, -1, timeStyle, ULocale
1020: .forLocale(locale));
1021: }
1022:
1023: /**
1024: * Create a {@link DateFormat} object that can be used to format times in
1025: * the calendar system specified by <code>cal</code>.
1026: * <p>
1027: * <b>Note:</b> When this functionality is moved into the core JDK, this method
1028: * will probably be replaced by a new overload of {@link DateFormat#getInstance}.
1029: * <p>
1030: * @param cal The calendar system for which a time format is desired.
1031: *
1032: * @param timeStyle The type of time format desired. This can be
1033: * {@link DateFormat#SHORT}, {@link DateFormat#MEDIUM},
1034: * etc.
1035: *
1036: * @param locale The locale for which the time format is desired.
1037: *
1038: * @see DateFormat#getTimeInstance
1039: * @draft ICU 3.2
1040: * @provisional This API might change or be removed in a future release.
1041: */
1042: static final public DateFormat getTimeInstance(Calendar cal,
1043: int timeStyle, ULocale locale) {
1044: return getDateTimeInstance(cal, -1, timeStyle, locale);
1045: }
1046:
1047: /**
1048: * Create a {@link DateFormat} object that can be used to format dates and times in
1049: * the calendar system specified by <code>cal</code>.
1050: * <p>
1051: * <b>Note:</b> When this functionality is moved into the core JDK, this method
1052: * will probably be replaced by a new overload of {@link DateFormat#getInstance}.
1053: * <p>
1054: * @param cal The calendar system for which a date/time format is desired.
1055: *
1056: * @param dateStyle The type of date format desired. This can be
1057: * {@link DateFormat#SHORT}, {@link DateFormat#MEDIUM},
1058: * etc.
1059: *
1060: * @param timeStyle The type of time format desired. This can be
1061: * {@link DateFormat#SHORT}, {@link DateFormat#MEDIUM},
1062: * etc.
1063: *
1064: * @param locale The locale for which the date/time format is desired.
1065: *
1066: * @see DateFormat#getDateTimeInstance
1067: * @stable ICU 2.0
1068: */
1069: static final public DateFormat getDateTimeInstance(Calendar cal,
1070: int dateStyle, int timeStyle, Locale locale) {
1071: return cal.getDateTimeFormat(dateStyle, timeStyle, ULocale
1072: .forLocale(locale));
1073: }
1074:
1075: /**
1076: * Create a {@link DateFormat} object that can be used to format dates and times in
1077: * the calendar system specified by <code>cal</code>.
1078: * <p>
1079: * <b>Note:</b> When this functionality is moved into the core JDK, this method
1080: * will probably be replaced by a new overload of {@link DateFormat#getInstance}.
1081: * <p>
1082: * @param cal The calendar system for which a date/time format is desired.
1083: *
1084: * @param dateStyle The type of date format desired. This can be
1085: * {@link DateFormat#SHORT}, {@link DateFormat#MEDIUM},
1086: * etc.
1087: *
1088: * @param timeStyle The type of time format desired. This can be
1089: * {@link DateFormat#SHORT}, {@link DateFormat#MEDIUM},
1090: * etc.
1091: *
1092: * @param locale The locale for which the date/time format is desired.
1093: *
1094: * @see DateFormat#getDateTimeInstance
1095: * @draft ICU 3.2
1096: * @provisional This API might change or be removed in a future release.
1097: */
1098: static final public DateFormat getDateTimeInstance(Calendar cal,
1099: int dateStyle, int timeStyle, ULocale locale) {
1100: return cal.getDateTimeFormat(dateStyle, timeStyle, locale);
1101: }
1102:
1103: /**
1104: * Convenience overload
1105: * @stable ICU 2.0
1106: */
1107: static final public DateFormat getInstance(Calendar cal,
1108: Locale locale) {
1109: return getDateTimeInstance(cal, SHORT, SHORT, ULocale
1110: .forLocale(locale));
1111: }
1112:
1113: /**
1114: * Convenience overload
1115: * @draft ICU 3.2
1116: * @provisional This API might change or be removed in a future release.
1117: */
1118: static final public DateFormat getInstance(Calendar cal,
1119: ULocale locale) {
1120: return getDateTimeInstance(cal, SHORT, SHORT, locale);
1121: }
1122:
1123: /**
1124: * Convenience overload
1125: * @stable ICU 2.0
1126: */
1127: static final public DateFormat getInstance(Calendar cal) {
1128: return getInstance(cal, ULocale.getDefault());
1129: }
1130:
1131: /**
1132: * Convenience overload
1133: * @stable ICU 2.0
1134: */
1135: static final public DateFormat getDateInstance(Calendar cal,
1136: int dateStyle) {
1137: return getDateInstance(cal, dateStyle, ULocale.getDefault());
1138: }
1139:
1140: /**
1141: * Convenience overload
1142: * @stable ICU 2.0
1143: */
1144: static final public DateFormat getTimeInstance(Calendar cal,
1145: int timeStyle) {
1146: return getTimeInstance(cal, timeStyle, ULocale.getDefault());
1147: }
1148:
1149: /**
1150: * Convenience overload
1151: * @stable ICU 2.0
1152: */
1153: static final public DateFormat getDateTimeInstance(Calendar cal,
1154: int dateStyle, int timeStyle) {
1155: return getDateTimeInstance(cal, dateStyle, timeStyle, ULocale
1156: .getDefault());
1157: }
1158: }
|