Source for java.text.DateFormat

   1: /* DateFormat.java -- Class for formatting/parsing date/times
   2:    Copyright (C) 1998, 1999, 2000, 2001, 2003, 2004, 2005
   3:    Free Software Foundation, Inc.
   4: 
   5: This file is part of GNU Classpath.
   6: 
   7: GNU Classpath is free software; you can redistribute it and/or modify
   8: it under the terms of the GNU General Public License as published by
   9: the Free Software Foundation; either version 2, or (at your option)
  10: any later version.
  11:  
  12: GNU Classpath is distributed in the hope that it will be useful, but
  13: WITHOUT ANY WARRANTY; without even the implied warranty of
  14: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  15: General Public License for more details.
  16: 
  17: You should have received a copy of the GNU General Public License
  18: along with GNU Classpath; see the file COPYING.  If not, write to the
  19: Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  20: 02110-1301 USA.
  21: 
  22: Linking this library statically or dynamically with other modules is
  23: making a combined work based on this library.  Thus, the terms and
  24: conditions of the GNU General Public License cover the whole
  25: combination.
  26: 
  27: As a special exception, the copyright holders of this library give you
  28: permission to link this library with independent modules to produce an
  29: executable, regardless of the license terms of these independent
  30: modules, and to copy and distribute the resulting executable under
  31: terms of your choice, provided that you also meet, for each linked
  32: independent module, the terms and conditions of the license of that
  33: module.  An independent module is a module which is not derived from
  34: or based on this library.  If you modify this library, you may extend
  35: this exception to your version of the library, but you are not
  36: obligated to do so.  If you do not wish to do so, delete this
  37: exception statement from your version. */
  38: 
  39: 
  40: package java.text;
  41: 
  42: import java.io.InvalidObjectException;
  43: import java.util.Calendar;
  44: import java.util.Date;
  45: import java.util.Locale;
  46: import java.util.MissingResourceException;
  47: import java.util.ResourceBundle;
  48: import java.util.TimeZone;
  49: 
  50: /**
  51:  * @author Per Bothner (bothner@cygnus.com)
  52:  * @date October 25, 1998.
  53:  */
  54: /* Written using "Java Class Libraries", 2nd edition, plus online
  55:  * API docs for JDK 1.2 beta from http://www.javasoft.com.
  56:  * Status:  Mostly complete; search for FIXME to see omissions.
  57:  */
  58: 
  59: public abstract class DateFormat extends Format implements Cloneable
  60: {
  61:   protected Calendar calendar;
  62:   protected NumberFormat numberFormat;
  63: 
  64:   // (Values determined using a test program.)
  65:   public static final int FULL = 0;
  66:   public static final int LONG = 1;
  67:   public static final int MEDIUM = 2;
  68:   public static final int SHORT = 3;
  69:   public static final int DEFAULT = MEDIUM;
  70: 
  71:   /* These constants need to have these exact values.  They
  72:    * correspond to index positions within the localPatternChars
  73:    * string for a given locale.  Each locale may specify its
  74:    * own character for a particular field, but the position
  75:    * of these characters must correspond to an appropriate field
  76:    * number (as listed below), in order for their meaning to
  77:    * be determined.  For example, the US locale uses
  78:    * the string "GyMdkHmsSEDFwWahKzYeugAZ", where 'G' is the character
  79:    * for era, 'y' for year, and so on down to 'Z' for time zone.
  80:    */
  81:   /**
  82:    * Represents the position of the era
  83:    * pattern character in the array of
  84:    * localized pattern characters. 
  85:    * For example, 'AD' is an era used
  86:    * in the Gregorian calendar system.
  87:    * In the U.S. locale, this is 'G'.
  88:    */  
  89:   public static final int ERA_FIELD = 0;
  90:   /**
  91:    * Represents the position of the year
  92:    * pattern character in the array of
  93:    * localized pattern characters.
  94:    * In the U.S. locale, this is 'y'.
  95:    */
  96:   public static final int YEAR_FIELD = 1;
  97:   /**
  98:    * Represents the position of the month
  99:    * pattern character in the array of
 100:    * localized pattern characters.
 101:    * In the U.S. locale, this is 'M'.
 102:    */
 103:   public static final int MONTH_FIELD = 2;
 104:   /**
 105:    * Represents the position of the date
 106:    * or day of the month pattern character
 107:    * in the array of localized pattern
 108:    * characters.  In the U.S. locale,
 109:    * this is 'd'.
 110:    */
 111:   public static final int DATE_FIELD = 3;
 112:   /**
 113:    * Represents the position of the 24
 114:    * hour pattern character in the array of
 115:    * localized pattern characters.
 116:    * In the U.S. locale, this is 'k'.
 117:    * This field numbers hours from 1 to 24.
 118:    */
 119:   public static final int HOUR_OF_DAY1_FIELD = 4;
 120:   /**
 121:    * Represents the position of the 24
 122:    * hour pattern character in the array of
 123:    * localized pattern characters.
 124:    * In the U.S. locale, this is 'H'.
 125:    * This field numbers hours from 0 to 23.
 126:    */
 127:   public static final int HOUR_OF_DAY0_FIELD = 5;
 128:   /**
 129:    * Represents the position of the minute
 130:    * pattern character in the array of
 131:    * localized pattern characters.
 132:    * In the U.S. locale, this is 'm'.
 133:    */
 134:   public static final int MINUTE_FIELD = 6;
 135:   /**
 136:    * Represents the position of the second
 137:    * pattern character in the array of
 138:    * localized pattern characters.
 139:    * In the U.S. locale, this is 's'.
 140:    */
 141:   public static final int SECOND_FIELD = 7;
 142:   /**
 143:    * Represents the position of the millisecond
 144:    * pattern character in the array of
 145:    * localized pattern characters.
 146:    * In the U.S. locale, this is 'S'.
 147:    */
 148:   public static final int MILLISECOND_FIELD = 8;
 149:   /**
 150:    * Represents the position of the day of the
 151:    * week pattern character in the array of
 152:    * localized pattern characters.
 153:    * In the U.S. locale, this is 'E'.
 154:    */
 155:   public static final int DAY_OF_WEEK_FIELD = 9;
 156:   /**
 157:    * Represents the position of the day of the
 158:    * year pattern character in the array of
 159:    * localized pattern characters.
 160:    * In the U.S. locale, this is 'D'.
 161:    */
 162:   public static final int DAY_OF_YEAR_FIELD = 10;
 163:   /**
 164:    * Represents the position of the day of the
 165:    * week in the month pattern character in the
 166:    * array of localized pattern characters.
 167:    * In the U.S. locale, this is 'F'.
 168:    */
 169:   public static final int DAY_OF_WEEK_IN_MONTH_FIELD = 11;
 170:   /**
 171:    * Represents the position of the week of the
 172:    * year pattern character in the array of
 173:    * localized pattern characters.
 174:    * In the U.S. locale, this is 'w'.
 175:    */
 176:   public static final int WEEK_OF_YEAR_FIELD = 12;
 177:   /**
 178:    * Represents the position of the week of the
 179:    * month pattern character in the array of
 180:    * localized pattern characters.
 181:    * In the U.S. locale, this is 'W'.
 182:    */
 183:   public static final int WEEK_OF_MONTH_FIELD = 13;
 184:   /**
 185:    * Represents the position of the am/pm
 186:    * pattern character in the array of
 187:    * localized pattern characters.
 188:    * In the U.S. locale, this is 'a'.
 189:    */
 190:   public static final int AM_PM_FIELD = 14;
 191:   /**
 192:    * Represents the position of the 12 
 193:    * hour pattern character in the array of
 194:    * localized pattern characters.
 195:    * In the U.S. locale, this is 'h'.
 196:    * This field numbers hours from 1 to 12.
 197:    */
 198:   public static final int HOUR1_FIELD = 15;
 199:   /**
 200:    * Represents the position of the 12 
 201:    * hour pattern character in the array of
 202:    * localized pattern characters.
 203:    * In the U.S. locale, this is 'K'.
 204:    * This field numbers hours from 0 to 11.
 205:    */
 206:   public static final int HOUR0_FIELD = 16;
 207:   /**
 208:    * Represents the position of the generic
 209:    * timezone pattern character in the array of
 210:    * localized pattern characters.
 211:    * In the U.S. locale, this is 'z'.
 212:    */
 213:   public static final int TIMEZONE_FIELD = 17;
 214:   /**
 215:    * Represents the position of the ISO year
 216:    * pattern character in the array of
 217:    * localized pattern characters.
 218:    * In the U.S. locale, this is 'Y'.
 219:    * This is a GNU extension in accordance with
 220:    * the CLDR data used.  This value may
 221:    * differ from the normal year value.
 222:    */
 223:   public static final int ISO_YEAR_FIELD = 18;
 224:   /**
 225:    * Represents the position of the localized
 226:    * day of the week pattern character in the
 227:    * array of localized pattern characters.
 228:    * In the U.S. locale, this is 'e'.
 229:    * This is a GNU extension in accordance with
 230:    * the CLDR data used.  This value only
 231:    * differs from the day of the week with
 232:    * numeric formatting, in which case the
 233:    * locale's first day of the week is used.
 234:    */
 235:   public static final int LOCALIZED_DAY_OF_WEEK_FIELD = 19;
 236:   /**
 237:    * Represents the position of the extended year
 238:    * pattern character in the array of
 239:    * localized pattern characters.
 240:    * In the U.S. locale, this is 'u'.
 241:    * This is a GNU extension in accordance with
 242:    * the CLDR data used.  This value modifies
 243:    * the year value, so as to incorporate the era.
 244:    * For example, in the Gregorian calendar system,
 245:    * the extended year is negative instead of being
 246:    * marked as BC.
 247:    */
 248:   public static final int EXTENDED_YEAR_FIELD = 20;
 249:   /**
 250:    * Represents the position of the modified Julian
 251:    * day pattern character in the array of
 252:    * localized pattern characters.
 253:    * In the U.S. locale, this is 'g'.
 254:    * This is a GNU extension in accordance with
 255:    * the CLDR data used.  This value differs
 256:    * from the standard Julian day in that days
 257:    * are marked from midnight onwards rather than
 258:    * noon, and the local time zone affects the value.
 259:    * In simple terms, it can be thought of as all
 260:    * the date fields represented as a single number.
 261:    */
 262:   public static final int MODIFIED_JULIAN_DAY_FIELD = 21;
 263:   /**
 264:    * Represents the position of the millisecond
 265:    * in the day pattern character in the array of
 266:    * localized pattern characters.
 267:    * In the U.S. locale, this is 'A'.
 268:    * This is a GNU extension in accordance with
 269:    * the CLDR data used.  This value represents
 270:    * all the time fields (excluding the time zone)
 271:    * numerically, giving the number of milliseconds
 272:    * into the day (e.g. 10 in the morning would
 273:    * be 10 * 60 * 60 * 1000).  Any daylight savings
 274:    * offset also affects this value.
 275:    */
 276:   public static final int MILLISECOND_IN_DAY_FIELD = 22;
 277:   /**
 278:    * Represents the position of the RFC822
 279:    * timezone pattern character in the array of
 280:    * localized pattern characters.
 281:    * In the U.S. locale, this is 'Z'.
 282:    * This is a GNU extension in accordance with
 283:    * the CLDR data used.  The value is the offset
 284:    * of the current time from GMT e.g. -0500 would
 285:    * be five hours prior to GMT.
 286:    */
 287:   public static final int RFC822_TIMEZONE_FIELD = 23;
 288: 
 289:   public static class Field extends Format.Field
 290:   {
 291:     static final long serialVersionUID = 7441350119349544720L;
 292:     
 293:     private int calendarField;
 294: 
 295:     public static final DateFormat.Field ERA
 296:     = new Field("era", Calendar.ERA);
 297:     public static final DateFormat.Field YEAR
 298:     = new Field("year", Calendar.YEAR);
 299:     public static final DateFormat.Field MONTH
 300:     = new Field("month", Calendar.MONTH);
 301:     public static final DateFormat.Field DAY_OF_MONTH
 302:     = new Field("day of month", Calendar.DAY_OF_MONTH);
 303:     public static final DateFormat.Field HOUR_OF_DAY1
 304:     = new Field("hour of day 1", Calendar.HOUR_OF_DAY);
 305:     public static final DateFormat.Field HOUR_OF_DAY0
 306:     = new Field("hour of day 0", Calendar.HOUR_OF_DAY);
 307:     public static final DateFormat.Field MINUTE
 308:     = new Field("minute", Calendar.MINUTE);
 309:     public static final DateFormat.Field SECOND
 310:     = new Field("second", Calendar.SECOND);
 311:     public static final DateFormat.Field MILLISECOND
 312:     = new Field("millisecond", Calendar.MILLISECOND);
 313:     public static final DateFormat.Field DAY_OF_WEEK
 314:     = new Field("day of week", Calendar.DAY_OF_WEEK);
 315:     public static final DateFormat.Field DAY_OF_YEAR
 316:     = new Field("day of year", Calendar.DAY_OF_YEAR);
 317:     public static final DateFormat.Field DAY_OF_WEEK_IN_MONTH
 318:     = new Field("day of week in month", Calendar.DAY_OF_WEEK_IN_MONTH);
 319:     public static final DateFormat.Field WEEK_OF_YEAR
 320:     = new Field("week of year", Calendar.WEEK_OF_YEAR);
 321:     public static final DateFormat.Field WEEK_OF_MONTH
 322:     = new Field("week of month", Calendar.WEEK_OF_MONTH);
 323:     public static final DateFormat.Field AM_PM
 324:     = new Field("am/pm", Calendar.AM_PM);
 325:     public static final DateFormat.Field HOUR1
 326:     = new Field("hour1", Calendar.HOUR);
 327:     public static final DateFormat.Field HOUR0
 328:     = new Field("hour0", Calendar.HOUR);
 329:     public static final DateFormat.Field TIME_ZONE
 330:     = new Field("timezone", Calendar.ZONE_OFFSET);
 331:     public static final DateFormat.Field ISO_YEAR
 332:     = new Field("iso year", Calendar.YEAR);
 333:     public static final DateFormat.Field LOCALIZED_DAY_OF_WEEK
 334:     = new Field("localized day of week", Calendar.DAY_OF_WEEK);
 335:     public static final DateFormat.Field EXTENDED_YEAR
 336:       = new Field("extended year", Calendar.YEAR);
 337:     public static final DateFormat.Field MODIFIED_JULIAN_DAY
 338:     = new Field("julian day", -1);
 339:     public static final DateFormat.Field MILLISECOND_IN_DAY
 340:     = new Field("millisecond in day", -1);
 341:     public static final DateFormat.Field RFC822_TIME_ZONE
 342:     = new Field("rfc822 timezone", Calendar.ZONE_OFFSET);
 343: 
 344:     static final DateFormat.Field[] allFields =
 345:     {
 346:       ERA, YEAR, MONTH, DAY_OF_MONTH, HOUR_OF_DAY1,
 347:       HOUR_OF_DAY0, MINUTE, SECOND, MILLISECOND,
 348:       DAY_OF_WEEK, DAY_OF_YEAR, DAY_OF_WEEK_IN_MONTH,
 349:       WEEK_OF_YEAR, WEEK_OF_MONTH, AM_PM, HOUR1, HOUR0,
 350:       TIME_ZONE, ISO_YEAR, LOCALIZED_DAY_OF_WEEK,
 351:       EXTENDED_YEAR, MODIFIED_JULIAN_DAY, MILLISECOND_IN_DAY,
 352:       RFC822_TIME_ZONE
 353:     };
 354: 
 355:     // For deserialization
 356:     private Field()
 357:     {
 358:       super("");
 359:     }
 360: 
 361:     protected Field(String name, int calendarField)
 362:     {
 363:       super(name);
 364:       this.calendarField = calendarField;
 365:     }
 366:     
 367:     public int getCalendarField()
 368:     {
 369:       return calendarField;
 370:     }
 371: 
 372:     public static Field ofCalendarField(int calendarField)
 373:     {
 374:       if (calendarField >= allFields.length || calendarField < 0)
 375:     throw new IllegalArgumentException("no such calendar field ("
 376:                        + calendarField + ")");
 377:       
 378:       return allFields[calendarField];
 379:     }
 380:     
 381:     protected Object readResolve() throws InvalidObjectException
 382:     {
 383:       String s = getName();
 384: 
 385:       for (int i=0;i<allFields.length;i++)
 386:     if (s.equals(allFields[i].getName()))
 387:       return allFields[i];
 388:       
 389:       throw new InvalidObjectException("no such DateFormat field called " + s);
 390:     }
 391:   }
 392: 
 393:   /**
 394:    * This method initializes a new instance of <code>DateFormat</code>.
 395:    */
 396:   protected DateFormat ()
 397:   {
 398:   }
 399: 
 400:   /**
 401:    * This method tests this object for equality against the specified object.
 402:    * The two objects will be considered equal if an only if the specified
 403:    * object:
 404:    * <P>
 405:    * <ul>
 406:    * <li>Is not <code>null</code>.</li>
 407:    * <li>Is an instance of <code>DateFormat</code>.</li>
 408:    * <li>Has the same numberFormat field value as this object.</li>
 409:    * </ul>
 410:    *
 411:    * @param obj The object to test for equality against.
 412:    * 
 413:    * @return <code>true</code> if the specified object is equal to this object,
 414:    * <code>false</code> otherwise.
 415:    */
 416:   public boolean equals (Object obj)
 417:   {
 418:     if (!(obj instanceof DateFormat))
 419:       return false;
 420: 
 421:     DateFormat d = (DateFormat) obj;
 422: 
 423:     return numberFormat.equals(d.numberFormat);
 424:   }
 425: 
 426:   /**
 427:    * This method returns a copy of this object.
 428:    *
 429:    * @return A copy of this object.
 430:    */
 431:   public Object clone ()
 432:   {
 433:     // We know the superclass just call's Object's generic cloner.
 434:     return super.clone ();
 435:   }
 436: 
 437:   /**
 438:    * This method formats the specified <code>Object</code> into a date string
 439:    * and appends it to the specified <code>StringBuffer</code>.
 440:    * The specified object must be an instance of <code>Number</code> or
 441:    * <code>Date</code> or an <code>IllegalArgumentException</code> will be
 442:    * thrown.
 443:    *
 444:    * @param obj The <code>Object</code> to format.
 445:    * @param toAppendTo The <code>StringBuffer</code> to append the resultant
 446:    * <code>String</code> to.
 447:    * @param fieldPosition Is updated to the start and end index of the
 448:    * specified field.
 449:    *
 450:    * @return The <code>StringBuffer</code> supplied on input, with the
 451:    * formatted date/time appended.
 452:    */
 453:   public final StringBuffer format (Object obj,
 454:                     StringBuffer buf, FieldPosition pos)
 455:   {
 456:     if (obj instanceof Number)
 457:       obj = new Date(((Number) obj).longValue());
 458:     else if (! (obj instanceof Date))
 459:       throw new IllegalArgumentException
 460:     ("Cannot format given Object as a Date");
 461: 
 462:     return format ((Date) obj, buf, pos);
 463:   }
 464: 
 465:   /**  
 466:     * Formats the date argument according to the pattern specified. 
 467:     *
 468:     * @param date The formatted date.
 469:     */
 470:   public final String format (Date date)
 471:   {
 472:     StringBuffer sb = new StringBuffer ();
 473:     format (date, sb, new FieldPosition (MONTH_FIELD));
 474:     return sb.toString();
 475:   }
 476: 
 477:   /**
 478:    * This method formats a <code>Date</code> into a string and appends it
 479:    * to the specified <code>StringBuffer</code>.
 480:    *
 481:    * @param date The <code>Date</code> value to format.
 482:    * @param toAppendTo The <code>StringBuffer</code> to append the resultant
 483:    * <code>String</code> to.
 484:    * @param fieldPosition Is updated to the start and end index of the
 485:    * specified field.
 486:    *
 487:    * @return The <code>StringBuffer</code> supplied on input, with the
 488:    * formatted date/time appended.
 489:    */
 490:   public abstract StringBuffer format (Date date,
 491:                        StringBuffer buf, FieldPosition pos);
 492: 
 493:   /**
 494:    * This method returns a list of available locales supported by this
 495:    * class.
 496:    */
 497:   public static Locale[] getAvailableLocales()
 498:   {
 499:     return Locale.getAvailableLocales();
 500:   }
 501: 
 502:   /**
 503:     * This method returns the <code>Calendar</code> object being used by
 504:     * this object to parse/format datetimes.
 505:     *
 506:     * @return The <code>Calendar</code> being used by this object.
 507:     *
 508:     * @see java.util.Calendar
 509:     */
 510:   public Calendar getCalendar ()
 511:   {
 512:     return calendar;
 513:   }
 514: 
 515:   private static DateFormat computeInstance (int style, Locale loc,
 516:                                              boolean use_date, boolean use_time)
 517:   {
 518:     return computeInstance (style, style, loc, use_date, use_time);
 519:   }
 520: 
 521:   private static DateFormat computeInstance (int dateStyle, int timeStyle,
 522:                                              Locale loc, boolean use_date,
 523:                                              boolean use_time)
 524:   {
 525:     ResourceBundle res;
 526:     try
 527:       {
 528:     res = ResourceBundle.getBundle("gnu.java.locale.LocaleInformation",
 529:                        loc, ClassLoader.getSystemClassLoader());
 530:       }
 531:     catch (MissingResourceException x)
 532:       {
 533:     res = null;
 534:       }
 535: 
 536:     String pattern = null;
 537:     if (use_date)
 538:       {
 539:     String name, def;
 540:     switch (dateStyle)
 541:       {
 542:       case FULL:
 543:         name = "fullDateFormat";
 544:         def = "EEEE MMMM d, yyyy G";
 545:         break;
 546:       case LONG:
 547:         name = "longDateFormat";
 548:         def = "MMMM d, yyyy";
 549:         break;
 550:       case MEDIUM:
 551:         name = "mediumDateFormat";
 552:         def = "d-MMM-yy";
 553:         break;
 554:       case SHORT:
 555:         name = "shortDateFormat";
 556:         def = "M/d/yy";
 557:         break;
 558:       default:
 559:         throw new IllegalArgumentException ();
 560:       }
 561:     try
 562:       {
 563:         pattern = res == null ? def : res.getString(name);
 564:       }
 565:     catch (MissingResourceException x)
 566:       {
 567:         pattern = def;
 568:       }
 569:       }
 570: 
 571:     if (use_time)
 572:       {
 573:     if (pattern == null)
 574:       pattern = "";
 575:     else
 576:       pattern += " ";
 577: 
 578:     String name, def;
 579:     switch (timeStyle)
 580:       {
 581:       case FULL:
 582:         name = "fullTimeFormat";
 583:         def = "h:mm:ss;S 'o''clock' a z";
 584:         break;
 585:       case LONG:
 586:         name = "longTimeFormat";
 587:         def = "h:mm:ss a z";
 588:         break;
 589:       case MEDIUM:
 590:         name = "mediumTimeFormat";
 591:         def = "h:mm:ss a";
 592:         break;
 593:       case SHORT:
 594:         name = "shortTimeFormat";
 595:         def = "h:mm a";
 596:         break;
 597:       default:
 598:         throw new IllegalArgumentException ();
 599:       }
 600: 
 601:     String s;
 602:     try
 603:       {
 604:         s = res == null ? def : res.getString(name);
 605:       }
 606:     catch (MissingResourceException x)
 607:       {
 608:         s = def;
 609:       }
 610:     pattern += s;
 611:       }
 612: 
 613:     return new SimpleDateFormat (pattern, loc);
 614:   }
 615: 
 616:  /**
 617:    * This method returns an instance of <code>DateFormat</code> that will
 618:    * format using the default formatting style for dates.
 619:    *
 620:    * @return A new <code>DateFormat</code> instance.
 621:    */
 622:   public static final DateFormat getDateInstance ()
 623:   {
 624:     return getDateInstance (DEFAULT, Locale.getDefault());
 625:   }
 626: 
 627:   /**
 628:    * This method returns an instance of <code>DateFormat</code> that will
 629:    * format using the specified formatting style for dates.
 630:    *
 631:    * @param style The type of formatting to perform. 
 632:    * 
 633:    * @return A new <code>DateFormat</code> instance.
 634:    */
 635:   public static final DateFormat getDateInstance (int style)
 636:   {
 637:     return getDateInstance (style, Locale.getDefault());
 638:   }
 639: 
 640:   /**
 641:    * This method returns an instance of <code>DateFormat</code> that will
 642:    * format using the specified formatting style for dates.  The specified
 643:    * localed will be used in place of the default.
 644:    *
 645:    * @param style The type of formatting to perform. 
 646:    * @param aLocale The desired locale.
 647:    * 
 648:    * @return A new <code>DateFormat</code> instance.
 649:    */
 650:   public static final DateFormat getDateInstance (int style, Locale loc)
 651:   {
 652:     return computeInstance (style, loc, true, false);
 653:   }
 654: 
 655:   /**
 656:    * This method returns a new instance of <code>DateFormat</code> that
 657:    * formats both dates and times using the <code>SHORT</code> style.
 658:    *
 659:    * @return A new <code>DateFormat</code>instance.
 660:    */
 661:   public static final DateFormat getDateTimeInstance ()
 662:   {
 663:     return getDateTimeInstance (DEFAULT, DEFAULT, Locale.getDefault());
 664:   }
 665: 
 666:   /**
 667:    * This method returns a new instance of <code>DateFormat</code> that
 668:    * formats both dates and times using the <code>DEFAULT</code> style.
 669:    *
 670:    * @return A new <code>DateFormat</code>instance.
 671:    */
 672:   public static final DateFormat getDateTimeInstance (int dateStyle, 
 673:                               int timeStyle)
 674:   {
 675:     return getDateTimeInstance (dateStyle, timeStyle, Locale.getDefault());
 676:   }
 677: 
 678:   /**
 679:    * This method returns a new instance of <code>DateFormat</code> that
 680:    * formats both dates and times using the specified styles.
 681:    * 
 682:    * @param dateStyle The desired style for date formatting.
 683:    * @param timeStyle The desired style for time formatting
 684:    *
 685:    * @return A new <code>DateFormat</code>instance.
 686:    */
 687:   public static final DateFormat getDateTimeInstance (int dateStyle, 
 688:                               int timeStyle, 
 689:                               Locale loc)
 690:   {
 691:     return computeInstance (dateStyle, timeStyle, loc, true, true);
 692:   }
 693: 
 694:   /**
 695:    * This method returns a new instance of <code>DateFormat</code> that
 696:    * formats both dates and times using the <code>SHORT</code> style.
 697:    *
 698:    * @return A new <code>DateFormat</code>instance.
 699:    */
 700:   public static final DateFormat getInstance ()
 701:   {
 702:     // JCL book says SHORT.
 703:     return getDateTimeInstance (SHORT, SHORT, Locale.getDefault());
 704:   }
 705: 
 706:   /**
 707:    * This method returns the <code>NumberFormat</code> object being used
 708:    * by this object to parse/format time values.
 709:    *
 710:    * @return The <code>NumberFormat</code> in use by this object.
 711:    */
 712:   public NumberFormat getNumberFormat ()
 713:   {
 714:     return numberFormat;
 715:   }
 716: 
 717:  /**
 718:    * This method returns an instance of <code>DateFormat</code> that will
 719:    * format using the default formatting style for times.
 720:    *
 721:    * @return A new <code>DateFormat</code> instance.
 722:    */
 723:   public static final DateFormat getTimeInstance ()
 724:   {
 725:     return getTimeInstance (DEFAULT, Locale.getDefault());
 726:   }
 727: 
 728:   /**
 729:    * This method returns an instance of <code>DateFormat</code> that will
 730:    * format using the specified formatting style for times.
 731:    *
 732:    * @param style The type of formatting to perform. 
 733:    * 
 734:    * @return A new <code>DateFormat</code> instance.
 735:    */
 736:   public static final DateFormat getTimeInstance (int style)
 737:   {
 738:     return getTimeInstance (style, Locale.getDefault());
 739:   }
 740: 
 741:   /**
 742:    * This method returns an instance of <code>DateFormat</code> that will
 743:    * format using the specified formatting style for times.  The specified
 744:    * localed will be used in place of the default.
 745:    *
 746:    * @param style The type of formatting to perform. 
 747:    * @param aLocale The desired locale.
 748:    * 
 749:    * @return A new <code>DateFormat</code> instance.
 750:    */
 751:   public static final DateFormat getTimeInstance (int style, Locale loc)
 752:   {
 753:     return computeInstance (style, loc, false, true);
 754:   }
 755: 
 756:   /**
 757:    * This method returns the <code>TimeZone</code> object being used by
 758:    * this instance.
 759:    *
 760:    * @return The time zone in use.
 761:    */
 762:   public TimeZone getTimeZone ()
 763:   {
 764:     return calendar.getTimeZone();
 765:   }
 766: 
 767:   /**
 768:    * This method returns a hash value for this object.
 769:    * 
 770:    * @return A hash value for this object.
 771:    */
 772:   public int hashCode ()
 773:   {
 774:     if (numberFormat != null)
 775:       return numberFormat.hashCode();
 776:     else
 777:       return 0;
 778:   }
 779: 
 780:   /**
 781:    * This method indicates whether or not the parsing of date and time
 782:    * values should be done in a lenient value.
 783:    *
 784:    * @return <code>true</code> if date/time parsing is lenient,
 785:    * <code>false</code> otherwise.
 786:    */
 787:   public boolean isLenient ()
 788:   {
 789:     return calendar.isLenient();
 790:   }
 791: 
 792:   /**
 793:    * This method parses the specified date/time string.
 794:    *
 795:    * @param source The string to parse.
 796:    * @return The resultant date.
 797:    *
 798:    * @exception ParseException If the specified string cannot be parsed.
 799:    */
 800:   public Date parse (String source) throws ParseException
 801:   {
 802:     ParsePosition pos = new ParsePosition(0);
 803:     Date result = parse (source, pos);
 804:     if (result == null)
 805:       {
 806:     int index = pos.getErrorIndex();
 807:     if (index < 0)
 808:       index = pos.getIndex();
 809:     throw new ParseException("invalid Date syntax in \""
 810:                  + source + '\"', index);
 811:       }
 812:     return result;
 813:   }
 814: 
 815:   /** 
 816:    * This method parses the specified <code>String</code> into a 
 817:    * <code>Date</code>.  The <code>pos</code> argument contains the
 818:    * starting parse position on method entry and the ending parse
 819:    * position on method exit.
 820:    *
 821:    * @param text The string to parse.
 822:    * @param pos The starting parse position in entry, the ending parse
 823:    * position on exit.
 824:    *
 825:    * @return The parsed date, or <code>null</code> if the string cannot
 826:    * be parsed.
 827:    */
 828:   public abstract Date parse (String source, ParsePosition pos);
 829: 
 830:   /**
 831:    * This method is identical to <code>parse(String, ParsePosition)</code>,
 832:    * but returns its result as an <code>Object</code> instead of a
 833:    * <code>Date</code>.
 834:    * 
 835:    * @param source The string to parse.
 836:    * @param pos The starting parse position in entry, the ending parse
 837:    * position on exit.
 838:    *
 839:    * @return The parsed date, or <code>null</code> if the string cannot
 840:    * be parsed.
 841:    */
 842:   public Object parseObject (String source, ParsePosition pos)
 843:   {
 844:     return parse(source, pos);
 845:   }
 846: 
 847:   /**
 848:    * This method specified the <code>Calendar</code> that should be used 
 849:    * by this object to parse/format datetimes.
 850:    *
 851:    * @param The new <code>Calendar</code> for this object.
 852:    *
 853:    * @see java.util.Calendar
 854:    */
 855:   public void setCalendar (Calendar calendar)
 856:   {
 857:     this.calendar = calendar;
 858:   }
 859: 
 860:   /**
 861:    * This method specifies whether or not this object should be lenient in 
 862:    * the syntax it accepts while parsing date/time values.
 863:    *
 864:    * @param lenient <code>true</code> if parsing should be lenient,
 865:    * <code>false</code> otherwise.
 866:    */
 867:   public void setLenient (boolean lenient)
 868:   {
 869:     calendar.setLenient(lenient);
 870:   }
 871: 
 872:   /**
 873:    * This method specifies the <code>NumberFormat</code> object that should
 874:    * be used by this object to parse/format times.
 875:    *
 876:    * @param The <code>NumberFormat</code> in use by this object.
 877:    */
 878:   public void setNumberFormat (NumberFormat numberFormat)
 879:   {
 880:     this.numberFormat = numberFormat;
 881:   }
 882: 
 883:   /**
 884:    * This method sets the time zone that should be used by this object.
 885:    *
 886:    * @param The new time zone.
 887:    */
 888:   public void setTimeZone (TimeZone timeZone)
 889:   {
 890:     calendar.setTimeZone(timeZone);
 891:   }
 892: }