Source for java.text.SimpleDateFormat

   1: /* SimpleDateFormat.java -- A class for parsing/formating simple 
   2:    date constructs
   3:    Copyright (C) 1998, 1999, 2000, 2001, 2003, 2004, 2005
   4:    Free Software Foundation, Inc.
   5: 
   6: This file is part of GNU Classpath.
   7: 
   8: GNU Classpath is free software; you can redistribute it and/or modify
   9: it under the terms of the GNU General Public License as published by
  10: the Free Software Foundation; either version 2, or (at your option)
  11: any later version.
  12:  
  13: GNU Classpath is distributed in the hope that it will be useful, but
  14: WITHOUT ANY WARRANTY; without even the implied warranty of
  15: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  16: General Public License for more details.
  17: 
  18: You should have received a copy of the GNU General Public License
  19: along with GNU Classpath; see the file COPYING.  If not, write to the
  20: Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  21: 02110-1301 USA.
  22: 
  23: Linking this library statically or dynamically with other modules is
  24: making a combined work based on this library.  Thus, the terms and
  25: conditions of the GNU General Public License cover the whole
  26: combination.
  27: 
  28: As a special exception, the copyright holders of this library give you
  29: permission to link this library with independent modules to produce an
  30: executable, regardless of the license terms of these independent
  31: modules, and to copy and distribute the resulting executable under
  32: terms of your choice, provided that you also meet, for each linked
  33: independent module, the terms and conditions of the license of that
  34: module.  An independent module is a module which is not derived from
  35: or based on this library.  If you modify this library, you may extend
  36: this exception to your version of the library, but you are not
  37: obligated to do so.  If you do not wish to do so, delete this
  38: exception statement from your version. */
  39: 
  40: 
  41: package java.text;
  42: 
  43: import gnu.java.text.AttributedFormatBuffer;
  44: import gnu.java.text.FormatBuffer;
  45: import gnu.java.text.FormatCharacterIterator;
  46: import gnu.java.text.StringFormatBuffer;
  47: 
  48: import java.io.IOException;
  49: import java.io.InvalidObjectException;
  50: import java.io.ObjectInputStream;
  51: import java.util.ArrayList;
  52: import java.util.Calendar;
  53: import java.util.Date;
  54: import java.util.GregorianCalendar;
  55: import java.util.Iterator;
  56: import java.util.Locale;
  57: import java.util.TimeZone;
  58: import java.util.regex.Matcher;
  59: import java.util.regex.Pattern;
  60: 
  61: /**
  62:  * SimpleDateFormat provides convenient methods for parsing and formatting
  63:  * dates using Gregorian calendars (see java.util.GregorianCalendar). 
  64:  */
  65: public class SimpleDateFormat extends DateFormat 
  66: {
  67:   /** 
  68:    * This class is used by <code>SimpleDateFormat</code> as a
  69:    * compiled representation of a format string.  The field
  70:    * ID, size, and character used are stored for each sequence
  71:    * of pattern characters.
  72:    */
  73:   private class CompiledField
  74:   {
  75:     /**
  76:      * The ID of the field within the local pattern characters.
  77:      * Package private for use in out class.
  78:      */
  79:     int field;
  80: 
  81:     /**
  82:      * The size of the character sequence.
  83:      * Package private for use in out class.
  84:      */
  85:     int size;
  86: 
  87:     /**
  88:      * The character used.
  89:      */
  90:     private char character;
  91: 
  92:     /** 
  93:      * Constructs a compiled field using the
  94:      * the given field ID, size and character
  95:      * values.
  96:      *
  97:      * @param f the field ID.
  98:      * @param s the size of the field.
  99:      * @param c the character used.
 100:      */
 101:     public CompiledField(int f, int s, char c)
 102:     {
 103:       field = f;
 104:       size = s;
 105:       character = c;
 106:     }
 107: 
 108:     /**
 109:      * Retrieves the ID of the field relative to
 110:      * the local pattern characters.
 111:      */
 112:     public int getField()
 113:     {
 114:       return field;
 115:     }
 116: 
 117:     /**
 118:      * Retrieves the size of the character sequence.
 119:      */
 120:     public int getSize()
 121:     {
 122:       return size;
 123:     }
 124: 
 125:     /**
 126:      * Retrieves the character used in the sequence.
 127:      */
 128:     public char getCharacter()
 129:     {
 130:       return character;
 131:     }
 132: 
 133:     /**
 134:      * Returns a <code>String</code> representation
 135:      * of the compiled field, primarily for debugging
 136:      * purposes.
 137:      *
 138:      * @return a <code>String</code> representation.
 139:      */
 140:     public String toString()
 141:     {
 142:       StringBuffer builder;
 143: 
 144:       builder = new StringBuffer(getClass().getName());
 145:       builder.append("[field=");
 146:       builder.append(field);
 147:       builder.append(", size=");
 148:       builder.append(size);
 149:       builder.append(", character=");
 150:       builder.append(character);
 151:       builder.append("]");
 152: 
 153:       return builder.toString();
 154:     }
 155:   }
 156: 
 157:   /**
 158:    * A list of <code>CompiledField</code>s,
 159:    * representing the compiled version of the pattern.
 160:    *
 161:    * @see CompiledField
 162:    * @serial Ignored.
 163:    */
 164:   private transient ArrayList tokens;
 165: 
 166:   /**
 167:    * The localised data used in formatting,
 168:    * such as the day and month names in the local
 169:    * language, and the localized pattern characters.
 170:    *
 171:    * @see DateFormatSymbols
 172:    * @serial The localisation data.  May not be null.
 173:    */
 174:   private DateFormatSymbols formatData;
 175: 
 176:   /**
 177:    * The date representing the start of the century
 178:    * used for interpreting two digit years.  For
 179:    * example, 24/10/2004 would cause two digit
 180:    * years to be interpreted as representing
 181:    * the years between 2004 and 2104.
 182:    *
 183:    * @see get2DigitYearStart()
 184:    * @see set2DigitYearStart(java.util.Date)
 185:    * @see Date
 186:    * @serial The start date of the century for parsing two digit years.
 187:    *         May not be null.
 188:    */
 189:   private Date defaultCenturyStart;
 190: 
 191:   /**
 192:    * The year at which interpretation of two
 193:    * digit years starts.
 194:    *
 195:    * @see get2DigitYearStart()
 196:    * @see set2DigitYearStart(java.util.Date)
 197:    * @serial Ignored.
 198:    */
 199:   private transient int defaultCentury;
 200: 
 201:   /**
 202:    * The non-localized pattern string.  This
 203:    * only ever contains the pattern characters
 204:    * stored in standardChars.  Localized patterns
 205:    * are translated to this form.
 206:    *
 207:    * @see applyPattern(String)
 208:    * @see applyLocalizedPattern(String)
 209:    * @see toPattern()
 210:    * @see toLocalizedPattern()
 211:    * @serial The non-localized pattern string.  May not be null.
 212:    */
 213:   private String pattern;
 214: 
 215:   /**
 216:    * The version of serialized data used by this class.
 217:    * Version 0 only includes the pattern and formatting
 218:    * data.  Version 1 adds the start date for interpreting
 219:    * two digit years.
 220:    *
 221:    * @serial This specifies the version of the data being serialized.
 222:    *         Version 0 (or no version) specifies just <code>pattern</code>
 223:    *         and <code>formatData</code>.  Version 1 adds
 224:    *         the <code>defaultCenturyStart</code>.  This implementation
 225:    *         always writes out version 1 data.
 226:    */
 227:   private int serialVersionOnStream = 1; // 0 indicates JDK1.1.3 or earlier
 228: 
 229:   /**
 230:    * For compatability.
 231:    */
 232:   private static final long serialVersionUID = 4774881970558875024L;
 233: 
 234:   // This string is specified in the root of the CLDR.  We set it here
 235:   // rather than doing a DateFormatSymbols(Locale.US).getLocalPatternChars()
 236:   // since someone could theoretically change those values (though unlikely).
 237:   private static final String standardChars = "GyMdkHmsSEDFwWahKzYeugAZ";
 238: 
 239:   /**
 240:    * Reads the serialized version of this object.
 241:    * If the serialized data is only version 0,
 242:    * then the date for the start of the century
 243:    * for interpreting two digit years is computed.
 244:    * The pattern is parsed and compiled following the process
 245:    * of reading in the serialized data.
 246:    *
 247:    * @param stream the object stream to read the data from.
 248:    * @throws IOException if an I/O error occurs.
 249:    * @throws ClassNotFoundException if the class of the serialized data
 250:    *         could not be found.
 251:    * @throws InvalidObjectException if the pattern is invalid.
 252:    */ 
 253:   private void readObject(ObjectInputStream stream)
 254:     throws IOException, ClassNotFoundException
 255:   {
 256:     stream.defaultReadObject();
 257:     if (serialVersionOnStream < 1)
 258:       {
 259:         computeCenturyStart ();
 260:     serialVersionOnStream = 1;
 261:       }
 262:     else
 263:       // Ensure that defaultCentury gets set.
 264:       set2DigitYearStart(defaultCenturyStart);
 265: 
 266:     // Set up items normally taken care of by the constructor.
 267:     tokens = new ArrayList();
 268:     try
 269:       {
 270:     compileFormat(pattern);
 271:       }
 272:     catch (IllegalArgumentException e)
 273:       {
 274:     throw new InvalidObjectException("The stream pattern was invalid.");
 275:       }
 276:   }
 277: 
 278:   /**
 279:    * Compiles the supplied non-localized pattern into a form
 280:    * from which formatting and parsing can be performed.
 281:    * This also detects errors in the pattern, which will
 282:    * be raised on later use of the compiled data.
 283:    *
 284:    * @param pattern the non-localized pattern to compile.
 285:    * @throws IllegalArgumentException if the pattern is invalid.
 286:    */
 287:   private void compileFormat(String pattern) 
 288:   {
 289:     // Any alphabetical characters are treated as pattern characters
 290:     // unless enclosed in single quotes.
 291: 
 292:     char thisChar;
 293:     int pos;
 294:     int field;
 295:     CompiledField current = null;
 296: 
 297:     for (int i=0; i<pattern.length(); i++) {
 298:       thisChar = pattern.charAt(i);
 299:       field = standardChars.indexOf(thisChar);
 300:       if (field == -1) {
 301:     current = null;
 302:     if ((thisChar >= 'A' && thisChar <= 'Z')
 303:         || (thisChar >= 'a' && thisChar <= 'z')) {
 304:        // Not a valid letter
 305:       throw new IllegalArgumentException("Invalid letter " + thisChar +
 306:                          "encountered at character " + i
 307:                          + ".");
 308:     } else if (thisChar == '\'') {
 309:       // Quoted text section; skip to next single quote
 310:       pos = pattern.indexOf('\'',i+1);
 311:       if (pos == -1) {
 312:         throw new IllegalArgumentException("Quotes starting at character "
 313:                            + i + " not closed.");
 314:       }
 315:       if ((pos+1 < pattern.length()) && (pattern.charAt(pos+1) == '\'')) {
 316:         tokens.add(pattern.substring(i+1,pos+1));
 317:       } else {
 318:         tokens.add(pattern.substring(i+1,pos));
 319:       }
 320:       i = pos;
 321:     } else {
 322:       // A special character
 323:       tokens.add(new Character(thisChar));
 324:     }
 325:       } else {
 326:     // A valid field
 327:     if ((current != null) && (field == current.field)) {
 328:       current.size++;
 329:     } else {
 330:       current = new CompiledField(field,1,thisChar);
 331:       tokens.add(current);
 332:     }
 333:       }
 334:     }
 335:   }
 336: 
 337:   /**
 338:    * Returns a string representation of this
 339:    * class.
 340:    *
 341:    * @return a string representation of the <code>SimpleDateFormat</code>
 342:    *         instance.
 343:    */
 344:   public String toString() 
 345:   {
 346:     StringBuffer output = new StringBuffer(getClass().getName());
 347:     output.append("[tokens=");
 348:     output.append(tokens);
 349:     output.append(", formatData=");
 350:     output.append(formatData);
 351:     output.append(", defaultCenturyStart=");
 352:     output.append(defaultCenturyStart);
 353:     output.append(", defaultCentury=");
 354:     output.append(defaultCentury);
 355:     output.append(", pattern=");
 356:     output.append(pattern);
 357:     output.append(", serialVersionOnStream=");
 358:     output.append(serialVersionOnStream);
 359:     output.append(", standardChars=");
 360:     output.append(standardChars);
 361:     output.append("]");
 362:     return output.toString();
 363:   }
 364: 
 365:   /**
 366:    * Constructs a SimpleDateFormat using the default pattern for
 367:    * the default locale.
 368:    */
 369:   public SimpleDateFormat() 
 370:   {
 371:     /*
 372:      * There does not appear to be a standard API for determining 
 373:      * what the default pattern for a locale is, so use package-scope
 374:      * variables in DateFormatSymbols to encapsulate this.
 375:      */
 376:     super();
 377:     Locale locale = Locale.getDefault();
 378:     calendar = new GregorianCalendar(locale);
 379:     computeCenturyStart();
 380:     tokens = new ArrayList();
 381:     formatData = new DateFormatSymbols(locale);
 382:     pattern = (formatData.dateFormats[DEFAULT] + ' '
 383:            + formatData.timeFormats[DEFAULT]);
 384:     compileFormat(pattern);
 385:     numberFormat = NumberFormat.getInstance(locale);
 386:     numberFormat.setGroupingUsed (false);
 387:     numberFormat.setParseIntegerOnly (true);
 388:     numberFormat.setMaximumFractionDigits (0);
 389:   }
 390:   
 391:   /**
 392:    * Creates a date formatter using the specified non-localized pattern,
 393:    * with the default DateFormatSymbols for the default locale.
 394:    *
 395:    * @param pattern the pattern to use.
 396:    * @throws NullPointerException if the pattern is null.
 397:    * @throws IllegalArgumentException if the pattern is invalid.
 398:    */
 399:   public SimpleDateFormat(String pattern) 
 400:   {
 401:     this(pattern, Locale.getDefault());
 402:   }
 403: 
 404:   /**
 405:    * Creates a date formatter using the specified non-localized pattern,
 406:    * with the default DateFormatSymbols for the given locale.
 407:    *
 408:    * @param pattern the non-localized pattern to use.
 409:    * @param locale the locale to use for the formatting symbols.
 410:    * @throws NullPointerException if the pattern is null.
 411:    * @throws IllegalArgumentException if the pattern is invalid.
 412:    */
 413:   public SimpleDateFormat(String pattern, Locale locale) 
 414:   {
 415:     super();
 416:     calendar = new GregorianCalendar(locale);
 417:     computeCenturyStart();
 418:     tokens = new ArrayList();
 419:     formatData = new DateFormatSymbols(locale);
 420:     compileFormat(pattern);
 421:     this.pattern = pattern;
 422:     numberFormat = NumberFormat.getInstance(locale);
 423:     numberFormat.setGroupingUsed (false);
 424:     numberFormat.setParseIntegerOnly (true);
 425:     numberFormat.setMaximumFractionDigits (0);
 426:   }
 427: 
 428:   /**
 429:    * Creates a date formatter using the specified non-localized
 430:    * pattern. The specified DateFormatSymbols will be used when
 431:    * formatting.
 432:    *
 433:    * @param pattern the non-localized pattern to use.
 434:    * @param formatData the formatting symbols to use.
 435:    * @throws NullPointerException if the pattern or formatData is null.
 436:    * @throws IllegalArgumentException if the pattern is invalid.
 437:    */
 438:   public SimpleDateFormat(String pattern, DateFormatSymbols formatData)
 439:   {
 440:     super();
 441:     calendar = new GregorianCalendar();
 442:     computeCenturyStart ();
 443:     tokens = new ArrayList();
 444:     if (formatData == null)
 445:       throw new NullPointerException("formatData");
 446:     this.formatData = formatData;
 447:     compileFormat(pattern);
 448:     this.pattern = pattern;
 449:     numberFormat = NumberFormat.getInstance();
 450:     numberFormat.setGroupingUsed (false);
 451:     numberFormat.setParseIntegerOnly (true);
 452:     numberFormat.setMaximumFractionDigits (0);
 453:   }
 454: 
 455:   /**
 456:    * This method returns a string with the formatting pattern being used
 457:    * by this object.  This string is unlocalized.
 458:    *
 459:    * @return The format string.
 460:    */
 461:   public String toPattern()
 462:   {
 463:     return pattern;
 464:   }
 465: 
 466:   /**
 467:    * This method returns a string with the formatting pattern being used
 468:    * by this object.  This string is localized.
 469:    *
 470:    * @return The format string.
 471:    */
 472:   public String toLocalizedPattern()
 473:   {
 474:     String localChars = formatData.getLocalPatternChars();
 475:     return translateLocalizedPattern(pattern, standardChars, localChars);
 476:   }
 477: 
 478:   /**
 479:    * This method sets the formatting pattern that should be used by this
 480:    * object.  This string is not localized.
 481:    *
 482:    * @param pattern The new format pattern.
 483:    * @throws NullPointerException if the pattern is null.
 484:    * @throws IllegalArgumentException if the pattern is invalid.
 485:    */
 486:   public void applyPattern(String pattern)
 487:   {
 488:     tokens = new ArrayList();
 489:     compileFormat(pattern);
 490:     this.pattern = pattern;
 491:   }
 492: 
 493:   /**
 494:    * This method sets the formatting pattern that should be used by this
 495:    * object.  This string is localized.
 496:    *
 497:    * @param pattern The new format pattern.
 498:    * @throws NullPointerException if the pattern is null.
 499:    * @throws IllegalArgumentException if the pattern is invalid.
 500:    */
 501:   public void applyLocalizedPattern(String pattern)
 502:   {
 503:     String localChars = formatData.getLocalPatternChars();
 504:     pattern = translateLocalizedPattern(pattern, localChars, standardChars);
 505:     applyPattern(pattern);
 506:   }
 507: 
 508:   /**
 509:    * Translates either from or to a localized variant of the pattern
 510:    * string.  For example, in the German locale, 't' (for 'tag') is
 511:    * used instead of 'd' (for 'date').  This method translates
 512:    * a localized pattern (such as 'ttt') to a non-localized pattern
 513:    * (such as 'ddd'), or vice versa.  Non-localized patterns use
 514:    * a standard set of characters, which match those of the U.S. English
 515:    * locale.
 516:    *
 517:    * @param pattern the pattern to translate.
 518:    * @param oldChars the old set of characters (used in the pattern).
 519:    * @param newChars the new set of characters (which will be used in the
 520:    *                 pattern).
 521:    * @return a version of the pattern using the characters in
 522:    *         <code>newChars</code>.
 523:    */
 524:   private String translateLocalizedPattern(String pattern,
 525:                        String oldChars, String newChars)
 526:   {
 527:     int len = pattern.length();
 528:     StringBuffer buf = new StringBuffer(len);
 529:     boolean quoted = false;
 530:     for (int i = 0;  i < len;  i++)
 531:       {
 532:     char ch = pattern.charAt(i);
 533:     if (ch == '\'')
 534:       quoted = ! quoted;
 535:     if (! quoted)
 536:       {
 537:         int j = oldChars.indexOf(ch);
 538:         if (j >= 0)
 539:           ch = newChars.charAt(j);
 540:       }
 541:     buf.append(ch);
 542:       }
 543:     return buf.toString();
 544:   }
 545: 
 546:   /** 
 547:    * Returns the start of the century used for two digit years.
 548:    *
 549:    * @return A <code>Date</code> representing the start of the century
 550:    * for two digit years.
 551:    */
 552:   public Date get2DigitYearStart()
 553:   {
 554:     return defaultCenturyStart;
 555:   }
 556: 
 557:   /**
 558:    * Sets the start of the century used for two digit years.
 559:    *
 560:    * @param date A <code>Date</code> representing the start of the century for
 561:    * two digit years.
 562:    */
 563:   public void set2DigitYearStart(Date date)
 564:   {
 565:     defaultCenturyStart = date;
 566:     calendar.clear();
 567:     calendar.setTime(date);
 568:     int year = calendar.get(Calendar.YEAR);
 569:     defaultCentury = year - (year % 100);
 570:   }
 571: 
 572:   /**
 573:    * This method returns a copy of the format symbol information used
 574:    * for parsing and formatting dates.
 575:    *
 576:    * @return a copy of the date format symbols.
 577:    */
 578:   public DateFormatSymbols getDateFormatSymbols()
 579:   {
 580:     return (DateFormatSymbols) formatData.clone();
 581:   }
 582: 
 583:   /**
 584:    * This method sets the format symbols information used for parsing
 585:    * and formatting dates.
 586:    *
 587:    * @param formatData The date format symbols.
 588:    * @throws NullPointerException if <code>formatData</code> is null.
 589:    */
 590:    public void setDateFormatSymbols(DateFormatSymbols formatData)
 591:    {
 592:      if (formatData == null)
 593:        {
 594:      throw new
 595:        NullPointerException("The supplied format data was null.");
 596:        }
 597:      this.formatData = formatData;
 598:    }
 599: 
 600:   /**
 601:    * This methods tests whether the specified object is equal to this
 602:    * object.  This will be true if and only if the specified object:
 603:    * <p>
 604:    * <ul>
 605:    * <li>Is not <code>null</code>.</li>
 606:    * <li>Is an instance of <code>SimpleDateFormat</code>.</li>
 607:    * <li>Is equal to this object at the superclass (i.e., <code>DateFormat</code>)
 608:    *     level.</li>
 609:    * <li>Has the same formatting pattern.</li>
 610:    * <li>Is using the same formatting symbols.</li>
 611:    * <li>Is using the same century for two digit years.</li>
 612:    * </ul>
 613:    *
 614:    * @param obj The object to compare for equality against.
 615:    *
 616:    * @return <code>true</code> if the specified object is equal to this object,
 617:    * <code>false</code> otherwise.
 618:    */
 619:   public boolean equals(Object o)
 620:   {
 621:     if (!super.equals(o))
 622:       return false;
 623: 
 624:     if (!(o instanceof SimpleDateFormat))
 625:       return false;
 626: 
 627:     SimpleDateFormat sdf = (SimpleDateFormat)o;
 628: 
 629:     if (defaultCentury != sdf.defaultCentury)
 630:       return false;
 631: 
 632:     if (!toPattern().equals(sdf.toPattern()))
 633:       return false;
 634: 
 635:     if (!getDateFormatSymbols().equals(sdf.getDateFormatSymbols()))
 636:       return false;
 637: 
 638:     return true;
 639:   }
 640: 
 641:   /**
 642:    * This method returns a hash value for this object.
 643:    *
 644:    * @return A hash value for this object.
 645:    */
 646:   public int hashCode()
 647:   {
 648:     return super.hashCode() ^ toPattern().hashCode() ^ defaultCentury ^
 649:       getDateFormatSymbols().hashCode();
 650:   }
 651: 
 652: 
 653:   /**
 654:    * Formats the date input according to the format string in use,
 655:    * appending to the specified StringBuffer.  The input StringBuffer
 656:    * is returned as output for convenience.
 657:    */
 658:   private void formatWithAttribute(Date date, FormatBuffer buffer, FieldPosition pos)
 659:   {
 660:     String temp;
 661:     AttributedCharacterIterator.Attribute attribute;
 662:     calendar.setTime(date);
 663: 
 664:     // go through vector, filling in fields where applicable, else toString
 665:     Iterator iter = tokens.iterator();
 666:     while (iter.hasNext())
 667:       {
 668:     Object o = iter.next();
 669:     if (o instanceof CompiledField)
 670:       {
 671:         CompiledField cf = (CompiledField) o;
 672:         int beginIndex = buffer.length();
 673:         
 674:         switch (cf.getField())
 675:           {
 676:           case ERA_FIELD:
 677:         buffer.append (formatData.eras[calendar.get (Calendar.ERA)], DateFormat.Field.ERA);
 678:         break;
 679:           case YEAR_FIELD:
 680:         // If we have two digits, then we truncate.  Otherwise, we
 681:         // use the size of the pattern, and zero pad.
 682:         buffer.setDefaultAttribute (DateFormat.Field.YEAR);
 683:         if (cf.getSize() == 2)
 684:           {
 685:             temp = "00"+String.valueOf (calendar.get (Calendar.YEAR));
 686:             buffer.append (temp.substring (temp.length() - 2));
 687:           }
 688:         else
 689:           withLeadingZeros (calendar.get (Calendar.YEAR), cf.getSize(), buffer);
 690:         break;
 691:           case MONTH_FIELD:
 692:         buffer.setDefaultAttribute (DateFormat.Field.MONTH);
 693:         if (cf.getSize() < 3)
 694:           withLeadingZeros (calendar.get (Calendar.MONTH) + 1, cf.getSize(), buffer);
 695:         else if (cf.getSize() < 4)
 696:           buffer.append (formatData.shortMonths[calendar.get (Calendar.MONTH)]);
 697:         else
 698:           buffer.append (formatData.months[calendar.get (Calendar.MONTH)]);
 699:         break;
 700:           case DATE_FIELD:
 701:         buffer.setDefaultAttribute (DateFormat.Field.DAY_OF_MONTH);
 702:         withLeadingZeros (calendar.get (Calendar.DATE), cf.getSize(), buffer);
 703:         break;
 704:           case HOUR_OF_DAY1_FIELD: // 1-24
 705:         buffer.setDefaultAttribute(DateFormat.Field.HOUR_OF_DAY1);
 706:         withLeadingZeros ( ((calendar.get (Calendar.HOUR_OF_DAY) + 23) % 24) + 1, 
 707:                    cf.getSize(), buffer);
 708:         break;
 709:           case HOUR_OF_DAY0_FIELD: // 0-23
 710:         buffer.setDefaultAttribute (DateFormat.Field.HOUR_OF_DAY0);
 711:         withLeadingZeros (calendar.get (Calendar.HOUR_OF_DAY), cf.getSize(), buffer);
 712:         break;
 713:           case MINUTE_FIELD:
 714:         buffer.setDefaultAttribute (DateFormat.Field.MINUTE);
 715:         withLeadingZeros (calendar.get (Calendar.MINUTE),
 716:                   cf.getSize(), buffer);
 717:         break;
 718:           case SECOND_FIELD:
 719:         buffer.setDefaultAttribute (DateFormat.Field.SECOND);
 720:         withLeadingZeros(calendar.get (Calendar.SECOND), 
 721:                  cf.getSize(), buffer);
 722:         break;
 723:           case MILLISECOND_FIELD:
 724:         buffer.setDefaultAttribute (DateFormat.Field.MILLISECOND);
 725:         withLeadingZeros (calendar.get (Calendar.MILLISECOND), cf.getSize(), buffer);
 726:         break;
 727:           case DAY_OF_WEEK_FIELD:
 728:         buffer.setDefaultAttribute (DateFormat.Field.DAY_OF_WEEK);
 729:         if (cf.getSize() < 4)
 730:           buffer.append (formatData.shortWeekdays[calendar.get (Calendar.DAY_OF_WEEK)]);
 731:         else
 732:           buffer.append (formatData.weekdays[calendar.get (Calendar.DAY_OF_WEEK)]);
 733:         break;
 734:           case DAY_OF_YEAR_FIELD:
 735:         buffer.setDefaultAttribute (DateFormat.Field.DAY_OF_YEAR);
 736:         withLeadingZeros (calendar.get (Calendar.DAY_OF_YEAR), cf.getSize(), buffer);
 737:         break;
 738:           case DAY_OF_WEEK_IN_MONTH_FIELD:
 739:         buffer.setDefaultAttribute (DateFormat.Field.DAY_OF_WEEK_IN_MONTH);
 740:         withLeadingZeros (calendar.get (Calendar.DAY_OF_WEEK_IN_MONTH), 
 741:                  cf.getSize(), buffer);
 742:         break;
 743:           case WEEK_OF_YEAR_FIELD:
 744:         buffer.setDefaultAttribute (DateFormat.Field.WEEK_OF_YEAR);
 745:         withLeadingZeros (calendar.get (Calendar.WEEK_OF_YEAR),
 746:                   cf.getSize(), buffer);
 747:         break;
 748:           case WEEK_OF_MONTH_FIELD:
 749:         buffer.setDefaultAttribute (DateFormat.Field.WEEK_OF_MONTH);
 750:         withLeadingZeros (calendar.get (Calendar.WEEK_OF_MONTH),
 751:                   cf.getSize(), buffer);
 752:         break;
 753:           case AM_PM_FIELD:
 754:         buffer.setDefaultAttribute (DateFormat.Field.AM_PM);
 755:         buffer.append (formatData.ampms[calendar.get (Calendar.AM_PM)]);
 756:         break;
 757:           case HOUR1_FIELD: // 1-12
 758:         buffer.setDefaultAttribute (DateFormat.Field.HOUR1);
 759:         withLeadingZeros (((calendar.get (Calendar.HOUR) + 11) % 12) + 1,
 760:                   cf.getSize(), buffer);
 761:         break;
 762:           case HOUR0_FIELD: // 0-11
 763:         buffer.setDefaultAttribute (DateFormat.Field.HOUR0);
 764:         withLeadingZeros (calendar.get (Calendar.HOUR), cf.getSize(), buffer);
 765:         break;
 766:           case TIMEZONE_FIELD:
 767:         buffer.setDefaultAttribute (DateFormat.Field.TIME_ZONE);
 768:         TimeZone zone = calendar.getTimeZone();
 769:         boolean isDST = calendar.get (Calendar.DST_OFFSET) != 0;
 770:         // FIXME: XXX: This should be a localized time zone.
 771:         String zoneID = zone.getDisplayName
 772:           (isDST, cf.getSize() > 3 ? TimeZone.LONG : TimeZone.SHORT);
 773:         buffer.append (zoneID);
 774:         break;
 775:           case RFC822_TIMEZONE_FIELD:
 776:         buffer.setDefaultAttribute(DateFormat.Field.RFC822_TIME_ZONE);
 777:         int pureMinutes = (calendar.get(Calendar.ZONE_OFFSET) +
 778:                    calendar.get(Calendar.DST_OFFSET)) / (1000 * 60);
 779:         String sign = (pureMinutes < 0) ? "-" : "+";      
 780:         int hours = pureMinutes / 60;
 781:         int minutes = pureMinutes % 60;
 782:         buffer.append(sign);
 783:         withLeadingZeros(hours, 2, buffer);
 784:         withLeadingZeros(minutes, 2, buffer);
 785:         break;
 786:           default:
 787:         throw new IllegalArgumentException ("Illegal pattern character " +
 788:                             cf.getCharacter());
 789:           }
 790:         if (pos != null && (buffer.getDefaultAttribute() == pos.getFieldAttribute()
 791:                 || cf.getField() == pos.getField()))
 792:           {
 793:         pos.setBeginIndex(beginIndex);
 794:         pos.setEndIndex(buffer.length());
 795:           }
 796:       } 
 797:       else
 798:     {  
 799:       buffer.append(o.toString(), null);
 800:     }
 801:       }
 802:   }
 803:   
 804:   public StringBuffer format(Date date, StringBuffer buffer, FieldPosition pos)
 805:   {
 806:     formatWithAttribute(date, new StringFormatBuffer (buffer), pos);
 807: 
 808:     return buffer;
 809:   }
 810: 
 811:   public AttributedCharacterIterator formatToCharacterIterator(Object date)
 812:     throws IllegalArgumentException
 813:   {
 814:     if (date == null)
 815:       throw new NullPointerException("null argument");
 816:     if (!(date instanceof Date))
 817:       throw new IllegalArgumentException("argument should be an instance of java.util.Date");
 818: 
 819:     AttributedFormatBuffer buf = new AttributedFormatBuffer();
 820:     formatWithAttribute((Date)date, buf,
 821:             null);
 822:     buf.sync();
 823:         
 824:     return new FormatCharacterIterator(buf.getBuffer().toString(),
 825:                        buf.getRanges(),
 826:                        buf.getAttributes());
 827:   }
 828: 
 829:   private void withLeadingZeros(int value, int length, FormatBuffer buffer) 
 830:   {
 831:     String valStr = String.valueOf(value);
 832:     for (length -= valStr.length(); length > 0; length--)
 833:       buffer.append('0');
 834:     buffer.append(valStr);
 835:   }
 836: 
 837:   private boolean expect(String source, ParsePosition pos, char ch)
 838:   {
 839:     int x = pos.getIndex();
 840:     boolean r = x < source.length() && source.charAt(x) == ch;
 841:     if (r)
 842:       pos.setIndex(x + 1);
 843:     else
 844:       pos.setErrorIndex(x);
 845:     return r;
 846:   }
 847: 
 848:   /**
 849:    * This method parses the specified string into a date.
 850:    * 
 851:    * @param dateStr The date string to parse.
 852:    * @param pos The input and output parse position
 853:    *
 854:    * @return The parsed date, or <code>null</code> if the string cannot be
 855:    * parsed.
 856:    */
 857:   public Date parse (String dateStr, ParsePosition pos)
 858:   {
 859:     int fmt_index = 0;
 860:     int fmt_max = pattern.length();
 861: 
 862:     calendar.clear();
 863:     boolean saw_timezone = false;
 864:     int quote_start = -1;
 865:     boolean is2DigitYear = false;
 866:     try
 867:       {
 868:     for (; fmt_index < fmt_max; ++fmt_index)
 869:       {
 870:         char ch = pattern.charAt(fmt_index);
 871:         if (ch == '\'')
 872:           {
 873:         int index = pos.getIndex();
 874:         if (fmt_index < fmt_max - 1
 875:             && pattern.charAt(fmt_index + 1) == '\'')
 876:           {
 877:             if (! expect (dateStr, pos, ch))
 878:               return null;
 879:             ++fmt_index;
 880:           }
 881:         else
 882:           quote_start = quote_start < 0 ? fmt_index : -1;
 883:         continue;
 884:           }
 885:         
 886:         if (quote_start != -1
 887:         || ((ch < 'a' || ch > 'z')
 888:             && (ch < 'A' || ch > 'Z')))
 889:           {
 890:         if (! expect (dateStr, pos, ch))
 891:           return null;
 892:         continue;
 893:           }
 894:         
 895:         // We've arrived at a potential pattern character in the
 896:         // pattern.
 897:         int fmt_count = 1;
 898:         while (++fmt_index < fmt_max && pattern.charAt(fmt_index) == ch)
 899:           {
 900:         ++fmt_count;
 901:           }
 902:         
 903:         // We might need to limit the number of digits to parse in
 904:         // some cases.  We look to the next pattern character to
 905:         // decide.
 906:         boolean limit_digits = false;
 907:         if (fmt_index < fmt_max
 908:         && standardChars.indexOf(pattern.charAt(fmt_index)) >= 0)
 909:           limit_digits = true;
 910:         --fmt_index;
 911:         
 912:         // We can handle most fields automatically: most either are
 913:         // numeric or are looked up in a string vector.  In some cases
 914:         // we need an offset.  When numeric, `offset' is added to the
 915:         // resulting value.  When doing a string lookup, offset is the
 916:         // initial index into the string array.
 917:         int calendar_field;
 918:         boolean is_numeric = true;
 919:         int offset = 0;
 920:         boolean maybe2DigitYear = false;
 921:         boolean oneBasedHour = false;
 922:         boolean oneBasedHourOfDay = false;
 923:         Integer simpleOffset;
 924:         String[] set1 = null;
 925:         String[] set2 = null;
 926:         switch (ch)
 927:           {
 928:           case 'd':
 929:         calendar_field = Calendar.DATE;
 930:         break;
 931:           case 'D':
 932:         calendar_field = Calendar.DAY_OF_YEAR;
 933:         break;
 934:           case 'F':
 935:         calendar_field = Calendar.DAY_OF_WEEK_IN_MONTH;
 936:         break;
 937:           case 'E':
 938:         is_numeric = false;
 939:         offset = 1;
 940:         calendar_field = Calendar.DAY_OF_WEEK;
 941:         set1 = formatData.getWeekdays();
 942:         set2 = formatData.getShortWeekdays();
 943:         break;
 944:           case 'w':
 945:         calendar_field = Calendar.WEEK_OF_YEAR;
 946:         break;
 947:           case 'W':
 948:         calendar_field = Calendar.WEEK_OF_MONTH;
 949:         break;
 950:           case 'M':
 951:         calendar_field = Calendar.MONTH;
 952:         if (fmt_count <= 2)
 953:           offset = -1;
 954:         else
 955:           {
 956:             is_numeric = false;
 957:             set1 = formatData.getMonths();
 958:             set2 = formatData.getShortMonths();
 959:           }
 960:         break;
 961:           case 'y':
 962:         calendar_field = Calendar.YEAR;
 963:         if (fmt_count <= 2)
 964:           maybe2DigitYear = true;
 965:         break;
 966:           case 'K':
 967:         calendar_field = Calendar.HOUR;
 968:         break;
 969:           case 'h':
 970:         calendar_field = Calendar.HOUR;
 971:         oneBasedHour = true;
 972:         break;
 973:           case 'H':
 974:         calendar_field = Calendar.HOUR_OF_DAY;
 975:         break;
 976:           case 'k':
 977:         calendar_field = Calendar.HOUR_OF_DAY;
 978:         oneBasedHourOfDay = true;
 979:         break;
 980:           case 'm':
 981:         calendar_field = Calendar.MINUTE;
 982:         break;
 983:           case 's':
 984:         calendar_field = Calendar.SECOND;
 985:         break;
 986:           case 'S':
 987:         calendar_field = Calendar.MILLISECOND;
 988:         break;
 989:           case 'a':
 990:         is_numeric = false;
 991:         calendar_field = Calendar.AM_PM;
 992:         set1 = formatData.getAmPmStrings();
 993:         break;
 994:           case 'z':
 995:           case 'Z':
 996:         // We need a special case for the timezone, because it
 997:         // uses a different data structure than the other cases.
 998:         is_numeric = false;
 999:         calendar_field = Calendar.ZONE_OFFSET;
1000:         String[][] zoneStrings = formatData.getZoneStrings();
1001:         int zoneCount = zoneStrings.length;
1002:         int index = pos.getIndex();
1003:         boolean found_zone = false;
1004:         simpleOffset = computeOffset(dateStr.substring(index), pos);
1005:         if (simpleOffset != null)
1006:           {
1007:             found_zone = true;
1008:             saw_timezone = true;
1009:             calendar.set(Calendar.DST_OFFSET, 0);
1010:             offset = simpleOffset.intValue();
1011:           }
1012:         else
1013:           {
1014:             for (int j = 0;  j < zoneCount;  j++)
1015:               {
1016:             String[] strings = zoneStrings[j];
1017:             int k;
1018:             for (k = 0; k < strings.length; ++k)
1019:               {
1020:                 if (dateStr.startsWith(strings[k], index))
1021:                   break;
1022:               }
1023:             if (k != strings.length)
1024:               {
1025:                 found_zone = true;
1026:                 saw_timezone = true;
1027:                 TimeZone tz = TimeZone.getTimeZone (strings[0]);
1028:                 // Check if it's a DST zone or ordinary 
1029:                 if(k == 3 || k == 4)
1030:                   calendar.set (Calendar.DST_OFFSET, tz.getDSTSavings());
1031:                 else
1032:                   calendar.set (Calendar.DST_OFFSET, 0);
1033:                             offset = tz.getRawOffset ();
1034:                 pos.setIndex(index + strings[k].length());
1035:                 break;
1036:               }
1037:               }
1038:           }
1039:         if (! found_zone)
1040:           {
1041:             pos.setErrorIndex(pos.getIndex());
1042:             return null;
1043:           }
1044:         break;
1045:           default:
1046:         pos.setErrorIndex(pos.getIndex());
1047:         return null;
1048:           }
1049:       
1050:         // Compute the value we should assign to the field.
1051:         int value;
1052:         int index = -1;
1053:         if (is_numeric)
1054:           {
1055:         numberFormat.setMinimumIntegerDigits(fmt_count);
1056:         if (limit_digits)
1057:           numberFormat.setMaximumIntegerDigits(fmt_count);
1058:         if (maybe2DigitYear)
1059:           index = pos.getIndex();
1060:         Number n = numberFormat.parse(dateStr, pos);
1061:         if (pos == null || ! (n instanceof Long))
1062:           return null;
1063:         value = n.intValue() + offset;
1064:           }
1065:         else if (set1 != null)
1066:           {
1067:         index = pos.getIndex();
1068:         int i;
1069:         boolean found = false;
1070:         for (i = offset; i < set1.length; ++i)
1071:           {
1072:             if (set1[i] != null)
1073:               if (dateStr.toUpperCase().startsWith(set1[i].toUpperCase(),
1074:                                index))
1075:             {
1076:               found = true;
1077:               pos.setIndex(index + set1[i].length());
1078:               break;
1079:             }
1080:           }
1081:         if (!found && set2 != null)
1082:           {
1083:             for (i = offset; i < set2.length; ++i)
1084:               {
1085:             if (set2[i] != null)
1086:               if (dateStr.toUpperCase().startsWith(set2[i].toUpperCase(),
1087:                                    index))
1088:                 {
1089:                   found = true;
1090:                   pos.setIndex(index + set2[i].length());
1091:                   break;
1092:                 }
1093:               }
1094:           }
1095:         if (!found)
1096:           {
1097:             pos.setErrorIndex(index);
1098:             return null;
1099:           }
1100:         value = i;
1101:           }
1102:         else
1103:           value = offset;
1104:       
1105:         if (maybe2DigitYear)
1106:           {
1107:         // Parse into default century if the numeric year string has 
1108:         // exactly 2 digits.
1109:         int digit_count = pos.getIndex() - index;
1110:         if (digit_count == 2)
1111:           {
1112:             is2DigitYear = true;
1113:             value += defaultCentury;
1114:           }
1115:           }
1116:         
1117:         // Calendar uses 0-based hours. 
1118:         // I.e. 00:00 AM is midnight, not 12 AM or 24:00
1119:         if (oneBasedHour && value == 12)
1120:           value = 0;
1121: 
1122:         if (oneBasedHourOfDay && value == 24)
1123:           value = 0;
1124:         
1125:         // Assign the value and move on.
1126:         calendar.set(calendar_field, value);
1127:       }
1128:     
1129:     if (is2DigitYear)
1130:       {
1131:         // Apply the 80-20 heuristic to dermine the full year based on 
1132:         // defaultCenturyStart. 
1133:         int year = calendar.get(Calendar.YEAR);
1134:         if (calendar.getTime().compareTo(defaultCenturyStart) < 0)
1135:           calendar.set(Calendar.YEAR, year + 100);      
1136:       }
1137:     if (! saw_timezone)
1138:       {
1139:         // Use the real rules to determine whether or not this
1140:         // particular time is in daylight savings.
1141:         calendar.clear (Calendar.DST_OFFSET);
1142:         calendar.clear (Calendar.ZONE_OFFSET);
1143:       }
1144:         return calendar.getTime();
1145:       }
1146:     catch (IllegalArgumentException x)
1147:       {
1148:         pos.setErrorIndex(pos.getIndex());
1149:     return null;
1150:       }
1151:       }
1152: 
1153:   /**
1154:    * <p>
1155:    * Computes the time zone offset in milliseconds
1156:    * relative to GMT, based on the supplied
1157:    * <code>String</code> representation.
1158:    * </p>
1159:    * <p>
1160:    * The supplied <code>String</code> must be a three
1161:    * or four digit signed number, with an optional 'GMT'
1162:    * prefix.  The first one or two digits represents the hours,
1163:    * while the last two represent the minutes.  The
1164:    * two sets of digits can optionally be separated by
1165:    * ':'.  The mandatory sign prefix (either '+' or '-')
1166:    * indicates the direction of the offset from GMT.
1167:    * </p>
1168:    * <p>
1169:    * For example, 'GMT+0200' specifies 2 hours after
1170:    * GMT, while '-05:00' specifies 5 hours prior to
1171:    * GMT.  The special case of 'GMT' alone can be used
1172:    * to represent the offset, 0.
1173:    * </p>
1174:    * <p>
1175:    * If the <code>String</code> can not be parsed,
1176:    * the result will be null.  The resulting offset
1177:    * is wrapped in an <code>Integer</code> object, in
1178:    * order to allow such failure to be represented.
1179:    * </p>
1180:    *
1181:    * @param zoneString a string in the form 
1182:    *        (GMT)? sign hours : minutes
1183:    *        where sign = '+' or '-', hours
1184:    *        is a one or two digits representing
1185:    *        a number between 0 and 23, and
1186:    *        minutes is two digits representing
1187:    *        a number between 0 and 59.
1188:    * @return the parsed offset, or null if parsing
1189:    *         failed.
1190:    */
1191:   private Integer computeOffset(String zoneString, ParsePosition pos)
1192:   {
1193:     Pattern pattern = 
1194:       Pattern.compile("(GMT)?([+-])([012])?([0-9]):?([0-9]{2})");
1195:     Matcher matcher = pattern.matcher(zoneString);
1196: 
1197:     // Match from start, but ignore trailing parts
1198:     boolean hasAll = matcher.lookingAt();
1199:     try
1200:       {
1201:     // Do we have at least the sign, hour and minute?
1202:     matcher.group(2);
1203:     matcher.group(4);
1204:     matcher.group(5);
1205:       }
1206:     catch (IllegalStateException ise)
1207:       {
1208:     hasAll = false;
1209:       }
1210:     if (hasAll)
1211:       {
1212:     int sign = matcher.group(2).equals("+") ? 1 : -1;
1213:     int hour = Integer.parseInt(matcher.group(4));
1214:     if (!matcher.group(3).equals(""))
1215:       hour += (Integer.parseInt(matcher.group(3)) * 10);
1216:     int minutes = Integer.parseInt(matcher.group(5));
1217: 
1218:     if (hour > 23)
1219:       return null;
1220:     int offset = sign * ((hour * 60) + minutes) * 60000;
1221: 
1222:     // advance the index
1223:     pos.setIndex(pos.getIndex() + matcher.end());
1224:     return new Integer(offset);
1225:       }
1226:     else if (zoneString.startsWith("GMT"))
1227:       {
1228:     pos.setIndex(pos.getIndex() + 3);
1229:     return new Integer(0);
1230:       }
1231:     return null;
1232:   }
1233: 
1234:   // Compute the start of the current century as defined by
1235:   // get2DigitYearStart.
1236:   private void computeCenturyStart()
1237:   {
1238:     int year = calendar.get(Calendar.YEAR);
1239:     calendar.set(Calendar.YEAR, year - 80);
1240:     set2DigitYearStart(calendar.getTime());
1241:   }
1242: 
1243:   /**
1244:    * Returns a copy of this instance of
1245:    * <code>SimpleDateFormat</code>.  The copy contains
1246:    * clones of the formatting symbols and the 2-digit
1247:    * year century start date.
1248:    */
1249:   public Object clone()
1250:   {
1251:     SimpleDateFormat clone = (SimpleDateFormat) super.clone();
1252:     clone.setDateFormatSymbols((DateFormatSymbols) formatData.clone());
1253:     clone.set2DigitYearStart((Date) defaultCenturyStart.clone());
1254:     return clone;
1255:   }
1256: 
1257: }