Source for java.util.Calendar

   1: /* Calendar.java --
   2:    Copyright (C) 1998, 1999, 2000, 2001, 2002, 2004, 2005, 2006,  
   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.util;
  41: 
  42: import java.io.IOException;
  43: import java.io.ObjectInputStream;
  44: import java.io.ObjectOutputStream;
  45: import java.io.Serializable;
  46: 
  47: import java.lang.reflect.Constructor;
  48: import java.lang.reflect.InvocationTargetException;
  49: 
  50: import java.text.DateFormatSymbols;
  51: 
  52: /**
  53:  * This class is an abstract base class for Calendars, which can be
  54:  * used to convert between <code>Date</code> objects and a set of
  55:  * integer fields which represent <code>YEAR</code>,
  56:  * <code>MONTH</code>, <code>DAY</code>, etc.  The <code>Date</code>
  57:  * object represents a time in milliseconds since the Epoch. <br>
  58:  *
  59:  * This class is locale sensitive.  To get the Object matching the
  60:  * current locale you can use <code>getInstance</code>.  You can even provide
  61:  * a locale or a timezone.  <code>getInstance</code> returns currently
  62:  * a <code>GregorianCalendar</code> for the current date. <br>
  63:  *
  64:  * If you want to convert a date from the Year, Month, Day, DayOfWeek,
  65:  * etc.  Representation to a <code>Date</code>-Object, you can create
  66:  * a new Calendar with <code>getInstance()</code>,
  67:  * <code>clear()</code> all fields, <code>set(int,int)</code> the
  68:  * fields you need and convert it with <code>getTime()</code>. <br>
  69:  *
  70:  * If you want to convert a <code>Date</code>-object to the Calendar
  71:  * representation, create a new Calendar, assign the
  72:  * <code>Date</code>-Object with <code>setTime()</code>, and read the
  73:  * fields with <code>get(int)</code>. <br>
  74:  *
  75:  * When computing the date from time fields, it may happen, that there
  76:  * are either two few fields set, or some fields are inconsistent.  This
  77:  * cases will handled in a calendar specific way.  Missing fields are
  78:  * replaced by the fields of the epoch: 1970 January 1 00:00. <br>
  79:  *
  80:  * To understand, how the day of year is computed out of the fields
  81:  * look at the following table.  It is traversed from top to bottom,
  82:  * and for the first line all fields are set, that line is used to
  83:  * compute the day. <br>
  84:  *
  85:  *
  86: <pre>month + day_of_month
  87: month + week_of_month + day_of_week
  88: month + day_of_week_of_month + day_of_week
  89: day_of_year
  90: day_of_week + week_of_year</pre>
  91:  *
  92:  * The hour_of_day-field takes precedence over the ampm and
  93:  * hour_of_ampm fields. <br>
  94:  *
  95:  * <STRONG>Note:</STRONG> This can differ for non-Gregorian calendar. <br>
  96:  *
  97:  * To convert a calendar to a human readable form and vice versa,  use
  98:  * the <code>java.text.DateFormat</code> class. <br>
  99:  *
 100:  * Other useful things you can do with an calendar, is
 101:  * <code>roll</code>ing fields (that means increase/decrease a
 102:  * specific field by one, propagating overflows), or
 103:  * <code>add</code>ing/substracting a fixed amount to a field.
 104:  *
 105:  * @author Aaron M. Renn (arenn@urbanophile.com)
 106:  * @author Jochen Hoenicke (Jochen.Hoenicke@Informatik.Uni-Oldenburg.de)
 107:  * @author Warren Levy (warrenl@cygnus.com)
 108:  * @author Jeff Sturm (jsturm@one-point.com)
 109:  * @author Tom Tromey (tromey@redhat.com)
 110:  * @author Bryce McKinlay (mckinlay@redhat.com)
 111:  * @author Ingo Proetel (proetel@aicas.com)
 112:  * @author Jerry Quinn (jlquinn@optonline.net)
 113:  * @author Jeroen Frijters (jeroen@frijters.net)
 114:  * @author Noa Resare (noa@resare.com)
 115:  * @author Sven de Marothy (sven@physto.se)
 116:  * @author David Gilbert (david.gilbert@object-refinery.com)
 117:  * @author Olivier Jolly (olivier.jolly@pcedev.com)
 118:  * @author Andrew John Hughes (gnu_andrew@member.fsf.org)
 119:  * @see Date
 120:  * @see GregorianCalendar
 121:  * @see TimeZone
 122:  * @see java.text.DateFormat
 123:  */
 124: public abstract class Calendar
 125:   implements Serializable, Cloneable, Comparable<Calendar>
 126: {
 127:   /**
 128:    * Constant representing the era time field.
 129:    */
 130:   public static final int ERA = 0;
 131: 
 132:   /**
 133:    * Constant representing the year time field.
 134:    */
 135:   public static final int YEAR = 1;
 136: 
 137:   /**
 138:    * Constant representing the month time field.  This field
 139:    * should contain one of the JANUARY,...,DECEMBER constants below.
 140:    */
 141:   public static final int MONTH = 2;
 142: 
 143:   /**
 144:    * Constant representing the week of the year field.
 145:    * @see #setFirstDayOfWeek(int)
 146:    */
 147:   public static final int WEEK_OF_YEAR = 3;
 148: 
 149:   /**
 150:    * Constant representing the week of the month time field.
 151:    * @see #setFirstDayOfWeek(int)
 152:    */
 153:   public static final int WEEK_OF_MONTH = 4;
 154: 
 155:   /**
 156:    * Constant representing the day time field, synonym for DAY_OF_MONTH.
 157:    */
 158:   public static final int DATE = 5;
 159: 
 160:   /**
 161:    * Constant representing the day time field.
 162:    */
 163:   public static final int DAY_OF_MONTH = 5;
 164: 
 165:   /**
 166:    * Constant representing the day of year time field.  This is
 167:    * 1 for the first day in month.
 168:    */
 169:   public static final int DAY_OF_YEAR = 6;
 170: 
 171:   /**
 172:    * Constant representing the day of week time field.  This field
 173:    * should contain one of the SUNDAY,...,SATURDAY constants below.
 174:    */
 175:   public static final int DAY_OF_WEEK = 7;
 176: 
 177:   /**
 178:    * Constant representing the day-of-week-in-month field.  For
 179:    * instance this field contains 2 for the second thursday in a
 180:    * month.  If you give a negative number here, the day will count
 181:    * from the end of the month.
 182:    */
 183:   public static final int DAY_OF_WEEK_IN_MONTH = 8;
 184: 
 185:   /**
 186:    * Constant representing the part of the day for 12-hour clock.  This
 187:    * should be one of AM or PM.
 188:    */
 189:   public static final int AM_PM = 9;
 190: 
 191:   /**
 192:    * Constant representing the hour time field for 12-hour clock.
 193:    */
 194:   public static final int HOUR = 10;
 195: 
 196:   /**
 197:    * Constant representing the hour of day time field for 24-hour clock.
 198:    */
 199:   public static final int HOUR_OF_DAY = 11;
 200: 
 201:   /**
 202:    * Constant representing the minute of hour time field.
 203:    */
 204:   public static final int MINUTE = 12;
 205: 
 206:   /**
 207:    * Constant representing the second time field.
 208:    */
 209:   public static final int SECOND = 13;
 210: 
 211:   /**
 212:    * Constant representing the millisecond time field.
 213:    */
 214:   public static final int MILLISECOND = 14;
 215: 
 216:   /**
 217:    * Constant representing the time zone offset time field for the
 218:    * time given in the other fields.  It is measured in
 219:    * milliseconds.  The default is the offset of the time zone.
 220:    */
 221:   public static final int ZONE_OFFSET = 15;
 222: 
 223:   /**
 224:    * Constant representing the daylight saving time offset in
 225:    * milliseconds.  The default is the value given by the time zone.
 226:    */
 227:   public static final int DST_OFFSET = 16;
 228: 
 229:   /**
 230:    * Number of time fields.
 231:    */
 232:   public static final int FIELD_COUNT = 17;
 233: 
 234:   /**
 235:    * Constant representing Sunday.
 236:    */
 237:   public static final int SUNDAY = 1;
 238: 
 239:   /**
 240:    * Constant representing Monday.
 241:    */
 242:   public static final int MONDAY = 2;
 243: 
 244:   /**
 245:    * Constant representing Tuesday.
 246:    */
 247:   public static final int TUESDAY = 3;
 248: 
 249:   /**
 250:    * Constant representing Wednesday.
 251:    */
 252:   public static final int WEDNESDAY = 4;
 253: 
 254:   /**
 255:    * Constant representing Thursday.
 256:    */
 257:   public static final int THURSDAY = 5;
 258: 
 259:   /**
 260:    * Constant representing Friday.
 261:    */
 262:   public static final int FRIDAY = 6;
 263: 
 264:   /**
 265:    * Constant representing Saturday.
 266:    */
 267:   public static final int SATURDAY = 7;
 268: 
 269:   /**
 270:    * Constant representing January.
 271:    */
 272:   public static final int JANUARY = 0;
 273: 
 274:   /**
 275:    * Constant representing February.
 276:    */
 277:   public static final int FEBRUARY = 1;
 278: 
 279:   /**
 280:    * Constant representing March.
 281:    */
 282:   public static final int MARCH = 2;
 283: 
 284:   /**
 285:    * Constant representing April.
 286:    */
 287:   public static final int APRIL = 3;
 288: 
 289:   /**
 290:    * Constant representing May.
 291:    */
 292:   public static final int MAY = 4;
 293: 
 294:   /**
 295:    * Constant representing June.
 296:    */
 297:   public static final int JUNE = 5;
 298: 
 299:   /**
 300:    * Constant representing July.
 301:    */
 302:   public static final int JULY = 6;
 303: 
 304:   /**
 305:    * Constant representing August.
 306:    */
 307:   public static final int AUGUST = 7;
 308: 
 309:   /**
 310:    * Constant representing September.
 311:    */
 312:   public static final int SEPTEMBER = 8;
 313: 
 314:   /**
 315:    * Constant representing October.
 316:    */
 317:   public static final int OCTOBER = 9;
 318: 
 319:   /**
 320:    * Constant representing November.
 321:    */
 322:   public static final int NOVEMBER = 10;
 323: 
 324:   /**
 325:    * Constant representing December.
 326:    */
 327:   public static final int DECEMBER = 11;
 328: 
 329:   /**
 330:    * Constant representing Undecimber. This is an artificial name useful
 331:    * for lunar calendars.
 332:    */
 333:   public static final int UNDECIMBER = 12;
 334: 
 335:   /**
 336:    * Useful constant for 12-hour clock.
 337:    */
 338:   public static final int AM = 0;
 339: 
 340:   /**
 341:    * Useful constant for 12-hour clock.
 342:    */
 343:   public static final int PM = 1;
 344: 
 345:   /**
 346:    * A style specifier for {@link #getDisplayNames(int,int,Locale)}
 347:    * stating that names should be returned in both long and short variants.
 348:    *
 349:    * @since 1.6
 350:    * @see #SHORT
 351:    * @see #LONG
 352:    */
 353:   public static final int ALL_STYLES = 0;
 354: 
 355:   /**
 356:    * A style specifier for {@link #getDisplayName(int,int,Locale)}
 357:    * and {@link #getDisplayNames(int,int,Locale)} stating that names
 358:    * should be returned in their short variant if applicable.
 359:    *
 360:    * @since 1.6
 361:    */
 362:   public static final int SHORT = 1;
 363: 
 364:   /**
 365:    * A style specifier for {@link #getDisplayName(int,int,Locale)}
 366:    * and {@link #getDisplayNames(int,int,Locale)} stating that names
 367:    * should be returned in their long variant if applicable.
 368:    *
 369:    * @since 1.6
 370:    */
 371:   public static final int LONG = 2;
 372: 
 373:   /**
 374:    * The time fields.  The array is indexed by the constants YEAR to
 375:    * DST_OFFSET.
 376:    * @serial
 377:    */
 378:   protected int[] fields = new int[FIELD_COUNT];
 379: 
 380:   /**
 381:    * The flags which tell if the fields above have a value.
 382:    * @serial
 383:    */
 384:   protected boolean[] isSet = new boolean[FIELD_COUNT];
 385: 
 386:   /**
 387:    * The time in milliseconds since the epoch.
 388:    * @serial
 389:    */
 390:   protected long time;
 391: 
 392:   /**
 393:    * Tells if the above field has a valid value.
 394:    * @serial
 395:    */
 396:   protected boolean isTimeSet;
 397: 
 398:   /**
 399:    * Tells if the fields have a valid value.  This superseeds the isSet
 400:    * array.
 401:    * @serial
 402:    */
 403:   protected boolean areFieldsSet;
 404: 
 405:   /**
 406:    * The time zone of this calendar.  Used by sub classes to do UTC / local
 407:    * time conversion.  Sub classes can access this field with getTimeZone().
 408:    * @serial
 409:    */
 410:   private TimeZone zone;
 411: 
 412:   /**
 413:    * This is the default calendar class, that is returned on
 414:    * java.util.Calendar.getInstance().
 415:    * XXX - this isn't localized anywhere, is it?
 416:    * @see java.util.Calendar#getInstance()
 417:    */
 418:   private static final String calendarClassName = "java.util.GregorianCalendar";
 419: 
 420:   /**
 421:    * Specifies if the date/time interpretation should be lenient.
 422:    * If the flag is set, a date such as "February 30, 1996" will be
 423:    * treated as the 29th day after the February 1.  If this flag
 424:    * is false, such dates will cause an exception.
 425:    * @serial
 426:    */
 427:   private boolean lenient;
 428: 
 429:   /**
 430:    * Sets what the first day of week is.  This is used for
 431:    * WEEK_OF_MONTH and WEEK_OF_YEAR fields.
 432:    * @serial
 433:    */
 434:   private int firstDayOfWeek;
 435: 
 436:   /**
 437:    * Sets how many days are required in the first week of the year.
 438:    * If the first day of the year should be the first week you should
 439:    * set this value to 1.  If the first week must be a full week, set
 440:    * it to 7.
 441:    * @serial
 442:    */
 443:   private int minimalDaysInFirstWeek;
 444: 
 445:   /**
 446:    * Is set to true if DST_OFFSET is explicitly set. In that case
 447:    * it's value overrides the value computed from the current
 448:    * time and the timezone.
 449:    */
 450:   private boolean explicitDSTOffset = false;
 451: 
 452:   /**
 453:    * The version of the serialized data on the stream.
 454:    * <dl><dt>0 or not present</dt>
 455:    * <dd> JDK 1.1.5 or later.</dd>
 456:    * <dt>1</dt>
 457:    * <dd>JDK 1.1.6 or later.  This always writes a correct `time' value
 458:    * on the stream, as well as the other fields, to be compatible with
 459:    * earlier versions</dd></dl>
 460:    * @since JDK1.1.6
 461:    * @serial
 462:    */
 463:   private int serialVersionOnStream = 1;
 464: 
 465:   /**
 466:    * XXX - I have not checked the compatibility.  The documentation of
 467:    * the serialized-form is quite hairy...
 468:    */
 469:   static final long serialVersionUID = -1807547505821590642L;
 470: 
 471:   /**
 472:    * The name of the resource bundle. Used only by getBundle()
 473:    */
 474:   private static final String bundleName = "gnu.java.locale.LocaleInformation";
 475: 
 476:   /**
 477:    * get resource bundle:
 478:    * The resources should be loaded via this method only. Iff an application
 479:    * uses this method, the resourcebundle is required.
 480:    */
 481:   private static ResourceBundle getBundle(Locale locale)
 482:   {
 483:     return ResourceBundle.getBundle(bundleName, locale,
 484:                                     ClassLoader.getSystemClassLoader());
 485:   }
 486: 
 487:   /**
 488:    * Constructs a new Calendar with the default time zone and the default
 489:    * locale.
 490:    */
 491:   protected Calendar()
 492:   {
 493:     this(TimeZone.getDefault(), Locale.getDefault());
 494:   }
 495: 
 496:   /**
 497:    * Constructs a new Calendar with the given time zone and the given
 498:    * locale.
 499:    * @param zone a time zone.
 500:    * @param locale a locale.
 501:    */
 502:   protected Calendar(TimeZone zone, Locale locale)
 503:   {
 504:     this.zone = zone;
 505:     lenient = true;
 506:     String[] days = { "", "sun", "mon", "tue", "wed", "thu", "fri", "sat" };
 507: 
 508:     ResourceBundle rb = getBundle(locale);
 509:     String min = (String) rb.getObject("minNumberOfDaysInFirstWeek");
 510:     String first = (String) rb.getObject("firstDayOfWeek");
 511:     try
 512:       {
 513:     if (min != null)
 514:       minimalDaysInFirstWeek = Integer.parseInt(min);
 515:       }
 516:     catch (NumberFormatException ex)
 517:       {
 518:     minimalDaysInFirstWeek = 1;
 519:       }
 520: 
 521:     firstDayOfWeek = 1;
 522:     if (first != null)
 523:       for (int i = 0; i < 8; i++)
 524:     if (days[i].equals(first))
 525:       firstDayOfWeek = i;
 526: 
 527:     clear();
 528:   }
 529: 
 530:   /**
 531:    * Creates a calendar representing the actual time, using the default
 532:    * time zone and locale.
 533:    * 
 534:    * @return The new calendar.
 535:    */
 536:   public static synchronized Calendar getInstance()
 537:   {
 538:     return getInstance(TimeZone.getDefault(), Locale.getDefault());
 539:   }
 540: 
 541:   /**
 542:    * Creates a calendar representing the actual time, using the given
 543:    * time zone and the default locale.
 544:    * 
 545:    * @param zone a time zone (<code>null</code> not permitted).
 546:    * 
 547:    * @return The new calendar.
 548:    * 
 549:    * @throws NullPointerException if <code>zone</code> is <code>null</code>.
 550:    */
 551:   public static synchronized Calendar getInstance(TimeZone zone)
 552:   {
 553:     return getInstance(zone, Locale.getDefault());
 554:   }
 555: 
 556:   /**
 557:    * Creates a calendar representing the actual time, using the default
 558:    * time zone and the given locale.
 559:    * 
 560:    * @param locale a locale (<code>null</code> not permitted).
 561:    * 
 562:    * @return The new calendar.
 563:    * 
 564:    * @throws NullPointerException if <code>locale</code> is <code>null</code>.
 565:    */
 566:   public static synchronized Calendar getInstance(Locale locale)
 567:   {
 568:     return getInstance(TimeZone.getDefault(), locale);
 569:   }
 570: 
 571:   /**
 572:    * Cache of locale->calendar-class mappings. This avoids having to do a ResourceBundle
 573:    * lookup for every getInstance call.
 574:    */
 575:   private static HashMap<Locale,Class> cache = new HashMap<Locale,Class>();
 576: 
 577:   /** Preset argument types for calendar-class constructor lookup.  */
 578:   private static Class[] ctorArgTypes = new Class[]
 579:                                         {
 580:                                           TimeZone.class, Locale.class
 581:                                         };
 582: 
 583:   /**
 584:    * Creates a calendar representing the actual time, using the given
 585:    * time zone and locale.
 586:    * 
 587:    * @param zone a time zone (<code>null</code> not permitted).
 588:    * @param locale a locale (<code>null</code> not permitted).
 589:    * 
 590:    * @return The new calendar.
 591:    * 
 592:    * @throws NullPointerException if <code>zone</code> or <code>locale</code>
 593:    *     is <code>null</code>.
 594:    */
 595:   public static synchronized Calendar getInstance(TimeZone zone, Locale locale)
 596:   {
 597:     Class calendarClass = cache.get(locale);
 598:     Throwable exception = null;
 599: 
 600:     try
 601:       {
 602:     if (calendarClass == null)
 603:       {
 604:         calendarClass = Class.forName(calendarClassName);
 605:         if (Calendar.class.isAssignableFrom(calendarClass))
 606:           cache.put(locale, calendarClass);
 607:       }
 608: 
 609:     // GregorianCalendar is by far the most common case. Optimize by 
 610:     // avoiding reflection.
 611:     if (calendarClass == GregorianCalendar.class)
 612:       return new GregorianCalendar(zone, locale);
 613: 
 614:     if (Calendar.class.isAssignableFrom(calendarClass))
 615:       {
 616:         Constructor ctor = calendarClass.getConstructor(ctorArgTypes);
 617:         return (Calendar) ctor.newInstance(new Object[] { zone, locale });
 618:       }
 619:       }
 620:     catch (ClassNotFoundException ex)
 621:       {
 622:     exception = ex;
 623:       }
 624:     catch (IllegalAccessException ex)
 625:       {
 626:     exception = ex;
 627:       }
 628:     catch (NoSuchMethodException ex)
 629:       {
 630:     exception = ex;
 631:       }
 632:     catch (InstantiationException ex)
 633:       {
 634:     exception = ex;
 635:       }
 636:     catch (InvocationTargetException ex)
 637:       {
 638:     exception = ex;
 639:       }
 640: 
 641:     throw new RuntimeException("Error instantiating calendar for locale "
 642:                                + locale, exception);
 643:   }
 644: 
 645:   /**
 646:    * Gets the set of locales for which a Calendar is available.
 647:    * @exception MissingResourceException if locale data couldn't be found.
 648:    * @return the set of locales.
 649:    */
 650:   public static synchronized Locale[] getAvailableLocales()
 651:   {
 652:     ResourceBundle rb = getBundle(new Locale("", ""));
 653:     return (Locale[]) rb.getObject("availableLocales");
 654:   }
 655: 
 656:   /**
 657:    * Converts the time field values (<code>fields</code>) to
 658:    * milliseconds since the epoch UTC (<code>time</code>).  Override
 659:    * this method if you write your own Calendar.  */
 660:   protected abstract void computeTime();
 661: 
 662:   /**
 663:    * Converts the milliseconds since the epoch UTC
 664:    * (<code>time</code>) to time fields
 665:    * (<code>fields</code>). Override this method if you write your
 666:    * own Calendar.
 667:    */
 668:   protected abstract void computeFields();
 669: 
 670:   /**
 671:    * Converts the time represented by this object to a
 672:    * <code>Date</code>-Object.
 673:    * @return the Date.
 674:    */
 675:   public final Date getTime()
 676:   {
 677:     if (! isTimeSet)
 678:       computeTime();
 679:     return new Date(time);
 680:   }
 681: 
 682:   /**
 683:    * Sets this Calendar's time to the given Date.  All time fields
 684:    * are invalidated by this method.
 685:    * 
 686:    * @param date  the date (<code>null</code> not permitted).
 687:    * 
 688:    * @throws NullPointerException if <code>date</code> is <code>null</code>.
 689:    */
 690:   public final void setTime(Date date)
 691:   {
 692:     setTimeInMillis(date.getTime());
 693:   }
 694: 
 695:   /**
 696:    * Returns the time represented by this Calendar.
 697:    * @return the time in milliseconds since the epoch.
 698:    * @specnote This was made public in 1.4.
 699:    */
 700:   public long getTimeInMillis()
 701:   {
 702:     if (! isTimeSet)
 703:       computeTime();
 704:     return time;
 705:   }
 706: 
 707:   /**
 708:    * Sets this Calendar's time to the given Time.  All time fields
 709:    * are invalidated by this method.
 710:    * @param time the time in milliseconds since the epoch
 711:    * @specnote This was made public in 1.4.
 712:    */
 713:   public void setTimeInMillis(long time)
 714:   {
 715:     clear();
 716:     this.time = time;
 717:     isTimeSet = true;
 718:     computeFields();
 719:   }
 720: 
 721:   /**
 722:    * Gets the value of the specified field.  They are recomputed
 723:    * if they are invalid.
 724:    * @param field the time field. One of the time field constants.
 725:    * @return the value of the specified field
 726:    * @throws ArrayIndexOutOfBoundsException if the field is outside
 727:    *         the valid range.  The value of field must be >= 0 and
 728:    *         <= <code>FIELD_COUNT</code>.
 729:    * @specnote Not final since JDK 1.4
 730:    */
 731:   public int get(int field)
 732:   {
 733:     // If the requested field is invalid, force all fields to be recomputed.
 734:     if (! isSet[field])
 735:       areFieldsSet = false;
 736:     complete();
 737:     return fields[field];
 738:   }
 739: 
 740:   /**
 741:    * Gets the value of the specified field. This method doesn't
 742:    * recompute the fields, if they are invalid.
 743:    * @param field the time field. One of the time field constants.
 744:    * @return the value of the specified field, undefined if
 745:    * <code>areFieldsSet</code> or <code>isSet[field]</code> is false.
 746:    * @throws ArrayIndexOutOfBoundsException if the field is outside
 747:    *         the valid range.  The value of field must be >= 0 and
 748:    *         <= <code>FIELD_COUNT</code>.
 749:    */
 750:   protected final int internalGet(int field)
 751:   {
 752:     return fields[field];
 753:   }
 754: 
 755:   /**
 756:    * Sets the time field with the given value.  This does invalidate
 757:    * the time in milliseconds.
 758:    * @param field the time field. One of the time field constants
 759:    * @param value the value to be set.
 760:    * @throws ArrayIndexOutOfBoundsException if field is outside
 761:    *         the valid range.  The value of field must be >= 0 and
 762:    *         <= <code>FIELD_COUNT</code>.
 763:    * @specnote Not final since JDK 1.4
 764:    */
 765:   public void set(int field, int value)
 766:   {
 767:     if (isTimeSet)
 768:       for (int i = 0; i < FIELD_COUNT; i++)
 769:     isSet[i] = false;
 770:     isTimeSet = false;
 771:     fields[field] = value;
 772:     isSet[field] = true;
 773: 
 774:     // The five valid date patterns, in order of priority
 775:     // 1  YEAR + MONTH + DAY_OF_MONTH
 776:     // 2  YEAR + MONTH + WEEK_OF_MONTH + DAY_OF_WEEK
 777:     // 3  YEAR + MONTH + DAY_OF_WEEK_IN_MONTH + DAY_OF_WEEK
 778:     // 4  YEAR + DAY_OF_YEAR
 779:     // 5  YEAR + DAY_OF_WEEK + WEEK_OF_YEAR
 780:     switch (field)
 781:       {
 782:       case MONTH: // pattern 1,2 or 3
 783:     isSet[DAY_OF_YEAR] = false;
 784:     isSet[WEEK_OF_YEAR] = false;
 785:     break;
 786:       case DAY_OF_MONTH: // pattern 1
 787:     isSet[YEAR] = true;
 788:     isSet[MONTH] = true;
 789:     isSet[WEEK_OF_MONTH] = true;
 790:     isSet[DAY_OF_WEEK] = false;
 791:     isSet[DAY_OF_WEEK_IN_MONTH] = false;
 792:     isSet[DAY_OF_YEAR] = false;
 793:     isSet[WEEK_OF_YEAR] = false;
 794:     break;
 795:       case WEEK_OF_MONTH: // pattern 2
 796:     if (! isSet[DAY_OF_WEEK])
 797:       fields[DAY_OF_WEEK] = getFirstDayOfWeek();
 798:     isSet[YEAR] = true;
 799:     isSet[MONTH] = true;
 800:     isSet[DAY_OF_WEEK] = true;
 801:     isSet[DAY_OF_MONTH] = false;
 802:     isSet[DAY_OF_WEEK_IN_MONTH] = false;
 803:     isSet[DAY_OF_YEAR] = false;
 804:     isSet[WEEK_OF_YEAR] = false;
 805:     break;
 806:       case DAY_OF_WEEK_IN_MONTH: // pattern 3
 807:     if (! isSet[DAY_OF_WEEK])
 808:       fields[DAY_OF_WEEK] = getFirstDayOfWeek();
 809:     isSet[YEAR] = true;
 810:     isSet[MONTH] = true;
 811:     isSet[DAY_OF_WEEK] = true;
 812:     isSet[DAY_OF_YEAR] = false;
 813:     isSet[DAY_OF_MONTH] = false;
 814:     isSet[WEEK_OF_MONTH] = false;
 815:     isSet[WEEK_OF_YEAR] = false;
 816:     break;
 817:       case DAY_OF_YEAR: // pattern 4
 818:     isSet[YEAR] = true;
 819:     isSet[MONTH] = false;
 820:     isSet[WEEK_OF_MONTH] = false;
 821:     isSet[DAY_OF_MONTH] = false;
 822:     isSet[DAY_OF_WEEK] = false;
 823:     isSet[WEEK_OF_YEAR] = false;
 824:     isSet[DAY_OF_WEEK_IN_MONTH] = false;
 825:     break;
 826:       case WEEK_OF_YEAR: // pattern 5
 827:     if (! isSet[DAY_OF_WEEK])
 828:       fields[DAY_OF_WEEK] = getFirstDayOfWeek();
 829:     isSet[YEAR] = true;
 830:     isSet[DAY_OF_WEEK] = true;
 831:     isSet[MONTH] = false;
 832:     isSet[DAY_OF_MONTH] = false;
 833:     isSet[WEEK_OF_MONTH] = false;
 834:     isSet[DAY_OF_YEAR] = false;
 835:     isSet[DAY_OF_WEEK_IN_MONTH] = false;
 836:     break;
 837:       case AM_PM:
 838:     isSet[HOUR] = true;
 839:     isSet[HOUR_OF_DAY] = false;
 840:     break;
 841:       case HOUR_OF_DAY:
 842:     isSet[AM_PM] = false;
 843:     isSet[HOUR] = false;
 844:     break;
 845:       case HOUR:
 846:     isSet[AM_PM] = true;
 847:     isSet[HOUR_OF_DAY] = false;
 848:     break;
 849:       case DST_OFFSET:
 850:     explicitDSTOffset = true;
 851:       }
 852: 
 853:     // May have crossed over a DST boundary.
 854:     if (! explicitDSTOffset && (field != DST_OFFSET && field != ZONE_OFFSET))
 855:       isSet[DST_OFFSET] = false;
 856:   }
 857: 
 858:   /**
 859:    * Sets the fields for year, month, and date
 860:    * @param year the year.
 861:    * @param month the month, one of the constants JANUARY..UNDICEMBER.
 862:    * @param date the day of the month
 863:    */
 864:   public final void set(int year, int month, int date)
 865:   {
 866:     isTimeSet = false;
 867:     fields[YEAR] = year;
 868:     fields[MONTH] = month;
 869:     fields[DATE] = date;
 870:     isSet[YEAR] = isSet[MONTH] = isSet[DATE] = true;
 871:     isSet[WEEK_OF_YEAR] = false;
 872:     isSet[DAY_OF_YEAR] = false;
 873:     isSet[WEEK_OF_MONTH] = false;
 874:     isSet[DAY_OF_WEEK] = false;
 875:     isSet[DAY_OF_WEEK_IN_MONTH] = false;
 876:     isSet[ERA] = false;
 877: 
 878:     if (! explicitDSTOffset)
 879:       isSet[DST_OFFSET] = false; // May have crossed a DST boundary.
 880:   }
 881: 
 882:   /**
 883:    * Sets the fields for year, month, date, hour, and minute
 884:    * @param year the year.
 885:    * @param month the month, one of the constants JANUARY..UNDICEMBER.
 886:    * @param date the day of the month
 887:    * @param hour the hour of day.
 888:    * @param minute the minute.
 889:    */
 890:   public final void set(int year, int month, int date, int hour, int minute)
 891:   {
 892:     set(year, month, date);
 893:     fields[HOUR_OF_DAY] = hour;
 894:     fields[MINUTE] = minute;
 895:     isSet[HOUR_OF_DAY] = isSet[MINUTE] = true;
 896:     isSet[AM_PM] = false;
 897:     isSet[HOUR] = false;
 898:   }
 899: 
 900:   /**
 901:    * Sets the fields for year, month, date, hour, and minute
 902:    * @param year the year.
 903:    * @param month the month, one of the constants JANUARY..UNDICEMBER.
 904:    * @param date the day of the month
 905:    * @param hour the hour of day.