1 /* 2 * Copyright (C) 1996-2015, International Business Machines 3 * Corporation and others. All Rights Reserved. 4 */ 5 6 package com.ibm.icu.text; 7 8 import java.io.IOException; 9 import java.io.InvalidObjectException; 10 import java.io.ObjectInputStream; 11 import java.text.FieldPosition; 12 import java.text.Format; 13 import java.text.ParseException; 14 import java.text.ParsePosition; 15 import java.util.Date; 16 import java.util.EnumSet; 17 import java.util.HashMap; 18 import java.util.Locale; 19 import java.util.Map; 20 import java.util.MissingResourceException; 21 22 import com.ibm.icu.impl.ICUResourceBundle; 23 import com.ibm.icu.impl.RelativeDateFormat; 24 import com.ibm.icu.util.Calendar; 25 import com.ibm.icu.util.GregorianCalendar; 26 import com.ibm.icu.util.TimeZone; 27 import com.ibm.icu.util.ULocale; 28 import com.ibm.icu.util.ULocale.Category; 29 30 /** 31 * {@icuenhanced java.text.DateFormat}.{@icu _usage_} 32 * 33 * <p> 34 * DateFormat is an abstract class for date/time formatting subclasses which formats and parses dates or time in a 35 * language-independent manner. The date/time formatting subclass, such as SimpleDateFormat, allows for formatting 36 * (i.e., date -> text), parsing (text -> date), and normalization. The date is represented as a <code>Date</code> 37 * object or as the milliseconds since January 1, 1970, 00:00:00 GMT. 38 * 39 * <p> 40 * DateFormat helps you to format and parse dates for any locale. Your code can be completely independent of the locale 41 * conventions for months, days of the week, or even the calendar format: lunar vs. solar. It provides many class 42 * methods for obtaining default date/time formatters based on the default for a given locale and a number of formatting 43 * styles or arbitrary "skeletons". 44 * <ol> 45 * <li>The formatting styles include FULL, LONG, MEDIUM, and SHORT. More detail and examples of using these styles are 46 * provided in the method descriptions. 47 * <li>The formatting styles only cover a fraction of the necessary usage. You often need to have just certain 48 * combinations of fields, like Month and Year, but have it to be formatted appropriate to a given locale. This is done 49 * using the (misnamed) getPatternInstance() method, supplying a skeleton. There are a number of constants that have 50 * common pre-defined skeletons, such as {@link #MINUTE_SECOND} for something like "13:45" or {@link #YEAR_ABBR_MONTH} 51 * for something like "Sept 2012". 52 * </ol> 53 * 54 * <p> 55 * To format a date for the current Locale, use one of the static factory methods: 56 * 57 * <pre> 58 * myString = DateFormat.getDateInstance().format(myDate); 59 * myString = DateFormat.getPatternInstance(DateFormat.YEAR_ABBR_MONTH).format(myDate); 60 * </pre> 61 * <p> 62 * If you are formatting multiple numbers, it is more efficient to get the format and use it multiple times so that the 63 * system doesn't have to fetch the information about the local language and country conventions multiple times. 64 * 65 * <pre> 66 * DateFormat df = DateFormat.getDateInstance(); 67 * for (int i = 0; i < a.length; ++i) { 68 * output.println(df.format(myDate[i]) + "; "); 69 * } 70 * </pre> 71 * <p> 72 * To format a date for a different Locale, specify it in the call to getDateInstance(). 73 * 74 * <pre> 75 * DateFormat df = DateFormat.getDateInstance(DateFormat.LONG, Locale.FRANCE); 76 * </pre> 77 * <p> 78 * You can use a DateFormat to parse also. 79 * 80 * <pre> 81 * myDate = df.parse(myString); 82 * </pre> 83 * <p> 84 * There are many static factory methods available. Use getDateInstance to get the normal date format for that country. 85 * Use getTimeInstance to get the time format for that country. Use getDateTimeInstance to get a date and time format. 86 * You can pass in different options to these factory methods to control the length of the result; from SHORT to MEDIUM 87 * to LONG to FULL. The exact result depends on the locale, but generally: 88 * <ul> 89 * <li>SHORT is completely numeric, such as 12.13.52 or 3:30pm 90 * <li>MEDIUM is longer, such as Jan 12, 1952 91 * <li>LONG is longer, such as January 12, 1952 or 3:30:32pm 92 * <li>FULL is pretty completely specified, such as Tuesday, April 12, 1952 AD or 3:30:42pm PST. 93 * </ul> 94 * 95 * <p> 96 * Use getPatternInstance to format with a skeleton. Typically this is with a predefined skeleton, like 97 * {@link #YEAR_ABBR_MONTH} for something like "Sept 2012". If you don't want to use one of the predefined skeletons, 98 * you can supply your own. The skeletons are like the patterns in SimpleDateFormat, except they: 99 * <ol> 100 * <li>only keep the field pattern letter and ignore all other parts in a pattern, such as space, punctuation, and 101 * string literals. 102 * <li>are independent of the order of fields. 103 * <li>ignore certain differences in the field's pattern letter length: 104 * <ol> 105 * <li>For those non-digit calendar fields, the pattern letter length is important, such as MMM, MMMM, and MMMMM; E and 106 * EEEE, and the field's pattern letter length is honored. 107 * <li>For the digit calendar fields, such as M or MM, d or dd, yy or yyyy, the field pattern length is ignored and the 108 * best match, which is defined in date time patterns, will be returned without honor the field pattern letter length in 109 * skeleton. 110 * </ol> 111 * </ol> 112 * 113 * <p> 114 * You can also set the time zone on the format if you wish. If you want even more control over the format or parsing, 115 * (or want to give your users more control), you can try casting the DateFormat you get from the factory methods to a 116 * SimpleDateFormat. This will work for the majority of countries; just remember to put it in a try block in case you 117 * encounter an unusual one. 118 * 119 * <p> 120 * You can also use forms of the parse and format methods with ParsePosition and FieldPosition to allow you to 121 * <ul> 122 * <li>progressively parse through pieces of a string. 123 * <li>align any particular field, or find out where it is for selection on the screen. 124 * </ul> 125 * 126 * <h4>Synchronization</h4> 127 * 128 * Date formats are not synchronized. It is recommended to create separate format instances for each thread. If multiple 129 * threads access a format concurrently, it must be synchronized externally. 130 * 131 * @see UFormat 132 * @see NumberFormat 133 * @see SimpleDateFormat 134 * @see com.ibm.icu.util.Calendar 135 * @see com.ibm.icu.util.GregorianCalendar 136 * @see com.ibm.icu.util.TimeZone 137 * @author Mark Davis, Chen-Lieh Huang, Alan Liu 138 * @stable ICU 2.0 139 */ 140 public abstract class DateFormat extends UFormat { 141 142 /** 143 * The calendar that <code>DateFormat</code> uses to produce the time field 144 * values needed to implement date and time formatting. Subclasses should 145 * initialize this to a calendar appropriate for the locale associated with 146 * this <code>DateFormat</code>. 147 * @serial 148 * @stable ICU 2.0 149 */ 150 protected Calendar calendar; 151 152 /** 153 * The number formatter that <code>DateFormat</code> uses to format numbers 154 * in dates and times. Subclasses should initialize this to a number format 155 * appropriate for the locale associated with this <code>DateFormat</code>. 156 * @serial 157 * @stable ICU 2.0 158 */ 159 protected NumberFormat numberFormat; 160 161 /** 162 * FieldPosition selector for 'G' field alignment, 163 * corresponding to the {@link Calendar#ERA} field. 164 * @stable ICU 2.0 165 */ 166 public final static int ERA_FIELD = 0; 167 168 /** 169 * FieldPosition selector for 'y' field alignment, 170 * corresponding to the {@link Calendar#YEAR} field. 171 * @stable ICU 2.0 172 */ 173 public final static int YEAR_FIELD = 1; 174 175 /** 176 * FieldPosition selector for 'M' field alignment, 177 * corresponding to the {@link Calendar#MONTH} field. 178 * @stable ICU 2.0 179 */ 180 public final static int MONTH_FIELD = 2; 181 182 /** 183 * FieldPosition selector for 'd' field alignment, 184 * corresponding to the {@link Calendar#DATE} field. 185 * @stable ICU 2.0 186 */ 187 public final static int DATE_FIELD = 3; 188 189 /** 190 * FieldPosition selector for 'k' field alignment, 191 * corresponding to the {@link Calendar#HOUR_OF_DAY} field. 192 * HOUR_OF_DAY1_FIELD is used for the one-based 24-hour clock. 193 * For example, 23:59 + 01:00 results in 24:59. 194 * @stable ICU 2.0 195 */ 196 public final static int HOUR_OF_DAY1_FIELD = 4; 197 198 /** 199 * FieldPosition selector for 'H' field alignment, 200 * corresponding to the {@link Calendar#HOUR_OF_DAY} field. 201 * HOUR_OF_DAY0_FIELD is used for the zero-based 24-hour clock. 202 * For example, 23:59 + 01:00 results in 00:59. 203 * @stable ICU 2.0 204 */ 205 public final static int HOUR_OF_DAY0_FIELD = 5; 206 207 /** 208 * FieldPosition selector for 'm' field alignment, 209 * corresponding to the {@link Calendar#MINUTE} field. 210 * @stable ICU 2.0 211 */ 212 public final static int MINUTE_FIELD = 6; 213 214 /** 215 * FieldPosition selector for 's' field alignment, 216 * corresponding to the {@link Calendar#SECOND} field. 217 * @stable ICU 2.0 218 */ 219 public final static int SECOND_FIELD = 7; 220 221 /** 222 * {@icu} FieldPosition selector for 'S' field alignment, 223 * corresponding to the {@link Calendar#MILLISECOND} field. 224 * 225 * Note: Time formats that use 'S' can display a maximum of three 226 * significant digits for fractional seconds, corresponding to millisecond 227 * resolution and a fractional seconds sub-pattern of SSS. If the 228 * sub-pattern is S or SS, the fractional seconds value will be truncated 229 * (not rounded) to the number of display places specified. If the 230 * fractional seconds sub-pattern is longer than SSS, the additional 231 * display places will be filled with zeros. 232 * @stable ICU 3.0 233 */ 234 public final static int FRACTIONAL_SECOND_FIELD = 8; 235 236 /** 237 * Alias for FRACTIONAL_SECOND_FIELD. 238 * @stable ICU 3.0 239 */ 240 public final static int MILLISECOND_FIELD = FRACTIONAL_SECOND_FIELD; 241 242 /** 243 * FieldPosition selector for 'E' field alignment, 244 * corresponding to the {@link Calendar#DAY_OF_WEEK} field. 245 * @stable ICU 2.0 246 */ 247 public final static int DAY_OF_WEEK_FIELD = 9; 248 249 /** 250 * FieldPosition selector for 'D' field alignment, 251 * corresponding to the {@link Calendar#DAY_OF_YEAR} field. 252 * @stable ICU 2.0 253 */ 254 public final static int DAY_OF_YEAR_FIELD = 10; 255 256 /** 257 * FieldPosition selector for 'F' field alignment, 258 * corresponding to the {@link Calendar#DAY_OF_WEEK_IN_MONTH} field. 259 * @stable ICU 2.0 260 */ 261 public final static int DAY_OF_WEEK_IN_MONTH_FIELD = 11; 262 263 /** 264 * FieldPosition selector for 'w' field alignment, 265 * corresponding to the {@link Calendar#WEEK_OF_YEAR} field. 266 * @stable ICU 2.0 267 */ 268 public final static int WEEK_OF_YEAR_FIELD = 12; 269 270 /** 271 * FieldPosition selector for 'W' field alignment, 272 * corresponding to the {@link Calendar#WEEK_OF_MONTH} field. 273 * @stable ICU 2.0 274 */ 275 public final static int WEEK_OF_MONTH_FIELD = 13; 276 277 /** 278 * FieldPosition selector for 'a' field alignment, 279 * corresponding to the {@link Calendar#AM_PM} field. 280 * @stable ICU 2.0 281 */ 282 public final static int AM_PM_FIELD = 14; 283 284 /** 285 * FieldPosition selector for 'h' field alignment, 286 * corresponding to the {@link Calendar#HOUR} field. 287 * HOUR1_FIELD is used for the one-based 12-hour clock. 288 * For example, 11:30 PM + 1 hour results in 12:30 AM. 289 * @stable ICU 2.0 290 */ 291 public final static int HOUR1_FIELD = 15; 292 293 /** 294 * FieldPosition selector for 'K' field alignment, 295 * corresponding to the {@link Calendar#HOUR} field. 296 * HOUR0_FIELD is used for the zero-based 12-hour clock. 297 * For example, 11:30 PM + 1 hour results in 00:30 AM. 298 * @stable ICU 2.0 299 */ 300 public final static int HOUR0_FIELD = 16; 301 302 /** 303 * FieldPosition selector for 'z' field alignment, 304 * corresponding to the {@link Calendar#ZONE_OFFSET} and 305 * {@link Calendar#DST_OFFSET} fields. 306 * @stable ICU 2.0 307 */ 308 public final static int TIMEZONE_FIELD = 17; 309 310 /** 311 * {@icu} FieldPosition selector for 'Y' field alignment, 312 * corresponding to the {@link Calendar#YEAR_WOY} field. 313 * @stable ICU 3.0 314 */ 315 public final static int YEAR_WOY_FIELD = 18; 316 317 /** 318 * {@icu} FieldPosition selector for 'e' field alignment, 319 * corresponding to the {@link Calendar#DOW_LOCAL} field. 320 * @stable ICU 3.0 321 */ 322 public final static int DOW_LOCAL_FIELD = 19; 323 324 /** 325 * {@icu} FieldPosition selector for 'u' field alignment, 326 * corresponding to the {@link Calendar#EXTENDED_YEAR} field. 327 * @stable ICU 3.0 328 */ 329 public final static int EXTENDED_YEAR_FIELD = 20; 330 331 /** 332 * {@icu} FieldPosition selector for 'g' field alignment, 333 * corresponding to the {@link Calendar#JULIAN_DAY} field. 334 * @stable ICU 3.0 335 */ 336 public final static int JULIAN_DAY_FIELD = 21; 337 338 /** 339 * {@icu} FieldPosition selector for 'A' field alignment, 340 * corresponding to the {@link Calendar#MILLISECONDS_IN_DAY} field. 341 * @stable ICU 3.0 342 */ 343 public final static int MILLISECONDS_IN_DAY_FIELD = 22; 344 345 /** 346 * {@icu} FieldPosition selector for 'Z' field alignment, 347 * corresponding to the {@link Calendar#ZONE_OFFSET} and 348 * {@link Calendar#DST_OFFSET} fields. 349 * @stable ICU 3.0 350 */ 351 public final static int TIMEZONE_RFC_FIELD = 23; 352 353 /** 354 * {@icu} FieldPosition selector for 'v' field alignment, 355 * corresponding to the {@link Calendar#ZONE_OFFSET} and 356 * {@link Calendar#DST_OFFSET} fields. This displays the generic zone 357 * name, if available. 358 * @stable ICU 3.4 359 */ 360 public final static int TIMEZONE_GENERIC_FIELD = 24; 361 362 /** 363 * {@icu} FieldPosition selector for 'c' field alignment, 364 * corresponding to the {@link Calendar#DAY_OF_WEEK} field. 365 * This displays the stand alone day name, if available. 366 * @stable ICU 3.4 367 */ 368 public final static int STANDALONE_DAY_FIELD = 25; 369 370 /** 371 * {@icu} FieldPosition selector for 'L' field alignment, 372 * corresponding to the {@link Calendar#MONTH} field. 373 * This displays the stand alone month name, if available. 374 * @stable ICU 3.4 375 */ 376 public final static int STANDALONE_MONTH_FIELD = 26; 377 378 /** 379 * {@icu} FieldPosition selector for 'Q' field alignment, 380 * corresponding to the {@link Calendar#MONTH} field. 381 * This displays the quarter. 382 * @stable ICU 3.6 383 */ 384 public final static int QUARTER_FIELD = 27; 385 386 /** 387 * {@icu} FieldPosition selector for 'q' field alignment, 388 * corresponding to the {@link Calendar#MONTH} field. 389 * This displays the stand alone quarter, if available. 390 * @stable ICU 3.6 391 */ 392 public final static int STANDALONE_QUARTER_FIELD = 28; 393 394 /** 395 * {@icu} FieldPosition selector for 'V' field alignment, 396 * corresponding to the {@link Calendar#ZONE_OFFSET} and 397 * {@link Calendar#DST_OFFSET} fields. This displays the fallback timezone 398 * name when VVVV is specified, and the short standard or daylight 399 * timezone name ignoring commonlyUsed when a single V is specified. 400 * @stable ICU 3.8 401 */ 402 public final static int TIMEZONE_SPECIAL_FIELD = 29; 403 404 /** 405 * {@icu} FieldPosition selector for 'U' field alignment, 406 * corresponding to the {@link Calendar#YEAR} field. 407 * This displays the cyclic year name, if available. 408 * @stable ICU 49 409 */ 410 public final static int YEAR_NAME_FIELD = 30; 411 412 /** 413 * {@icu} FieldPosition selector for 'O' field alignment, 414 * corresponding to the {@link Calendar#ZONE_OFFSET} and 415 * {@link Calendar#DST_OFFSET} fields. This displays the 416 * localized GMT format. 417 * @stable ICU 51 418 */ 419 public final static int TIMEZONE_LOCALIZED_GMT_OFFSET_FIELD = 31; 420 421 /** 422 * {@icu} FieldPosition selector for 'X' field alignment, 423 * corresponding to the {@link Calendar#ZONE_OFFSET} and 424 * {@link Calendar#DST_OFFSET} fields. This displays the 425 * ISO 8601 local time offset format or UTC indicator ("Z"). 426 * @stable ICU 51 427 */ 428 public final static int TIMEZONE_ISO_FIELD = 32; 429 430 /** 431 * {@icu} FieldPosition selector for 'x' field alignment, 432 * corresponding to the {@link Calendar#ZONE_OFFSET} and 433 * {@link Calendar#DST_OFFSET} fields. This displays the 434 * ISO 8601 local time offset format. 435 * @stable ICU 51 436 */ 437 public final static int TIMEZONE_ISO_LOCAL_FIELD = 33; 438 439 /** 440 * {@icu} FieldPosition selector for 'r' field alignment, 441 * corresponding to the {@link Calendar#EXTENDED_YEAR} field 442 * of the *related* calendar which may be different than the 443 * one used by the DateFormat. 444 * @internal 445 * @deprecated This API is ICU internal only. 446 */ 447 @Deprecated 448 final static int RELATED_YEAR = 34; 449 450 /** 451 * {@icu} FieldPosition selector for ':' field alignment, 452 * no related Calendar field. 453 * @draft ICU 55 454 * @provisional This API might change or be removed in a future release. 455 */ 456 public final static int TIME_SEPARATOR = 35; 457 458 /** 459 * {@icu} Number of FieldPosition selectors for DateFormat. 460 * Valid selectors range from 0 to FIELD_COUNT-1. 461 * @stable ICU 3.0 462 */ 463 464 public final static int FIELD_COUNT = 36; // must == DateFormatSymbols.patternChars.length() 465 466 467 /** 468 * boolean attributes 469 * 470 * @stable ICU 53 471 */ 472 public enum BooleanAttribute { 473 /** 474 * indicates whitespace tolerance. Also included is trailing dot tolerance. 475 * @stable ICU 53 476 */ 477 PARSE_ALLOW_WHITESPACE, 478 /** 479 * indicates tolerance of numeric data when String data may be assumed. 480 * e.g. YEAR_NAME_FIELD 481 * @stable ICU 53 482 */ 483 PARSE_ALLOW_NUMERIC, 484 /** 485 * indicates tolerance of pattern mismatch between input data and specified format pattern. 486 * e.g. accepting "September" for a month pattern of MMM ("Sep") 487 * @draft ICU 53 488 * @provisional This API might change or be removed in a future release. 489 */ 490 PARSE_MULTIPLE_PATTERNS_FOR_MATCH, 491 /** 492 * indicates tolerance of a partial literal match 493 * @draft ICU 53 494 * @provisional This API might change or be removed in a future release. 495 */ 496 PARSE_PARTIAL_MATCH 497 }; 498 499 /** 500 * boolean attributes for this instance. Inclusion in this is indicates a true condition. 501 */ 502 private EnumSet<BooleanAttribute> booleanAttributes = EnumSet.allOf(BooleanAttribute.class); 503 504 /* 505 * Capitalization setting, hoisted to DateFormat ICU 53 506 * Note that SimpleDateFormat serialization may call getContext/setContext to read/write 507 * this for compatibility with serialization for its old copy of capitalizationSetting. 508 * @serial 509 */ 510 private DisplayContext capitalizationSetting = DisplayContext.CAPITALIZATION_NONE; 511 512 static final int currentSerialVersion = 1; 513 514 /** 515 * Describes the version of <code>DateFormat</code> present on the stream. 516 * Possible values are: 517 * <ul> 518 * <li><b>0</b> (or uninitialized): the pre-ICU-53 version 519 * 520 * <li><b>1</b>: ICU 53, adds serialVersionOnStream and capitalizationSetting 521 * </ul> 522 * When streaming out a <code>DateFormat</code>, the most recent format 523 * (corresponding to the highest allowable <code>serialVersionOnStream</code>) 524 * is always written. 525 * 526 * @serial 527 */ 528 private int serialVersionOnStream = currentSerialVersion; 529 530 // Proclaim serial compatibility with 1.1 FCS 531 private static final long serialVersionUID = 7218322306649953788L; 532 533 /** 534 * Formats a time object into a time string. Examples of time objects 535 * are a time value expressed in milliseconds and a Date object. 536 * @param obj must be a Number or a Date or a Calendar. 537 * @param toAppendTo the string buffer for the returning time string. 538 * @return the formatted time string. 539 * @param fieldPosition keeps track of the position of the field 540 * within the returned string. 541 * On input: an alignment field, 542 * if desired. On output: the offsets of the alignment field. For 543 * example, given a time text "1996.07.10 AD at 15:08:56 PDT", 544 * if the given fieldPosition is DateFormat.YEAR_FIELD, the 545 * begin index and end index of fieldPosition will be set to 546 * 0 and 4, respectively. 547 * Notice that if the same time field appears 548 * more than once in a pattern, the fieldPosition will be set for the first 549 * occurrence of that time field. For instance, formatting a Date to 550 * the time string "1 PM PDT (Pacific Daylight Time)" using the pattern 551 * "h a z (zzzz)" and the alignment field DateFormat.TIMEZONE_FIELD, 552 * the begin index and end index of fieldPosition will be set to 553 * 5 and 8, respectively, for the first occurrence of the timezone 554 * pattern character 'z'. 555 * @see java.text.Format 556 * @stable ICU 2.0 557 */ format(Object obj, StringBuffer toAppendTo, FieldPosition fieldPosition)558 public final StringBuffer format(Object obj, StringBuffer toAppendTo, 559 FieldPosition fieldPosition) 560 { 561 if (obj instanceof Calendar) 562 return format( (Calendar)obj, toAppendTo, fieldPosition ); 563 else if (obj instanceof Date) 564 return format( (Date)obj, toAppendTo, fieldPosition ); 565 else if (obj instanceof Number) 566 return format( new Date(((Number)obj).longValue()), 567 toAppendTo, fieldPosition ); 568 else 569 throw new IllegalArgumentException("Cannot format given Object (" + 570 obj.getClass().getName() + ") as a Date"); 571 } 572 573 /** 574 * Formats a date into a date/time string. 575 * @param cal a Calendar set to the date and time to be formatted 576 * into a date/time string. When the calendar type is different from 577 * the internal calendar held by this DateFormat instance, the date 578 * and the time zone will be inherited from the input calendar, but 579 * other calendar field values will be calculated by the internal calendar. 580 * @param toAppendTo the string buffer for the returning date/time string. 581 * @param fieldPosition keeps track of the position of the field 582 * within the returned string. 583 * On input: an alignment field, 584 * if desired. On output: the offsets of the alignment field. For 585 * example, given a time text "1996.07.10 AD at 15:08:56 PDT", 586 * if the given fieldPosition is DateFormat.YEAR_FIELD, the 587 * begin index and end index of fieldPosition will be set to 588 * 0 and 4, respectively. 589 * Notice that if the same time field appears 590 * more than once in a pattern, the fieldPosition will be set for the first 591 * occurrence of that time field. For instance, formatting a Date to 592 * the time string "1 PM PDT (Pacific Daylight Time)" using the pattern 593 * "h a z (zzzz)" and the alignment field DateFormat.TIMEZONE_FIELD, 594 * the begin index and end index of fieldPosition will be set to 595 * 5 and 8, respectively, for the first occurrence of the timezone 596 * pattern character 'z'. 597 * @return the formatted date/time string. 598 * @stable ICU 2.0 599 */ format(Calendar cal, StringBuffer toAppendTo, FieldPosition fieldPosition)600 public abstract StringBuffer format(Calendar cal, StringBuffer toAppendTo, 601 FieldPosition fieldPosition); 602 603 /** 604 * Formats a Date into a date/time string. 605 * @param date a Date to be formatted into a date/time string. 606 * @param toAppendTo the string buffer for the returning date/time string. 607 * @param fieldPosition keeps track of the position of the field 608 * within the returned string. 609 * On input: an alignment field, 610 * if desired. On output: the offsets of the alignment field. For 611 * example, given a time text "1996.07.10 AD at 15:08:56 PDT", 612 * if the given fieldPosition is DateFormat.YEAR_FIELD, the 613 * begin index and end index of fieldPosition will be set to 614 * 0 and 4, respectively. 615 * Notice that if the same time field appears 616 * more than once in a pattern, the fieldPosition will be set for the first 617 * occurrence of that time field. For instance, formatting a Date to 618 * the time string "1 PM PDT (Pacific Daylight Time)" using the pattern 619 * "h a z (zzzz)" and the alignment field DateFormat.TIMEZONE_FIELD, 620 * the begin index and end index of fieldPosition will be set to 621 * 5 and 8, respectively, for the first occurrence of the timezone 622 * pattern character 'z'. 623 * @return the formatted date/time string. 624 * @stable ICU 2.0 625 */ format(Date date, StringBuffer toAppendTo, FieldPosition fieldPosition)626 public StringBuffer format(Date date, StringBuffer toAppendTo, 627 FieldPosition fieldPosition) { 628 // Use our Calendar object 629 calendar.setTime(date); 630 return format(calendar, toAppendTo, fieldPosition); 631 } 632 633 /** 634 * Formats a Date into a date/time string. 635 * @param date the time value to be formatted into a time string. 636 * @return the formatted time string. 637 * @stable ICU 2.0 638 */ format(Date date)639 public final String format(Date date) 640 { 641 return format(date, new StringBuffer(64),new FieldPosition(0)).toString(); 642 } 643 644 /** 645 * Parses a date/time string. For example, a time text "07/10/96 4:5 PM, PDT" 646 * will be parsed into a Date that is equivalent to Date(837039928046). 647 * Parsing begins at the beginning of the string and proceeds as far as 648 * possible. Assuming no parse errors were encountered, this function 649 * doesn't return any information about how much of the string was consumed 650 * by the parsing. If you need that information, use a version of 651 * parse() that takes a ParsePosition. 652 * 653 * <p> By default, parsing is lenient: If the input is not in the form used 654 * by this object's format method but can still be parsed as a date, then 655 * the parse succeeds. Clients may insist on strict adherence to the 656 * format by calling setLenient(false). 657 * 658 * <p> Note that the normal date formats associated with some calendars - such 659 * as the Chinese lunar calendar - do not specify enough fields to enable 660 * dates to be parsed unambiguously. In the case of the Chinese lunar 661 * calendar, while the year within the current 60-year cycle is specified, 662 * the number of such cycles since the start date of the calendar (in the 663 * ERA field of the Calendar object) is not normally part of the format, 664 * and parsing may assume the wrong era. For cases such as this it is 665 * recommended that clients parse using the parse method that takes a Calendar 666 * with the Calendar passed in set to the current date, or to a date 667 * within the era/cycle that should be assumed if absent in the format. 668 * 669 * @param text The date/time string to be parsed 670 * 671 * @return A Date, or null if the input could not be parsed 672 * 673 * @exception ParseException If the given string cannot be parsed as a date. 674 * 675 * @see #parse(String, ParsePosition) 676 * @stable ICU 2.0 677 */ parse(String text)678 public Date parse(String text) throws ParseException 679 { 680 ParsePosition pos = new ParsePosition(0); 681 Date result = parse(text, pos); 682 if (pos.getIndex() == 0) // ICU4J 683 throw new ParseException("Unparseable date: \"" + text + "\"" , 684 pos.getErrorIndex()); // ICU4J 685 return result; 686 } 687 688 /** 689 * Parses a date/time string according to the given parse position. 690 * For example, a time text "07/10/96 4:5 PM, PDT" will be parsed 691 * into a Calendar that is equivalent to Date(837039928046). Before 692 * calling this method the caller should initialize the calendar 693 * in one of two ways (unless existing field information is to be kept): 694 * (1) clear the calendar, or (2) set the calendar to the current date 695 * (or to any date whose fields should be used to supply values that 696 * are missing in the parsed date). For example, Chinese calendar dates 697 * do not normally provide an era/cycle; in this case the calendar that 698 * is passed in should be set to a date within the era that should be 699 * assumed, normally the current era. 700 * 701 * <p> By default, parsing is lenient: If the input is not in the form used 702 * by this object's format method but can still be parsed as a date, then 703 * the parse succeeds. Clients may insist on strict adherence to the 704 * format by calling setLenient(false). 705 * 706 * @see #setLenient(boolean) 707 * 708 * @param text The date/time string to be parsed 709 * 710 * @param cal The calendar set on input to the date and time to be used 711 * for missing values in the date/time string being parsed, 712 * and set on output to the parsed date/time. In general, this 713 * should be initialized before calling this method - either 714 * cleared or set to the current date, depending on desired 715 * behavior. If this parse fails, the calendar may still 716 * have been modified. When the calendar type is different 717 * from the internal calendar held by this DateFormat 718 * instance, calendar field values will be parsed based 719 * on the internal calendar initialized with the time and 720 * the time zone taken from this calendar, then the 721 * parse result (time in milliseconds and time zone) will 722 * be set back to this calendar. 723 * 724 * @param pos On input, the position at which to start parsing; on 725 * output, the position at which parsing terminated, or the 726 * start position if the parse failed. 727 * @stable ICU 2.0 728 */ parse(String text, Calendar cal, ParsePosition pos)729 public abstract void parse(String text, Calendar cal, ParsePosition pos); 730 731 /** 732 * Parses a date/time string according to the given parse position. For 733 * example, a time text "07/10/96 4:5 PM, PDT" will be parsed into a Date 734 * that is equivalent to Date(837039928046). 735 * 736 * <p> By default, parsing is lenient: If the input is not in the form used 737 * by this object's format method but can still be parsed as a date, then 738 * the parse succeeds. Clients may insist on strict adherence to the 739 * format by calling setLenient(false). 740 * 741 * <p> Note that the normal date formats associated with some calendars - such 742 * as the Chinese lunar calendar - do not specify enough fields to enable 743 * dates to be parsed unambiguously. In the case of the Chinese lunar 744 * calendar, while the year within the current 60-year cycle is specified, 745 * the number of such cycles since the start date of the calendar (in the 746 * ERA field of the Calendar object) is not normally part of the format, 747 * and parsing may assume the wrong era. For cases such as this it is 748 * recommended that clients parse using the parse method that takes a Calendar 749 * with the Calendar passed in set to the current date, or to a date 750 * within the era/cycle that should be assumed if absent in the format. 751 * 752 * @see #setLenient(boolean) 753 * 754 * @param text The date/time string to be parsed 755 * 756 * @param pos On input, the position at which to start parsing; on 757 * output, the position at which parsing terminated, or the 758 * start position if the parse failed. 759 * 760 * @return A Date, or null if the input could not be parsed 761 * @stable ICU 2.0 762 */ parse(String text, ParsePosition pos)763 public Date parse(String text, ParsePosition pos) { 764 Date result = null; 765 int start = pos.getIndex(); 766 TimeZone tzsav = calendar.getTimeZone(); 767 calendar.clear(); 768 parse(text, calendar, pos); 769 if (pos.getIndex() != start) { 770 try { 771 result = calendar.getTime(); 772 } catch (IllegalArgumentException e) { 773 // This occurs if the calendar is non-lenient and there is 774 // an out-of-range field. We don't know which field was 775 // illegal so we set the error index to the start. 776 pos.setIndex(start); 777 pos.setErrorIndex(start); 778 } 779 } 780 // Restore TimeZone 781 calendar.setTimeZone(tzsav); 782 return result; 783 } 784 785 /** 786 * Parses a date/time string into an Object. This convenience method simply 787 * calls parse(String, ParsePosition). 788 * 789 * @see #parse(String, ParsePosition) 790 * @stable ICU 2.0 791 */ parseObject(String source, ParsePosition pos)792 public Object parseObject (String source, ParsePosition pos) 793 { 794 return parse(source, pos); 795 } 796 797 /** 798 * {@icu} Constant for empty style pattern. 799 * @stable ICU 3.8 800 */ 801 public static final int NONE = -1; 802 803 /** 804 * Constant for full style pattern. 805 * @stable ICU 2.0 806 */ 807 public static final int FULL = 0; 808 809 /** 810 * Constant for long style pattern. 811 * @stable ICU 2.0 812 */ 813 public static final int LONG = 1; 814 815 /** 816 * Constant for medium style pattern. 817 * @stable ICU 2.0 818 */ 819 public static final int MEDIUM = 2; 820 821 /** 822 * Constant for short style pattern. 823 * @stable ICU 2.0 824 */ 825 public static final int SHORT = 3; 826 827 /** 828 * Constant for default style pattern. Its value is MEDIUM. 829 * @stable ICU 2.0 830 */ 831 public static final int DEFAULT = MEDIUM; 832 833 /** 834 * {@icu} Constant for relative style mask. 835 * @stable ICU 3.8 836 */ 837 public static final int RELATIVE = (1 << 7); 838 839 /** 840 * {@icu} Constant for relative full style pattern. 841 * @stable ICU 3.8 842 */ 843 public static final int RELATIVE_FULL = RELATIVE | FULL; 844 845 /** 846 * {@icu} Constant for relative style pattern. 847 * @stable ICU 3.8 848 */ 849 public static final int RELATIVE_LONG = RELATIVE | LONG; 850 851 /** 852 * {@icu} Constant for relative style pattern. 853 * @stable ICU 3.8 854 */ 855 public static final int RELATIVE_MEDIUM = RELATIVE | MEDIUM; 856 857 /** 858 * {@icu} Constant for relative style pattern. 859 * @stable ICU 3.8 860 */ 861 public static final int RELATIVE_SHORT = RELATIVE | SHORT; 862 863 /** 864 * {@icu} Constant for relative default style pattern. 865 * @stable ICU 3.8 866 */ 867 public static final int RELATIVE_DEFAULT = RELATIVE | DEFAULT; 868 869 /* 870 * DATES 871 */ 872 873 /** 874 * {@icu} Constant for date skeleton with year. 875 * @stable ICU 4.0 876 */ 877 public static final String YEAR = "y"; 878 879 /** 880 * {@icu} Constant for date skeleton with quarter. 881 * @stable ICU 50 882 */ 883 public static final String QUARTER = "QQQQ"; 884 885 /** 886 * {@icu} Constant for date skeleton with abbreviated quarter. 887 * @stable ICU 50 888 */ 889 public static final String ABBR_QUARTER = "QQQ"; 890 891 /** 892 * {@icu} Constant for date skeleton with year and quarter. 893 * @stable ICU 4.0 894 */ 895 public static final String YEAR_QUARTER = "yQQQQ"; 896 897 /** 898 * {@icu} Constant for date skeleton with year and abbreviated quarter. 899 * @stable ICU 4.0 900 */ 901 public static final String YEAR_ABBR_QUARTER = "yQQQ"; 902 903 /** 904 * {@icu} Constant for date skeleton with month. 905 * @stable ICU 4.0 906 */ 907 public static final String MONTH = "MMMM"; 908 909 /** 910 * {@icu} Constant for date skeleton with abbreviated month. 911 * @stable ICU 4.0 912 */ 913 public static final String ABBR_MONTH = "MMM"; 914 915 /** 916 * {@icu} Constant for date skeleton with numeric month. 917 * @stable ICU 4.0 918 */ 919 public static final String NUM_MONTH = "M"; 920 921 /** 922 * {@icu} Constant for date skeleton with year and month. 923 * @stable ICU 4.0 924 */ 925 public static final String YEAR_MONTH = "yMMMM"; 926 927 /** 928 * {@icu} Constant for date skeleton with year and abbreviated month. 929 * @stable ICU 4.0 930 */ 931 public static final String YEAR_ABBR_MONTH = "yMMM"; 932 933 /** 934 * {@icu} Constant for date skeleton with year and numeric month. 935 * @stable ICU 4.0 936 */ 937 public static final String YEAR_NUM_MONTH = "yM"; 938 939 /** 940 * {@icu} Constant for date skeleton with day. 941 * @stable ICU 4.0 942 */ 943 public static final String DAY = "d"; 944 945 /** 946 * {@icu} Constant for date skeleton with year, month, and day. 947 * Used in combinations date + time, date + time + zone, or time + zone. 948 * @stable ICU 4.0 949 */ 950 public static final String YEAR_MONTH_DAY = "yMMMMd"; 951 952 /** 953 * {@icu} Constant for date skeleton with year, abbreviated month, and day. 954 * Used in combinations date + time, date + time + zone, or time + zone. 955 * @stable ICU 4.0 956 */ 957 public static final String YEAR_ABBR_MONTH_DAY = "yMMMd"; 958 959 /** 960 * {@icu} Constant for date skeleton with year, numeric month, and day. 961 * Used in combinations date + time, date + time + zone, or time + zone. 962 * @stable ICU 4.0 963 */ 964 public static final String YEAR_NUM_MONTH_DAY = "yMd"; 965 966 /** 967 * {@icu} Constant for date skeleton with weekday. 968 * @stable ICU 50 969 */ 970 public static final String WEEKDAY = "EEEE"; 971 972 /** 973 * {@icu} Constant for date skeleton with abbreviated weekday. 974 * @stable ICU 50 975 */ 976 public static final String ABBR_WEEKDAY = "E"; 977 978 /** 979 * {@icu} Constant for date skeleton with year, month, weekday, and day. 980 * Used in combinations date + time, date + time + zone, or time + zone. 981 * @stable ICU 4.0 982 */ 983 public static final String YEAR_MONTH_WEEKDAY_DAY = "yMMMMEEEEd"; 984 985 /** 986 * {@icu} Constant for date skeleton with year, abbreviated month, weekday, and day. 987 * Used in combinations date + time, date + time + zone, or time + zone. 988 * @stable ICU 4.0 989 */ 990 public static final String YEAR_ABBR_MONTH_WEEKDAY_DAY = "yMMMEd"; 991 992 /** 993 * {@icu} Constant for date skeleton with year, numeric month, weekday, and day. 994 * Used in combinations date + time, date + time + zone, or time + zone. 995 * @stable ICU 4.0 996 */ 997 public static final String YEAR_NUM_MONTH_WEEKDAY_DAY = "yMEd"; 998 999 /** 1000 * {@icu} Constant for date skeleton with long month and day. 1001 * Used in combinations date + time, date + time + zone, or time + zone. 1002 * @stable ICU 4.0 1003 */ 1004 public static final String MONTH_DAY = "MMMMd"; 1005 1006 /** 1007 * {@icu} Constant for date skeleton with abbreviated month and day. 1008 * Used in combinations date + time, date + time + zone, or time + zone. 1009 * @stable ICU 4.0 1010 */ 1011 public static final String ABBR_MONTH_DAY = "MMMd"; 1012 1013 /** 1014 * {@icu} Constant for date skeleton with numeric month and day. 1015 * Used in combinations date + time, date + time + zone, or time + zone. 1016 * @stable ICU 4.0 1017 */ 1018 public static final String NUM_MONTH_DAY = "Md"; 1019 1020 /** 1021 * {@icu} Constant for date skeleton with month, weekday, and day. 1022 * Used in combinations date + time, date + time + zone, or time + zone. 1023 * @stable ICU 4.0 1024 */ 1025 public static final String MONTH_WEEKDAY_DAY = "MMMMEEEEd"; 1026 1027 /** 1028 * {@icu} Constant for date skeleton with abbreviated month, weekday, and day. 1029 * Used in combinations date + time, date + time + zone, or time + zone. 1030 * @stable ICU 4.0 1031 */ 1032 public static final String ABBR_MONTH_WEEKDAY_DAY = "MMMEd"; 1033 1034 /** 1035 * {@icu} Constant for date skeleton with numeric month, weekday, and day. 1036 * Used in combinations date + time, date + time + zone, or time + zone. 1037 * @stable ICU 4.0 1038 */ 1039 public static final String NUM_MONTH_WEEKDAY_DAY = "MEd"; 1040 1041 /* 1042 * TIMES 1043 */ 1044 1045 /** 1046 * {@icu} Constant for date skeleton with hour, with the locale's preferred hour format (12 or 24). 1047 * @stable ICU 4.0 1048 */ 1049 public static final String HOUR = "j"; 1050 1051 /** 1052 * {@icu} Constant for date skeleton with hour in 24-hour presentation. 1053 * @stable ICU 50 1054 */ 1055 public static final String HOUR24 = "H"; 1056 1057 /** 1058 * {@icu} Constant for date skeleton with minute. 1059 * @stable ICU 50 1060 */ 1061 public static final String MINUTE = "m"; 1062 1063 /** 1064 * {@icu} Constant for date skeleton with hour and minute, with the locale's preferred hour format (12 or 24). 1065 * Used in combinations date + time, date + time + zone, or time + zone. 1066 * @stable ICU 4.0 1067 */ 1068 public static final String HOUR_MINUTE = "jm"; 1069 1070 /** 1071 * {@icu} Constant for date skeleton with hour and minute in 24-hour presentation. 1072 * Used in combinations date + time, date + time + zone, or time + zone. 1073 * @stable ICU 4.0 1074 */ 1075 public static final String HOUR24_MINUTE = "Hm"; 1076 1077 /** 1078 * {@icu} Constant for date skeleton with second. 1079 * @stable ICU 50 1080 */ 1081 public static final String SECOND = "s"; 1082 1083 /** 1084 * {@icu} Constant for date skeleton with hour, minute, and second, 1085 * with the locale's preferred hour format (12 or 24). 1086 * Used in combinations date + time, date + time + zone, or time + zone. 1087 * @stable ICU 4.0 1088 */ 1089 public static final String HOUR_MINUTE_SECOND = "jms"; 1090 1091 /** 1092 * {@icu} Constant for date skeleton with hour, minute, and second in 1093 * 24-hour presentation. 1094 * Used in combinations date + time, date + time + zone, or time + zone. 1095 * @stable ICU 4.0 1096 */ 1097 public static final String HOUR24_MINUTE_SECOND = "Hms"; 1098 1099 /** 1100 * {@icu} Constant for date skeleton with minute and second. 1101 * Used in combinations date + time, date + time + zone, or time + zone. 1102 * @stable ICU 4.0 1103 */ 1104 public static final String MINUTE_SECOND = "ms"; 1105 1106 /* 1107 * TIMEZONES 1108 */ 1109 1110 /** 1111 * {@icu} Constant for <i>generic location format</i>, such as Los Angeles Time; 1112 * used in combinations date + time + zone, or time + zone. 1113 * @see <a href="http://unicode.org/reports/tr35/#Date_Format_Patterns">LDML Date Format Patterns</a> 1114 * @see <a href="http://unicode.org/reports/tr35/#Time_Zone_Fallback">LDML Time Zone Fallback</a> 1115 * @stable ICU 50 1116 */ 1117 public static final String LOCATION_TZ = "VVVV"; 1118 1119 /** 1120 * {@icu} Constant for <i>generic non-location format</i>, such as Pacific Time; 1121 * used in combinations date + time + zone, or time + zone. 1122 * @see <a href="http://unicode.org/reports/tr35/#Date_Format_Patterns">LDML Date Format Patterns</a> 1123 * @see <a href="http://unicode.org/reports/tr35/#Time_Zone_Fallback">LDML Time Zone Fallback</a> 1124 * @stable ICU 50 1125 */ 1126 public static final String GENERIC_TZ = "vvvv"; 1127 1128 /** 1129 * {@icu} Constant for <i>generic non-location format</i>, abbreviated if possible, such as PT; 1130 * used in combinations date + time + zone, or time + zone. 1131 * @see <a href="http://unicode.org/reports/tr35/#Date_Format_Patterns">LDML Date Format Patterns</a> 1132 * @see <a href="http://unicode.org/reports/tr35/#Time_Zone_Fallback">LDML Time Zone Fallback</a> 1133 * @stable ICU 50 1134 */ 1135 public static final String ABBR_GENERIC_TZ = "v"; 1136 1137 /** 1138 * {@icu} Constant for <i>specific non-location format</i>, such as Pacific Daylight Time; 1139 * used in combinations date + time + zone, or time + zone. 1140 * @see <a href="http://unicode.org/reports/tr35/#Date_Format_Patterns">LDML Date Format Patterns</a> 1141 * @see <a href="http://unicode.org/reports/tr35/#Time_Zone_Fallback">LDML Time Zone Fallback</a> 1142 * @stable ICU 50 1143 */ 1144 public static final String SPECIFIC_TZ = "zzzz"; 1145 1146 /** 1147 * {@icu} Constant for <i>specific non-location format</i>, abbreviated if possible, such as PDT; 1148 * used in combinations date + time + zone, or time + zone. 1149 * @see <a href="http://unicode.org/reports/tr35/#Date_Format_Patterns">LDML Date Format Patterns</a> 1150 * @see <a href="http://unicode.org/reports/tr35/#Time_Zone_Fallback">LDML Time Zone Fallback</a> 1151 * @stable ICU 50 1152 */ 1153 public static final String ABBR_SPECIFIC_TZ = "z"; 1154 1155 /** 1156 * {@icu} Constant for <i>localized GMT/UTC format</i>, such as GMT+8:00 or HPG-8:00; 1157 * used in combinations date + time + zone, or time + zone. 1158 * @see <a href="http://unicode.org/reports/tr35/#Date_Format_Patterns">LDML Date Format Patterns</a> 1159 * @see <a href="http://unicode.org/reports/tr35/#Time_Zone_Fallback">LDML Time Zone Fallback</a> 1160 * @stable ICU 50 1161 */ 1162 public static final String ABBR_UTC_TZ = "ZZZZ"; 1163 1164 /* 1165 * deprecated skeleton constants 1166 */ 1167 1168 /** 1169 * {@icu} Constant for date skeleton with standalone month. 1170 * @deprecated ICU 50 Use {@link #MONTH} instead. 1171 */ 1172 @Deprecated 1173 public static final String STANDALONE_MONTH = "LLLL"; 1174 1175 /** 1176 * {@icu} Constant for date skeleton with standalone abbreviated month. 1177 * @deprecated ICU 50 Use {@link #ABBR_MONTH} instead. 1178 */ 1179 @Deprecated 1180 public static final String ABBR_STANDALONE_MONTH = "LLL"; 1181 1182 /** 1183 * {@icu} Constant for date skeleton with hour, minute, and generic timezone. 1184 * @deprecated ICU 50 Use instead {@link #HOUR_MINUTE}+{@link #ABBR_GENERIC_TZ} or some other timezone presentation. 1185 */ 1186 @Deprecated 1187 public static final String HOUR_MINUTE_GENERIC_TZ = "jmv"; 1188 1189 /** 1190 * {@icu} Constant for date skeleton with hour, minute, and timezone. 1191 * @deprecated ICU 50 Use instead {@link #HOUR_MINUTE}+{@link #ABBR_SPECIFIC_TZ} or some other timezone presentation. 1192 */ 1193 @Deprecated 1194 public static final String HOUR_MINUTE_TZ = "jmz"; 1195 1196 /** 1197 * {@icu} Constant for date skeleton with hour and generic timezone. 1198 * @deprecated ICU 50 Use instead {@link #HOUR}+{@link #ABBR_GENERIC_TZ} or some other timezone presentation. 1199 */ 1200 @Deprecated 1201 public static final String HOUR_GENERIC_TZ = "jv"; 1202 1203 /** 1204 * {@icu} Constant for date skeleton with hour and timezone. 1205 * @deprecated ICU 50 Use instead {@link #HOUR}+{@link #ABBR_SPECIFIC_TZ} or some other timezone presentation. 1206 */ 1207 @Deprecated 1208 public static final String HOUR_TZ = "jz"; 1209 1210 1211 /** 1212 * Gets the time formatter with the default formatting style 1213 * for the default <code>FORMAT</code> locale. 1214 * @return a time formatter. 1215 * @see Category#FORMAT 1216 * @stable ICU 2.0 1217 */ getTimeInstance()1218 public final static DateFormat getTimeInstance() 1219 { 1220 return get(-1, DEFAULT, ULocale.getDefault(Category.FORMAT), null); 1221 } 1222 1223 /** 1224 * Returns the time formatter with the given formatting style 1225 * for the default <code>FORMAT</code> locale. 1226 * @param style the given formatting style. For example, 1227 * SHORT for "h:mm a" in the US locale. Relative time styles are not currently 1228 * supported, and behave just like the corresponding non-relative style. 1229 * @return a time formatter. 1230 * @see Category#FORMAT 1231 * @stable ICU 2.0 1232 */ getTimeInstance(int style)1233 public final static DateFormat getTimeInstance(int style) 1234 { 1235 return get(-1, style, ULocale.getDefault(Category.FORMAT), null); 1236 } 1237 1238 /** 1239 * Returns the time formatter with the given formatting style 1240 * for the given locale. 1241 * @param style the given formatting style. For example, 1242 * SHORT for "h:mm a" in the US locale. Relative time styles are not currently 1243 * supported, and behave just like the corresponding non-relative style. 1244 * @param aLocale the given locale. 1245 * @return a time formatter. 1246 * @stable ICU 2.0 1247 */ getTimeInstance(int style, Locale aLocale)1248 public final static DateFormat getTimeInstance(int style, 1249 Locale aLocale) 1250 { 1251 return get(-1, style, ULocale.forLocale(aLocale), null); 1252 } 1253 1254 /** 1255 * Returns the time formatter with the given formatting style 1256 * for the given locale. 1257 * @param style the given formatting style. For example, 1258 * SHORT for "h:mm a" in the US locale. Relative time styles are not currently 1259 * supported, and behave just like the corresponding non-relative style. 1260 * @param locale the given ulocale. 1261 * @return a time formatter. 1262 * @stable ICU 3.2 1263 */ getTimeInstance(int style, ULocale locale)1264 public final static DateFormat getTimeInstance(int style, 1265 ULocale locale) 1266 { 1267 return get(-1, style, locale, null); 1268 } 1269 1270 /** 1271 * Returns the date formatter with the default formatting style 1272 * for the default <code>FORMAT</code> locale. 1273 * @return a date formatter. 1274 * @see Category#FORMAT 1275 * @stable ICU 2.0 1276 */ getDateInstance()1277 public final static DateFormat getDateInstance() 1278 { 1279 return get(DEFAULT, -1, ULocale.getDefault(Category.FORMAT), null); 1280 } 1281 1282 /** 1283 * Returns the date formatter with the given formatting style 1284 * for the default <code>FORMAT</code> locale. 1285 * @param style the given formatting style. For example, 1286 * SHORT for "M/d/yy" in the US locale. As currently implemented, relative date 1287 * formatting only affects a limited range of calendar days before or after the 1288 * current date, based on the CLDR <field type="day">/<relative> data: For example, 1289 * in English, "Yesterday", "Today", and "Tomorrow". Outside of this range, relative 1290 * dates are formatted using the corresponding non-relative style. 1291 * @return a date formatter. 1292 * @see Category#FORMAT 1293 * @stable ICU 2.0 1294 */ getDateInstance(int style)1295 public final static DateFormat getDateInstance(int style) 1296 { 1297 return get(style, -1, ULocale.getDefault(Category.FORMAT), null); 1298 } 1299 1300 /** 1301 * Returns the date formatter with the given formatting style 1302 * for the given locale. 1303 * @param style the given formatting style. For example, 1304 * SHORT for "M/d/yy" in the US locale. As currently implemented, relative date 1305 * formatting only affects a limited range of calendar days before or after the 1306 * current date, based on the CLDR <field type="day">/<relative> data: For example, 1307 * in English, "Yesterday", "Today", and "Tomorrow". Outside of this range, relative 1308 * dates are formatted using the corresponding non-relative style. 1309 * @param aLocale the given locale. 1310 * @return a date formatter. 1311 * @stable ICU 2.0 1312 */ getDateInstance(int style, Locale aLocale)1313 public final static DateFormat getDateInstance(int style, 1314 Locale aLocale) 1315 { 1316 return get(style, -1, ULocale.forLocale(aLocale), null); 1317 } 1318 1319 /** 1320 * Returns the date formatter with the given formatting style 1321 * for the given locale. 1322 * @param style the given formatting style. For example, 1323 * SHORT for "M/d/yy" in the US locale. As currently implemented, relative date 1324 * formatting only affects a limited range of calendar days before or after the 1325 * current date, based on the CLDR <field type="day">/<relative> data: For example, 1326 * in English, "Yesterday", "Today", and "Tomorrow". Outside of this range, relative 1327 * dates are formatted using the corresponding non-relative style. 1328 * @param locale the given ulocale. 1329 * @return a date formatter. 1330 * @stable ICU 3.2 1331 */ getDateInstance(int style, ULocale locale)1332 public final static DateFormat getDateInstance(int style, 1333 ULocale locale) 1334 { 1335 return get(style, -1, locale, null); 1336 } 1337 1338 /** 1339 * Returns the date/time formatter with the default formatting style 1340 * for the default <code>FORMAT</code> locale. 1341 * @return a date/time formatter. 1342 * @see Category#FORMAT 1343 * @stable ICU 2.0 1344 */ getDateTimeInstance()1345 public final static DateFormat getDateTimeInstance() 1346 { 1347 return get(DEFAULT, DEFAULT, ULocale.getDefault(Category.FORMAT), null); 1348 } 1349 1350 /** 1351 * Returns the date/time formatter with the given date and time 1352 * formatting styles for the default <code>FORMAT</code> locale. 1353 * @param dateStyle the given date formatting style. For example, 1354 * SHORT for "M/d/yy" in the US locale. As currently implemented, relative date 1355 * formatting only affects a limited range of calendar days before or after the 1356 * current date, based on the CLDR <field type="day">/<relative> data: For example, 1357 * in English, "Yesterday", "Today", and "Tomorrow". Outside of this range, relative 1358 * dates are formatted using the corresponding non-relative style. 1359 * @param timeStyle the given time formatting style. For example, 1360 * SHORT for "h:mm a" in the US locale. Relative time styles are not currently 1361 * supported, and behave just like the corresponding non-relative style. 1362 * @return a date/time formatter. 1363 * @see Category#FORMAT 1364 * @stable ICU 2.0 1365 */ getDateTimeInstance(int dateStyle, int timeStyle)1366 public final static DateFormat getDateTimeInstance(int dateStyle, 1367 int timeStyle) 1368 { 1369 return get(dateStyle, timeStyle, ULocale.getDefault(Category.FORMAT), null); 1370 } 1371 1372 /** 1373 * Returns the date/time formatter with the given formatting styles 1374 * for the given locale. 1375 * @param dateStyle the given date formatting style. As currently implemented, relative date 1376 * formatting only affects a limited range of calendar days before or after the 1377 * current date, based on the CLDR <field type="day">/<relative> data: For example, 1378 * in English, "Yesterday", "Today", and "Tomorrow". Outside of this range, relative 1379 * dates are formatted using the corresponding non-relative style. 1380 * @param timeStyle the given time formatting style. Relative time styles are not 1381 * currently supported, and behave just like the corresponding non-relative style. 1382 * @param aLocale the given locale. 1383 * @return a date/time formatter. 1384 * @stable ICU 2.0 1385 */ getDateTimeInstance( int dateStyle, int timeStyle, Locale aLocale)1386 public final static DateFormat getDateTimeInstance( 1387 int dateStyle, int timeStyle, Locale aLocale) 1388 { 1389 return get(dateStyle, timeStyle, ULocale.forLocale(aLocale), null); 1390 } 1391 1392 /** 1393 * Returns the date/time formatter with the given formatting styles 1394 * for the given locale. 1395 * @param dateStyle the given date formatting style. As currently implemented, relative date 1396 * formatting only affects a limited range of calendar days before or after the 1397 * current date, based on the CLDR <field type="day">/<relative> data: For example, 1398 * in English, "Yesterday", "Today", and "Tomorrow". Outside of this range, relative 1399 * dates are formatted using the corresponding non-relative style. 1400 * @param timeStyle the given time formatting style. Relative time styles are not 1401 * currently supported, and behave just like the corresponding non-relative style. 1402 * @param locale the given ulocale. 1403 * @return a date/time formatter. 1404 * @stable ICU 3.2 1405 */ getDateTimeInstance( int dateStyle, int timeStyle, ULocale locale)1406 public final static DateFormat getDateTimeInstance( 1407 int dateStyle, int timeStyle, ULocale locale) 1408 { 1409 return get(dateStyle, timeStyle, locale, null); 1410 } 1411 1412 /** 1413 * Returns a default date/time formatter that uses the SHORT style for both the 1414 * date and the time. 1415 * @stable ICU 2.0 1416 */ getInstance()1417 public final static DateFormat getInstance() { 1418 return getDateTimeInstance(SHORT, SHORT); 1419 } 1420 1421 /** 1422 * Returns the set of locales for which DateFormats are installed. 1423 * @return the set of locales for which DateFormats are installed. 1424 * @stable ICU 2.0 1425 */ getAvailableLocales()1426 public static Locale[] getAvailableLocales() 1427 { 1428 return ICUResourceBundle.getAvailableLocales(); 1429 } 1430 1431 /** 1432 * {@icu} Returns the set of locales for which DateFormats are installed. 1433 * @return the set of locales for which DateFormats are installed. 1434 * @draft ICU 3.2 (retain) 1435 * @provisional This API might change or be removed in a future release. 1436 */ getAvailableULocales()1437 public static ULocale[] getAvailableULocales() 1438 { 1439 return ICUResourceBundle.getAvailableULocales(); 1440 } 1441 1442 /** 1443 * Sets the calendar to be used by this date format. Initially, the default 1444 * calendar for the specified or default locale is used. 1445 * @param newCalendar the new Calendar to be used by the date format 1446 * @stable ICU 2.0 1447 */ setCalendar(Calendar newCalendar)1448 public void setCalendar(Calendar newCalendar) 1449 { 1450 this.calendar = newCalendar; 1451 } 1452 1453 /** 1454 * Returns the calendar associated with this date/time formatter. 1455 * @return the calendar associated with this date/time formatter. 1456 * @stable ICU 2.0 1457 */ getCalendar()1458 public Calendar getCalendar() 1459 { 1460 return calendar; 1461 } 1462 1463 /** 1464 * Sets the number formatter. 1465 * @param newNumberFormat the given new NumberFormat. 1466 * @stable ICU 2.0 1467 */ setNumberFormat(NumberFormat newNumberFormat)1468 public void setNumberFormat(NumberFormat newNumberFormat) 1469 { 1470 this.numberFormat = newNumberFormat; 1471 /*In order to parse String like "11.10.2001" to DateTime correctly 1472 in Locale("fr","CH") [Richard/GCL] 1473 */ 1474 this.numberFormat.setParseIntegerOnly(true); 1475 } 1476 1477 /** 1478 * Returns the number formatter which this date/time formatter uses to 1479 * format and parse a time. 1480 * @return the number formatter which this date/time formatter uses. 1481 * @stable ICU 2.0 1482 */ getNumberFormat()1483 public NumberFormat getNumberFormat() 1484 { 1485 return numberFormat; 1486 } 1487 1488 /** 1489 * Sets the time zone for the calendar of this DateFormat object. 1490 * @param zone the given new time zone. 1491 * @stable ICU 2.0 1492 */ setTimeZone(TimeZone zone)1493 public void setTimeZone(TimeZone zone) 1494 { 1495 calendar.setTimeZone(zone); 1496 } 1497 1498 /** 1499 * Returns the time zone. 1500 * @return the time zone associated with the calendar of DateFormat. 1501 * @stable ICU 2.0 1502 */ getTimeZone()1503 public TimeZone getTimeZone() 1504 { 1505 return calendar.getTimeZone(); 1506 } 1507 1508 /** 1509 * Specifies whether date/time parsing is to be lenient. With 1510 * lenient parsing, the parser may use heuristics to interpret inputs that 1511 * do not precisely match this object's format. Without lenient parsing, 1512 * inputs must match this object's format more closely. 1513 * <br/><br/> 1514 * <b>Note:</b> ICU 53 introduced finer grained control of leniency (and added 1515 * new control points) making the preferred method a combination of 1516 * setCalendarLenient() & setBooleanAttribute() calls. 1517 * This method supports prior functionality but may not support all 1518 * future leniency control & behavior of DateFormat. For control of pre 53 leniency, 1519 * Calendar and DateFormat whitespace & numeric tolerance, this method is safe to 1520 * use. However, mixing leniency control via this method and modification of the 1521 * newer attributes via setBooleanAttribute() may produce undesirable 1522 * results. 1523 * 1524 * @param lenient True specifies date/time interpretation to be lenient. 1525 * @see com.ibm.icu.util.Calendar#setLenient 1526 * @see #setBooleanAttribute(BooleanAttribute, boolean) 1527 * @see #setCalendarLenient(boolean) 1528 * @stable ICU 2.0 1529 */ setLenient(boolean lenient)1530 public void setLenient(boolean lenient) 1531 { 1532 calendar.setLenient(lenient); 1533 setBooleanAttribute(BooleanAttribute.PARSE_ALLOW_NUMERIC, lenient); 1534 setBooleanAttribute(BooleanAttribute.PARSE_ALLOW_WHITESPACE, lenient); 1535 } 1536 1537 /** 1538 * Returns whether both date/time parsing in the encapsulated Calendar object and DateFormat whitespace & 1539 * numeric processing is lenient. 1540 * @stable ICU 2.0 1541 */ isLenient()1542 public boolean isLenient() 1543 { 1544 return calendar.isLenient() 1545 && getBooleanAttribute(BooleanAttribute.PARSE_ALLOW_NUMERIC) 1546 && getBooleanAttribute(BooleanAttribute.PARSE_ALLOW_WHITESPACE); 1547 } 1548 1549 /** 1550 * Specifies whether date/time parsing in the encapsulated Calendar object should be lenient. 1551 * With lenient parsing, the parser may use heuristics to interpret inputs that 1552 * do not precisely match this object's format. Without lenient parsing, 1553 * inputs must match this object's format more closely. 1554 * @param lenient when true, Calendar parsing is lenient 1555 * @see com.ibm.icu.util.Calendar#setLenient 1556 * @stable ICU 53 1557 */ setCalendarLenient(boolean lenient)1558 public void setCalendarLenient(boolean lenient) 1559 { 1560 calendar.setLenient(lenient); 1561 } 1562 1563 1564 /** 1565 * Returns whether date/time parsing in the encapsulated Calendar object is lenient. 1566 * @stable ICU 53 1567 */ isCalendarLenient()1568 public boolean isCalendarLenient() 1569 { 1570 return calendar.isLenient(); 1571 } 1572 1573 /** 1574 * Sets a boolean attribute for this instance. Aspects of DateFormat leniency are controlled by 1575 * boolean attributes. 1576 * 1577 * @see BooleanAttribute 1578 * @stable ICU 53 1579 */ setBooleanAttribute(BooleanAttribute key, boolean value)1580 public DateFormat setBooleanAttribute(BooleanAttribute key, boolean value) 1581 { 1582 if(value) 1583 { 1584 booleanAttributes.add(key); 1585 } 1586 else 1587 { 1588 booleanAttributes.remove(key); 1589 } 1590 1591 return this; 1592 } 1593 1594 /** 1595 * Returns the current value for the specified BooleanAttribute for this instance 1596 * 1597 * if attribute is missing false is returned. 1598 * 1599 * @see BooleanAttribute 1600 * @stable ICU 53 1601 */ getBooleanAttribute(BooleanAttribute key)1602 public boolean getBooleanAttribute(BooleanAttribute key) 1603 { 1604 return booleanAttributes.contains(key); 1605 } 1606 1607 1608 /** 1609 * {@icu} Set a particular DisplayContext value in the formatter, 1610 * such as CAPITALIZATION_FOR_STANDALONE. 1611 * 1612 * @param context The DisplayContext value to set. 1613 * @stable ICU 53 1614 */ setContext(DisplayContext context)1615 public void setContext(DisplayContext context) { 1616 if (context.type() == DisplayContext.Type.CAPITALIZATION) { 1617 capitalizationSetting = context; 1618 } 1619 } 1620 1621 /** 1622 * {@icu} Get the formatter's DisplayContext value for the specified DisplayContext.Type, 1623 * such as CAPITALIZATION. 1624 * 1625 * @param type the DisplayContext.Type whose value to return 1626 * @return the current DisplayContext setting for the specified type 1627 * @stable ICU 53 1628 */ getContext(DisplayContext.Type type)1629 public DisplayContext getContext(DisplayContext.Type type) { 1630 return (type == DisplayContext.Type.CAPITALIZATION && capitalizationSetting != null)? 1631 capitalizationSetting: DisplayContext.CAPITALIZATION_NONE; 1632 } 1633 1634 /** 1635 * Overrides hashCode. 1636 * @stable ICU 2.0 1637 */ 1638 ///CLOVER:OFF 1639 // turn off code coverage since all subclasses override this hashCode()1640 public int hashCode() { 1641 return numberFormat.hashCode(); 1642 // just enough fields for a reasonable distribution 1643 } 1644 ///CLOVER:ON 1645 1646 /** 1647 * Overrides equals. 1648 * @stable ICU 2.0 1649 */ equals(Object obj)1650 public boolean equals(Object obj) { 1651 if (this == obj) return true; 1652 if (obj == null || getClass() != obj.getClass()) return false; 1653 DateFormat other = (DateFormat) obj; 1654 return (((calendar==null && other.calendar==null) || 1655 (calendar!=null && other.calendar!=null && calendar.isEquivalentTo(other.calendar))) && 1656 ((numberFormat==null && other.numberFormat==null) || 1657 (numberFormat!=null && other.numberFormat!=null && numberFormat.equals(other.numberFormat))) && 1658 capitalizationSetting == other.capitalizationSetting); 1659 } 1660 1661 /** 1662 * Overrides clone. 1663 * @stable ICU 2.0 1664 */ clone()1665 public Object clone() 1666 { 1667 DateFormat other = (DateFormat) super.clone(); 1668 other.calendar = (Calendar) calendar.clone(); 1669 if (numberFormat != null) { 1670 other.numberFormat = (NumberFormat) numberFormat.clone(); 1671 } 1672 return other; 1673 } 1674 1675 /** 1676 * Creates a DateFormat with the given time and/or date style in the given 1677 * locale. 1678 * @param dateStyle a value from 0 to 3 indicating the time format, 1679 * or -1 to indicate no date 1680 * @param timeStyle a value from 0 to 3 indicating the time format, 1681 * or -1 to indicate no time 1682 * @param loc the locale for the format 1683 * @param cal the calendar to be used, or null 1684 */ get(int dateStyle, int timeStyle, ULocale loc, Calendar cal)1685 private static DateFormat get(int dateStyle, int timeStyle, ULocale loc, Calendar cal) { 1686 if((timeStyle != DateFormat.NONE && (timeStyle & RELATIVE)>0) || 1687 (dateStyle != DateFormat.NONE && (dateStyle & RELATIVE)>0)) { 1688 RelativeDateFormat r = new RelativeDateFormat(timeStyle, dateStyle /* offset? */, loc, cal); 1689 return r; 1690 } 1691 1692 if (timeStyle < DateFormat.NONE || timeStyle > DateFormat.SHORT) { 1693 throw new IllegalArgumentException("Illegal time style " + timeStyle); 1694 } 1695 if (dateStyle < DateFormat.NONE || dateStyle > DateFormat.SHORT) { 1696 throw new IllegalArgumentException("Illegal date style " + dateStyle); 1697 } 1698 1699 if (cal == null) { 1700 cal = Calendar.getInstance(loc); 1701 } 1702 1703 try { 1704 DateFormat result = cal.getDateTimeFormat(dateStyle, timeStyle, loc); 1705 result.setLocale(cal.getLocale(ULocale.VALID_LOCALE), 1706 cal.getLocale(ULocale.ACTUAL_LOCALE)); 1707 return result; 1708 } catch (MissingResourceException e) { 1709 ///CLOVER:OFF 1710 // coverage requires separate run with no data, so skip 1711 return new SimpleDateFormat("M/d/yy h:mm a"); 1712 ///CLOVER:ON 1713 } 1714 } 1715 1716 /** 1717 * First, read in the default serializable data. 1718 * 1719 * Then, if <code>serialVersionOnStream</code> is less than 1, indicating that 1720 * the stream was written by a pre-ICU-53 version, 1721 * set capitalizationSetting to a default value. 1722 * Finally, set serialVersionOnStream back to the maximum allowed value so that 1723 * default serialization will work properly if this object is streamed out again. 1724 */ readObject(ObjectInputStream stream)1725 private void readObject(ObjectInputStream stream) 1726 throws IOException, ClassNotFoundException 1727 { 1728 stream.defaultReadObject(); 1729 if (serialVersionOnStream < 1) { 1730 // Didn't have capitalizationSetting, set it to default 1731 capitalizationSetting = DisplayContext.CAPITALIZATION_NONE; 1732 } 1733 serialVersionOnStream = currentSerialVersion; 1734 } 1735 1736 /** 1737 * Creates a new date format. 1738 * @stable ICU 2.0 1739 */ DateFormat()1740 protected DateFormat() {} 1741 1742 //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 1743 1744 //------------------------------------------------------------------------- 1745 // Public static interface for creating custon DateFormats for different 1746 // types of Calendars. 1747 //------------------------------------------------------------------------- 1748 1749 /** 1750 * Creates a {@link DateFormat} object that can be used to format dates in 1751 * the calendar system specified by <code>cal</code>. 1752 * <p> 1753 * @param cal The calendar system for which a date format is desired. 1754 * 1755 * @param dateStyle The type of date format desired. This can be 1756 * {@link DateFormat#SHORT}, {@link DateFormat#MEDIUM}, 1757 * etc. 1758 * 1759 * @param locale The locale for which the date format is desired. 1760 * @stable ICU 2.0 1761 */ getDateInstance(Calendar cal, int dateStyle, Locale locale)1762 static final public DateFormat getDateInstance(Calendar cal, int dateStyle, Locale locale) 1763 { 1764 return getDateTimeInstance(cal, dateStyle, -1, ULocale.forLocale(locale)); 1765 } 1766 1767 /** 1768 * Creates a {@link DateFormat} object that can be used to format dates in 1769 * the calendar system specified by <code>cal</code>. 1770 * <p> 1771 * @param cal The calendar system for which a date format is desired. 1772 * 1773 * @param dateStyle The type of date format desired. This can be 1774 * {@link DateFormat#SHORT}, {@link DateFormat#MEDIUM}, 1775 * etc. 1776 * 1777 * @param locale The locale for which the date format is desired. 1778 * @stable ICU 3.2 1779 */ getDateInstance(Calendar cal, int dateStyle, ULocale locale)1780 static final public DateFormat getDateInstance(Calendar cal, int dateStyle, ULocale locale) 1781 { 1782 return getDateTimeInstance(cal, dateStyle, -1, locale); 1783 } 1784 1785 /** 1786 * Creates a {@link DateFormat} object that can be used to format times in 1787 * the calendar system specified by <code>cal</code>. 1788 * <p> 1789 * <b>Note:</b> When this functionality is moved into the core JDK, this method 1790 * will probably be replaced by a new overload of {@link DateFormat#getInstance}. 1791 * <p> 1792 * @param cal The calendar system for which a time format is desired. 1793 * 1794 * @param timeStyle The type of time format desired. This can be 1795 * {@link DateFormat#SHORT}, {@link DateFormat#MEDIUM}, 1796 * etc. 1797 * 1798 * @param locale The locale for which the time format is desired. 1799 * 1800 * @see DateFormat#getTimeInstance 1801 * @stable ICU 2.0 1802 */ getTimeInstance(Calendar cal, int timeStyle, Locale locale)1803 static final public DateFormat getTimeInstance(Calendar cal, int timeStyle, Locale locale) 1804 { 1805 return getDateTimeInstance(cal, -1, timeStyle, ULocale.forLocale(locale)); 1806 } 1807 1808 /** 1809 * Creates a {@link DateFormat} object that can be used to format times in 1810 * the calendar system specified by <code>cal</code>. 1811 * <p> 1812 * <b>Note:</b> When this functionality is moved into the core JDK, this method 1813 * will probably be replaced by a new overload of {@link DateFormat#getInstance}. 1814 * <p> 1815 * @param cal The calendar system for which a time format is desired. 1816 * 1817 * @param timeStyle The type of time format desired. This can be 1818 * {@link DateFormat#SHORT}, {@link DateFormat#MEDIUM}, 1819 * etc. 1820 * 1821 * @param locale The locale for which the time format is desired. 1822 * 1823 * @see DateFormat#getTimeInstance 1824 * @stable ICU 3.2 1825 */ getTimeInstance(Calendar cal, int timeStyle, ULocale locale)1826 static final public DateFormat getTimeInstance(Calendar cal, int timeStyle, ULocale locale) 1827 { 1828 return getDateTimeInstance(cal, -1, timeStyle, locale); 1829 } 1830 1831 /** 1832 * Creates a {@link DateFormat} object that can be used to format dates and times in 1833 * the calendar system specified by <code>cal</code>. 1834 * <p> 1835 * <b>Note:</b> When this functionality is moved into the core JDK, this method 1836 * will probably be replaced by a new overload of {@link DateFormat#getInstance}. 1837 * <p> 1838 * @param cal The calendar system for which a date/time format is desired. 1839 * 1840 * @param dateStyle The type of date format desired. This can be 1841 * {@link DateFormat#SHORT}, {@link DateFormat#MEDIUM}, 1842 * etc. 1843 * 1844 * @param timeStyle The type of time format desired. This can be 1845 * {@link DateFormat#SHORT}, {@link DateFormat#MEDIUM}, 1846 * etc. 1847 * 1848 * @param locale The locale for which the date/time format is desired. 1849 * 1850 * @see DateFormat#getDateTimeInstance 1851 * @stable ICU 2.0 1852 */ getDateTimeInstance(Calendar cal, int dateStyle, int timeStyle, Locale locale)1853 static final public DateFormat getDateTimeInstance(Calendar cal, int dateStyle, 1854 int timeStyle, Locale locale) 1855 { 1856 return getDateTimeInstance(dateStyle, timeStyle, ULocale.forLocale(locale)); 1857 } 1858 1859 /** 1860 * Creates a {@link DateFormat} object that can be used to format dates and times in 1861 * the calendar system specified by <code>cal</code>. 1862 * <p> 1863 * <b>Note:</b> When this functionality is moved into the core JDK, this method 1864 * will probably be replaced by a new overload of {@link DateFormat#getInstance}. 1865 * <p> 1866 * @param cal The calendar system for which a date/time format is desired. 1867 * 1868 * @param dateStyle The type of date format desired. This can be 1869 * {@link DateFormat#SHORT}, {@link DateFormat#MEDIUM}, 1870 * etc. 1871 * 1872 * @param timeStyle The type of time format desired. This can be 1873 * {@link DateFormat#SHORT}, {@link DateFormat#MEDIUM}, 1874 * etc. 1875 * 1876 * @param locale The locale for which the date/time format is desired. 1877 * 1878 * @see DateFormat#getDateTimeInstance 1879 * @stable ICU 3.2 1880 */ getDateTimeInstance(Calendar cal, int dateStyle, int timeStyle, ULocale locale)1881 static final public DateFormat getDateTimeInstance(Calendar cal, int dateStyle, 1882 int timeStyle, ULocale locale) 1883 { 1884 if (cal == null) { 1885 throw new IllegalArgumentException("Calendar must be supplied"); 1886 } 1887 return get(dateStyle, timeStyle, locale, cal); 1888 } 1889 1890 /** 1891 * Returns a date/time formatter that uses the SHORT style 1892 * for both the date and the time. 1893 * 1894 * @param cal The calendar system for which a date/time format is desired. 1895 * @param locale The locale for which the date/time format is desired. 1896 * @stable ICU 2.0 1897 */ getInstance(Calendar cal, Locale locale)1898 static final public DateFormat getInstance(Calendar cal, Locale locale) { 1899 return getDateTimeInstance(cal, SHORT, SHORT, ULocale.forLocale(locale)); 1900 } 1901 1902 /** 1903 * Returns a date/time formatter that uses the SHORT style 1904 * for both the date and the time. 1905 * 1906 * @param cal The calendar system for which a date/time format is desired. 1907 * @param locale The locale for which the date/time format is desired. 1908 * @stable ICU 3.2 1909 * @provisional This API might change or be removed in a future release. 1910 */ getInstance(Calendar cal, ULocale locale)1911 static final public DateFormat getInstance(Calendar cal, ULocale locale) { 1912 return getDateTimeInstance(cal, SHORT, SHORT, locale); 1913 } 1914 1915 /** 1916 * Returns a default date/time formatter that uses the SHORT style for both the 1917 * date and the time. 1918 * 1919 * @param cal The calendar system for which a date/time format is desired. 1920 * @stable ICU 2.0 1921 */ getInstance(Calendar cal)1922 static final public DateFormat getInstance(Calendar cal) { 1923 return getInstance(cal, ULocale.getDefault(Category.FORMAT)); 1924 } 1925 1926 /** 1927 * Creates a {@link DateFormat} object for the default locale that can be used 1928 * to format dates in the calendar system specified by <code>cal</code>. 1929 * <p> 1930 * @param cal The calendar system for which a date format is desired. 1931 * 1932 * @param dateStyle The type of date format desired. This can be 1933 * {@link DateFormat#SHORT}, {@link DateFormat#MEDIUM}, 1934 * etc. 1935 * @stable ICU 2.0 1936 */ getDateInstance(Calendar cal, int dateStyle)1937 static final public DateFormat getDateInstance(Calendar cal, int dateStyle) { 1938 return getDateInstance(cal, dateStyle, ULocale.getDefault(Category.FORMAT)); 1939 } 1940 1941 /** 1942 * Creates a {@link DateFormat} object that can be used to format times in 1943 * the calendar system specified by <code>cal</code>. 1944 * <p> 1945 * <b>Note:</b> When this functionality is moved into the core JDK, this method 1946 * will probably be replaced by a new overload of {@link DateFormat#getInstance}. 1947 * <p> 1948 * @param cal The calendar system for which a time format is desired. 1949 * 1950 * @param timeStyle The type of time format desired. This can be 1951 * {@link DateFormat#SHORT}, {@link DateFormat#MEDIUM}, 1952 * etc. 1953 * 1954 * @see DateFormat#getTimeInstance 1955 * @stable ICU 2.0 1956 */ getTimeInstance(Calendar cal, int timeStyle)1957 static final public DateFormat getTimeInstance(Calendar cal, int timeStyle) { 1958 return getTimeInstance(cal, timeStyle, ULocale.getDefault(Category.FORMAT)); 1959 } 1960 1961 /** 1962 * Creates a {@link DateFormat} object for the default locale that can be used to format 1963 * dates and times in the calendar system specified by <code>cal</code>. 1964 * <p> 1965 * <b>Note:</b> When this functionality is moved into the core JDK, this method 1966 * will probably be replaced by a new overload of {@link DateFormat#getInstance}. 1967 * <p> 1968 * @param cal The calendar system for which a date/time format is desired. 1969 * 1970 * @param dateStyle The type of date format desired. This can be 1971 * {@link DateFormat#SHORT}, {@link DateFormat#MEDIUM}, 1972 * etc. 1973 * 1974 * @param timeStyle The type of time format desired. This can be 1975 * {@link DateFormat#SHORT}, {@link DateFormat#MEDIUM}, 1976 * etc. 1977 * 1978 * @see DateFormat#getDateTimeInstance 1979 * @stable ICU 2.0 1980 */ getDateTimeInstance(Calendar cal, int dateStyle, int timeStyle)1981 static final public DateFormat getDateTimeInstance(Calendar cal, int dateStyle, int timeStyle) { 1982 return getDateTimeInstance(cal, dateStyle, timeStyle, ULocale.getDefault(Category.FORMAT)); 1983 } 1984 1985 /** 1986 * {@icu} Returns a {@link DateFormat} object that can be used to format dates and times in 1987 * the default locale. 1988 * 1989 * @param skeleton The skeleton that selects the fields to be formatted. (Uses the 1990 * {@link DateTimePatternGenerator}.) This can be {@link DateFormat#ABBR_MONTH}, 1991 * {@link DateFormat#MONTH_WEEKDAY_DAY}, etc. 1992 * 1993 * @draft ICU 54 1994 * @provisional This API might change or be removed in a future release. 1995 */ getInstanceForSkeleton(String skeleton)1996 public final static DateFormat getInstanceForSkeleton(String skeleton) { 1997 return getPatternInstance(skeleton, ULocale.getDefault(Category.FORMAT)); 1998 } 1999 2000 /** 2001 * {@icu} Returns a {@link DateFormat} object that can be used to format dates and times in 2002 * the given locale. 2003 * 2004 * @param skeleton The skeleton that selects the fields to be formatted. (Uses the 2005 * {@link DateTimePatternGenerator}.) This can be {@link DateFormat#ABBR_MONTH}, 2006 * {@link DateFormat#MONTH_WEEKDAY_DAY}, etc. 2007 * 2008 * @param locale The locale for which the date/time format is desired. 2009 * 2010 * @draft ICU 54 2011 * @provisional This API might change or be removed in a future release. 2012 */ getInstanceForSkeleton(String skeleton, Locale locale)2013 public final static DateFormat getInstanceForSkeleton(String skeleton, Locale locale) { 2014 return getPatternInstance(skeleton, ULocale.forLocale(locale)); 2015 } 2016 2017 /** 2018 * {@icu} Returns a {@link DateFormat} object that can be used to format dates and times in 2019 * the given locale. 2020 * 2021 * @param skeleton The skeleton that selects the fields to be formatted. (Uses the 2022 * {@link DateTimePatternGenerator}.) This can be {@link DateFormat#ABBR_MONTH}, 2023 * {@link DateFormat#MONTH_WEEKDAY_DAY}, etc. 2024 * 2025 * @param locale The locale for which the date/time format is desired. 2026 * 2027 * @draft ICU 54 2028 * @provisional This API might change or be removed in a future release. 2029 */ getInstanceForSkeleton(String skeleton, ULocale locale)2030 public final static DateFormat getInstanceForSkeleton(String skeleton, ULocale locale) { 2031 DateTimePatternGenerator generator = DateTimePatternGenerator.getInstance(locale); 2032 final String bestPattern = generator.getBestPattern(skeleton); 2033 return new SimpleDateFormat(bestPattern, locale); 2034 } 2035 2036 /** 2037 * {@icu} Creates a {@link DateFormat} object that can be used to format dates and 2038 * times in the calendar system specified by <code>cal</code>. 2039 * 2040 * @param cal The calendar system for which a date/time format is desired. 2041 * 2042 * @param skeleton The skeleton that selects the fields to be formatted. (Uses the 2043 * {@link DateTimePatternGenerator}.) This can be 2044 * {@link DateFormat#ABBR_MONTH}, {@link DateFormat#MONTH_WEEKDAY_DAY}, 2045 * etc. 2046 * 2047 * @param locale The locale for which the date/time format is desired. 2048 * 2049 * @draft ICU 54 2050 * @provisional This API might change or be removed in a future release. 2051 */ getInstanceForSkeleton(Calendar cal, String skeleton, Locale locale)2052 public final static DateFormat getInstanceForSkeleton(Calendar cal, String skeleton, Locale locale) { 2053 return getPatternInstance(cal, skeleton, ULocale.forLocale(locale)); 2054 } 2055 2056 /** 2057 * {@icu} Creates a {@link DateFormat} object that can be used to format dates and 2058 * times in the calendar system specified by <code>cal</code>. 2059 * 2060 * @param cal The calendar system for which a date/time format is desired. 2061 * 2062 * @param skeleton The skeleton that selects the fields to be formatted. (Uses the 2063 * {@link DateTimePatternGenerator}.) This can be 2064 * {@link DateFormat#ABBR_MONTH}, {@link DateFormat#MONTH_WEEKDAY_DAY}, 2065 * etc. 2066 * 2067 * @param locale The locale for which the date/time format is desired. 2068 * 2069 * @draft ICU 54 2070 * @provisional This API might change or be removed in a future release. 2071 */ getInstanceForSkeleton( Calendar cal, String skeleton, ULocale locale)2072 public final static DateFormat getInstanceForSkeleton( 2073 Calendar cal, String skeleton, ULocale locale) { 2074 DateTimePatternGenerator generator = DateTimePatternGenerator.getInstance(locale); 2075 final String bestPattern = generator.getBestPattern(skeleton); 2076 SimpleDateFormat format = new SimpleDateFormat(bestPattern, locale); 2077 format.setCalendar(cal); 2078 return format; 2079 } 2080 2081 2082 /** 2083 * {@icu} Returns a {@link DateFormat} object that can be used to format dates and times in 2084 * the default locale. 2085 * The getInstanceForSkeleton methods are preferred over the getPatternInstance methods. 2086 * 2087 * @param skeleton The skeleton that selects the fields to be formatted. (Uses the 2088 * {@link DateTimePatternGenerator}.) This can be {@link DateFormat#ABBR_MONTH}, 2089 * {@link DateFormat#MONTH_WEEKDAY_DAY}, etc. 2090 * 2091 * @stable ICU 4.0 2092 */ getPatternInstance(String skeleton)2093 public final static DateFormat getPatternInstance(String skeleton) { 2094 return getInstanceForSkeleton(skeleton); 2095 } 2096 2097 /** 2098 * {@icu} Returns a {@link DateFormat} object that can be used to format dates and times in 2099 * the given locale. 2100 * The getInstanceForSkeleton methods are preferred over the getPatternInstance methods. 2101 * 2102 * @param skeleton The skeleton that selects the fields to be formatted. (Uses the 2103 * {@link DateTimePatternGenerator}.) This can be {@link DateFormat#ABBR_MONTH}, 2104 * {@link DateFormat#MONTH_WEEKDAY_DAY}, etc. 2105 * 2106 * @param locale The locale for which the date/time format is desired. 2107 * 2108 * @stable ICU 4.0 2109 */ getPatternInstance(String skeleton, Locale locale)2110 public final static DateFormat getPatternInstance(String skeleton, Locale locale) { 2111 return getInstanceForSkeleton(skeleton, locale); 2112 } 2113 2114 /** 2115 * {@icu} Returns a {@link DateFormat} object that can be used to format dates and times in 2116 * the given locale. 2117 * The getInstanceForSkeleton methods are preferred over the getPatternInstance methods. 2118 * 2119 * @param skeleton The skeleton that selects the fields to be formatted. (Uses the 2120 * {@link DateTimePatternGenerator}.) This can be {@link DateFormat#ABBR_MONTH}, 2121 * {@link DateFormat#MONTH_WEEKDAY_DAY}, etc. 2122 * 2123 * @param locale The locale for which the date/time format is desired. 2124 * 2125 * @stable ICU 4.0 2126 */ getPatternInstance(String skeleton, ULocale locale)2127 public final static DateFormat getPatternInstance(String skeleton, ULocale locale) { 2128 return getInstanceForSkeleton(skeleton, locale); 2129 } 2130 2131 /** 2132 * {@icu} Creates a {@link DateFormat} object that can be used to format dates and 2133 * times in the calendar system specified by <code>cal</code>. 2134 * The getInstanceForSkeleton methods are preferred over the getPatternInstance methods. 2135 * 2136 * @param cal The calendar system for which a date/time format is desired. 2137 * 2138 * @param skeleton The skeleton that selects the fields to be formatted. (Uses the 2139 * {@link DateTimePatternGenerator}.) This can be 2140 * {@link DateFormat#ABBR_MONTH}, {@link DateFormat#MONTH_WEEKDAY_DAY}, 2141 * etc. 2142 * 2143 * @param locale The locale for which the date/time format is desired. 2144 * 2145 * @stable ICU 4.0 2146 */ getPatternInstance(Calendar cal, String skeleton, Locale locale)2147 public final static DateFormat getPatternInstance(Calendar cal, String skeleton, Locale locale) { 2148 return getInstanceForSkeleton(cal, skeleton, locale); 2149 } 2150 2151 /** 2152 * {@icu} Creates a {@link DateFormat} object that can be used to format dates and 2153 * times in the calendar system specified by <code>cal</code>. 2154 * The getInstanceForSkeleton methods are preferred over the getPatternInstance methods. 2155 * 2156 * @param cal The calendar system for which a date/time format is desired. 2157 * 2158 * @param skeleton The skeleton that selects the fields to be formatted. (Uses the 2159 * {@link DateTimePatternGenerator}.) This can be 2160 * {@link DateFormat#ABBR_MONTH}, {@link DateFormat#MONTH_WEEKDAY_DAY}, 2161 * etc. 2162 * 2163 * @param locale The locale for which the date/time format is desired. 2164 * 2165 * @stable ICU 4.0 2166 */ getPatternInstance( Calendar cal, String skeleton, ULocale locale)2167 public final static DateFormat getPatternInstance( 2168 Calendar cal, String skeleton, ULocale locale) { 2169 return getInstanceForSkeleton(cal, skeleton, locale); 2170 } 2171 2172 /** 2173 * The instances of this inner class are used as attribute keys and values 2174 * in AttributedCharacterIterator that 2175 * DateFormat.formatToCharacterIterator() method returns. 2176 * 2177 * <p>There is no public constructor to this class, the only instances are the 2178 * constants defined here. 2179 * <p> 2180 * @stable ICU 3.8 2181 */ 2182 public static class Field extends Format.Field { 2183 2184 private static final long serialVersionUID = -3627456821000730829L; 2185 2186 // Max number of calendar fields 2187 private static final int CAL_FIELD_COUNT; 2188 2189 // Table for mapping calendar field number to DateFormat.Field 2190 private static final Field[] CAL_FIELDS; 2191 2192 // Map for resolving DateFormat.Field by name 2193 private static final Map<String, Field> FIELD_NAME_MAP; 2194 2195 static { 2196 GregorianCalendar cal = new GregorianCalendar(); 2197 CAL_FIELD_COUNT = cal.getFieldCount(); 2198 CAL_FIELDS = new Field[CAL_FIELD_COUNT]; 2199 FIELD_NAME_MAP = new HashMap<String, Field>(CAL_FIELD_COUNT); 2200 } 2201 2202 // Java fields ------------------- 2203 2204 /** 2205 * Constant identifying the time of day indicator(am/pm). 2206 * @stable ICU 3.8 2207 */ 2208 public static final Field AM_PM = new Field("am pm", Calendar.AM_PM); 2209 2210 /** 2211 * Constant identifying the day of month field. 2212 * @stable ICU 3.8 2213 */ 2214 public static final Field DAY_OF_MONTH = new Field("day of month", Calendar.DAY_OF_MONTH); 2215 2216 /** 2217 * Constant identifying the day of week field. 2218 * @stable ICU 3.8 2219 */ 2220 public static final Field DAY_OF_WEEK = new Field("day of week", Calendar.DAY_OF_WEEK); 2221 2222 /** 2223 * Constant identifying the day of week in month field. 2224 * @stable ICU 3.8 2225 */ 2226 public static final Field DAY_OF_WEEK_IN_MONTH = 2227 new Field("day of week in month", Calendar.DAY_OF_WEEK_IN_MONTH); 2228 2229 /** 2230 * Constant identifying the day of year field. 2231 * @stable ICU 3.8 2232 */ 2233 public static final Field DAY_OF_YEAR = new Field("day of year", Calendar.DAY_OF_YEAR); 2234 2235 /** 2236 * Constant identifying the era field. 2237 * @stable ICU 3.8 2238 */ 2239 public static final Field ERA = new Field("era", Calendar.ERA); 2240 2241 /** 2242 * Constant identifying the hour(0-23) of day field. 2243 * @stable ICU 3.8 2244 */ 2245 public static final Field HOUR_OF_DAY0 = new Field("hour of day", Calendar.HOUR_OF_DAY); 2246 2247 /** 2248 * Constant identifying the hour(1-24) of day field. 2249 * @stable ICU 3.8 2250 */ 2251 public static final Field HOUR_OF_DAY1 = new Field("hour of day 1", -1); 2252 2253 /** 2254 * Constant identifying the hour(0-11) field. 2255 * @stable ICU 3.8 2256 */ 2257 public static final Field HOUR0 = new Field("hour", Calendar.HOUR); 2258 2259 /** 2260 * Constant identifying the hour(1-12) field. 2261 * @stable ICU 3.8 2262 */ 2263 public static final Field HOUR1 = new Field("hour 1", -1); 2264 2265 /** 2266 * Constant identifying the millisecond field. 2267 * @stable ICU 3.8 2268 */ 2269 public static final Field MILLISECOND = new Field("millisecond", Calendar.MILLISECOND); 2270 2271 /** 2272 * Constant identifying the minute field. 2273 * @stable ICU 3.8 2274 */ 2275 public static final Field MINUTE = new Field("minute", Calendar.MINUTE); 2276 2277 /** 2278 * Constant identifying the month field. 2279 * @stable ICU 3.8 2280 */ 2281 public static final Field MONTH = new Field("month", Calendar.MONTH); 2282 2283 /** 2284 * Constant identifying the second field. 2285 * @stable ICU 3.8 2286 */ 2287 public static final Field SECOND = new Field("second", Calendar.SECOND); 2288 2289 /** 2290 * Constant identifying the time zone field. 2291 * @stable ICU 3.8 2292 */ 2293 public static final Field TIME_ZONE = new Field("time zone", -1); 2294 2295 /** 2296 * Constant identifying the week of month field. 2297 * @stable ICU 3.8 2298 */ 2299 public static final Field WEEK_OF_MONTH = 2300 new Field("week of month", Calendar.WEEK_OF_MONTH); 2301 2302 /** 2303 * Constant identifying the week of year field. 2304 * @stable ICU 3.8 2305 */ 2306 public static final Field WEEK_OF_YEAR = new Field("week of year", Calendar.WEEK_OF_YEAR); 2307 2308 /** 2309 * Constant identifying the year field. 2310 * @stable ICU 3.8 2311 */ 2312 public static final Field YEAR = new Field("year", Calendar.YEAR); 2313 2314 2315 // ICU only fields ------------------- 2316 2317 /** 2318 * Constant identifying the local day of week field. 2319 * @stable ICU 3.8 2320 */ 2321 public static final Field DOW_LOCAL = new Field("local day of week", Calendar.DOW_LOCAL); 2322 2323 /** 2324 * Constant identifying the extended year field. 2325 * @stable ICU 3.8 2326 */ 2327 public static final Field EXTENDED_YEAR = new Field("extended year", 2328 Calendar.EXTENDED_YEAR); 2329 2330 /** 2331 * Constant identifying the Julian day field. 2332 * @stable ICU 3.8 2333 */ 2334 public static final Field JULIAN_DAY = new Field("Julian day", Calendar.JULIAN_DAY); 2335 2336 /** 2337 * Constant identifying the milliseconds in day field. 2338 * @stable ICU 3.8 2339 */ 2340 public static final Field MILLISECONDS_IN_DAY = 2341 new Field("milliseconds in day", Calendar.MILLISECONDS_IN_DAY); 2342 2343 /** 2344 * Constant identifying the year used with week of year field. 2345 * @stable ICU 3.8 2346 */ 2347 public static final Field YEAR_WOY = new Field("year for week of year", Calendar.YEAR_WOY); 2348 2349 /** 2350 * Constant identifying the quarter field. 2351 * @stable ICU 3.8 2352 */ 2353 public static final Field QUARTER = new Field("quarter", -1); 2354 2355 /** 2356 * Constant identifying the related year field. 2357 * @internal 2358 * @deprecated This API is ICU internal only. 2359 */ 2360 @Deprecated 2361 public static final Field RELATED_YEAR = new Field("related year", -1); 2362 2363 /** 2364 * Constant identifying the time separator field. 2365 * @draft ICU 55 2366 * @provisional This API might change or be removed in a future release. 2367 */ 2368 public static final Field TIME_SEPARATOR = new Field("time separator", -1); 2369 2370 // Stand alone types are variants for its base types. So we do not define Field for 2371 // them. 2372 /* 2373 public static final Field STANDALONE_DAY = 2374 new Field("stand alone day of week", Calendar.DAY_OF_WEEK); 2375 public static final Field STANDALONE_MONTH = new Field("stand alone month", Calendar.MONTH); 2376 public static final Field STANDALONE_QUARTER = new Field("stand alone quarter", -1); 2377 */ 2378 2379 // Corresponding calendar field 2380 private final int calendarField; 2381 2382 /** 2383 * Constructs a <code>DateFormat.Field</code> with the given name and 2384 * the <code>Calendar</code> field which this attribute represents. Use -1 for 2385 * <code>calendarField</code> if this field does not have a corresponding 2386 * <code>Calendar</code> field. 2387 * 2388 * @param name Name of the attribute 2389 * @param calendarField <code>Calendar</code> field constant 2390 * 2391 * @stable ICU 3.8 2392 */ Field(String name, int calendarField)2393 protected Field(String name, int calendarField) { 2394 super(name); 2395 this.calendarField = calendarField; 2396 if (this.getClass() == DateFormat.Field.class) { 2397 FIELD_NAME_MAP.put(name, this); 2398 if (calendarField >= 0 && calendarField < CAL_FIELD_COUNT) { 2399 CAL_FIELDS[calendarField] = this; 2400 } 2401 } 2402 } 2403 2404 /** 2405 * Returns the <code>Field</code> constant that corresponds to the <code> 2406 * Calendar</code> field <code>calendarField</code>. If there is no 2407 * corresponding <code>Field</code> is available, null is returned. 2408 * 2409 * @param calendarField <code>Calendar</code> field constant 2410 * @return <code>Field</code> associated with the <code>calendarField</code>, 2411 * or null if no associated <code>Field</code> is available. 2412 * @throws IllegalArgumentException if <code>calendarField</code> is not 2413 * a valid <code>Calendar</code> field constant. 2414 * 2415 * @stable ICU 3.8 2416 */ ofCalendarField(int calendarField)2417 public static DateFormat.Field ofCalendarField(int calendarField) { 2418 if (calendarField < 0 || calendarField >= CAL_FIELD_COUNT) { 2419 throw new IllegalArgumentException("Calendar field number is out of range"); 2420 } 2421 return CAL_FIELDS[calendarField]; 2422 } 2423 2424 /** 2425 * Returns the <code>Calendar</code> field associated with this attribute. 2426 * If there is no corresponding <code>Calendar</code> available, this will 2427 * return -1. 2428 * 2429 * @return <code>Calendar</code> constant for this attribute. 2430 * 2431 * @stable ICU 3.8 2432 */ getCalendarField()2433 public int getCalendarField() { 2434 return calendarField; 2435 } 2436 2437 /** 2438 * Resolves instances being deserialized to the predefined constants. 2439 * 2440 * @throws InvalidObjectException if the constant could not be resolved. 2441 * 2442 * @stable ICU 3.8 2443 */ readResolve()2444 protected Object readResolve() throws InvalidObjectException { 2445 ///CLOVER:OFF 2446 if (this.getClass() != DateFormat.Field.class) { 2447 throw new InvalidObjectException( 2448 "A subclass of DateFormat.Field must implement readResolve."); 2449 } 2450 ///CLOVER:ON 2451 Object o = FIELD_NAME_MAP.get(this.getName()); 2452 ///CLOVER:OFF 2453 if (o == null) { 2454 throw new InvalidObjectException("Unknown attribute name."); 2455 } 2456 ///CLOVER:ON 2457 return o; 2458 } 2459 } 2460 } 2461