jdk/src/share/classes/java/text/DateFormat.java
changeset 19054 a64012cb49d6
parent 16010 2727163b5df5
equal deleted inserted replaced
19053:69648476a89e 19054:a64012cb49d6
     1 /*
     1 /*
     2  * Copyright (c) 1996, 2012, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 1996, 2013, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     5  * This code is free software; you can redistribute it and/or modify it
     6  * under the terms of the GNU General Public License version 2 only, as
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.  Oracle designates this
     7  * published by the Free Software Foundation.  Oracle designates this
    55 
    55 
    56 /**
    56 /**
    57  * {@code DateFormat} is an abstract class for date/time formatting subclasses which
    57  * {@code DateFormat} is an abstract class for date/time formatting subclasses which
    58  * formats and parses dates or time in a language-independent manner.
    58  * formats and parses dates or time in a language-independent manner.
    59  * The date/time formatting subclass, such as {@link SimpleDateFormat}, allows for
    59  * The date/time formatting subclass, such as {@link SimpleDateFormat}, allows for
    60  * formatting (i.e., date -> text), parsing (text -> date), and
    60  * formatting (i.e., date → text), parsing (text → date), and
    61  * normalization.  The date is represented as a <code>Date</code> object or
    61  * normalization.  The date is represented as a <code>Date</code> object or
    62  * as the milliseconds since January 1, 1970, 00:00:00 GMT.
    62  * as the milliseconds since January 1, 1970, 00:00:00 GMT.
    63  *
    63  *
    64  * <p>{@code DateFormat} provides many class methods for obtaining default date/time
    64  * <p>{@code DateFormat} provides many class methods for obtaining default date/time
    65  * formatters based on the default or a given locale and a number of formatting
    65  * formatters based on the default or a given locale and a number of formatting
    71  * Your code can be completely independent of the locale conventions for
    71  * Your code can be completely independent of the locale conventions for
    72  * months, days of the week, or even the calendar format: lunar vs. solar.
    72  * months, days of the week, or even the calendar format: lunar vs. solar.
    73  *
    73  *
    74  * <p>To format a date for the current Locale, use one of the
    74  * <p>To format a date for the current Locale, use one of the
    75  * static factory methods:
    75  * static factory methods:
    76  * <pre>
    76  * <blockquote>
    77  *  myString = DateFormat.getDateInstance().format(myDate);
    77  * <pre>{@code
    78  * </pre>
    78  * myString = DateFormat.getDateInstance().format(myDate);
       
    79  * }</pre>
       
    80  * </blockquote>
    79  * <p>If you are formatting multiple dates, it is
    81  * <p>If you are formatting multiple dates, it is
    80  * more efficient to get the format and use it multiple times so that
    82  * more efficient to get the format and use it multiple times so that
    81  * the system doesn't have to fetch the information about the local
    83  * the system doesn't have to fetch the information about the local
    82  * language and country conventions multiple times.
    84  * language and country conventions multiple times.
    83  * <pre>
    85  * <blockquote>
    84  *  DateFormat df = DateFormat.getDateInstance();
    86  * <pre>{@code
    85  *  for (int i = 0; i < myDate.length; ++i) {
    87  * DateFormat df = DateFormat.getDateInstance();
    86  *    output.println(df.format(myDate[i]) + "; ");
    88  * for (int i = 0; i < myDate.length; ++i) {
    87  *  }
    89  *     output.println(df.format(myDate[i]) + "; ");
    88  * </pre>
    90  * }
       
    91  * }</pre>
       
    92  * </blockquote>
    89  * <p>To format a date for a different Locale, specify it in the
    93  * <p>To format a date for a different Locale, specify it in the
    90  * call to {@link #getDateInstance(int, Locale) getDateInstance()}.
    94  * call to {@link #getDateInstance(int, Locale) getDateInstance()}.
    91  * <pre>
    95  * <blockquote>
    92  *  DateFormat df = DateFormat.getDateInstance(DateFormat.LONG, Locale.FRANCE);
    96  * <pre>{@code
    93  * </pre>
    97  * DateFormat df = DateFormat.getDateInstance(DateFormat.LONG, Locale.FRANCE);
       
    98  * }</pre>
       
    99  * </blockquote>
    94  * <p>You can use a DateFormat to parse also.
   100  * <p>You can use a DateFormat to parse also.
    95  * <pre>
   101  * <blockquote>
    96  *  myDate = df.parse(myString);
   102  * <pre>{@code
    97  * </pre>
   103  * myDate = df.parse(myString);
       
   104  * }</pre>
       
   105  * </blockquote>
    98  * <p>Use {@code getDateInstance} to get the normal date format for that country.
   106  * <p>Use {@code getDateInstance} to get the normal date format for that country.
    99  * There are other static factory methods available.
   107  * There are other static factory methods available.
   100  * Use {@code getTimeInstance} to get the time format for that country.
   108  * Use {@code getTimeInstance} to get the time format for that country.
   101  * Use {@code getDateTimeInstance} to get a date and time format. You can pass in
   109  * Use {@code getDateTimeInstance} to get a date and time format. You can pass in
   102  * different options to these factory methods to control the length of the
   110  * different options to these factory methods to control the length of the
   123  * <ul><li>progressively parse through pieces of a string.
   131  * <ul><li>progressively parse through pieces of a string.
   124  * <li>align any particular field, or find out where it is for selection
   132  * <li>align any particular field, or find out where it is for selection
   125  * on the screen.
   133  * on the screen.
   126  * </ul>
   134  * </ul>
   127  *
   135  *
   128  * <h4><a name="synchronization">Synchronization</a></h4>
   136  * <h3><a name="synchronization">Synchronization</a></h3>
   129  *
   137  *
   130  * <p>
   138  * <p>
   131  * Date formats are not synchronized.
   139  * Date formats are not synchronized.
   132  * It is recommended to create separate format instances for each thread.
   140  * It is recommended to create separate format instances for each thread.
   133  * If multiple threads access a format concurrently, it must be synchronized
   141  * If multiple threads access a format concurrently, it must be synchronized
   579     }
   587     }
   580 
   588 
   581     /**
   589     /**
   582      * Get a default date/time formatter that uses the SHORT style for both the
   590      * Get a default date/time formatter that uses the SHORT style for both the
   583      * date and the time.
   591      * date and the time.
       
   592      *
       
   593      * @return a date/time formatter
   584      */
   594      */
   585     public final static DateFormat getInstance() {
   595     public final static DateFormat getInstance() {
   586         return getDateTimeInstance(SHORT, SHORT);
   596         return getDateTimeInstance(SHORT, SHORT);
   587     }
   597     }
   588 
   598 
   651     }
   661     }
   652 
   662 
   653     /**
   663     /**
   654      * Sets the time zone for the calendar of this {@code DateFormat} object.
   664      * Sets the time zone for the calendar of this {@code DateFormat} object.
   655      * This method is equivalent to the following call.
   665      * This method is equivalent to the following call.
   656      * <blockquote><pre>
   666      * <blockquote><pre>{@code
   657      *  getCalendar().setTimeZone(zone)
   667      * getCalendar().setTimeZone(zone)
   658      * </pre></blockquote>
   668      * }</pre></blockquote>
   659      *
   669      *
   660      * <p>The {@code TimeZone} set by this method is overwritten by a
   670      * <p>The {@code TimeZone} set by this method is overwritten by a
   661      * {@link #setCalendar(java.util.Calendar) setCalendar} call.
   671      * {@link #setCalendar(java.util.Calendar) setCalendar} call.
   662      *
   672      *
   663      * <p>The {@code TimeZone} set by this method may be overwritten as
   673      * <p>The {@code TimeZone} set by this method may be overwritten as
   671     }
   681     }
   672 
   682 
   673     /**
   683     /**
   674      * Gets the time zone.
   684      * Gets the time zone.
   675      * This method is equivalent to the following call.
   685      * This method is equivalent to the following call.
   676      * <blockquote><pre>
   686      * <blockquote><pre>{@code
   677      *  getCalendar().getTimeZone()
   687      * getCalendar().getTimeZone()
   678      * </pre></blockquote>
   688      * }</pre></blockquote>
   679      *
   689      *
   680      * @return the time zone associated with the calendar of DateFormat.
   690      * @return the time zone associated with the calendar of DateFormat.
   681      */
   691      */
   682     public TimeZone getTimeZone()
   692     public TimeZone getTimeZone()
   683     {
   693     {
   689      * lenient parsing, the parser may use heuristics to interpret inputs that
   699      * lenient parsing, the parser may use heuristics to interpret inputs that
   690      * do not precisely match this object's format.  With strict parsing,
   700      * do not precisely match this object's format.  With strict parsing,
   691      * inputs must match this object's format.
   701      * inputs must match this object's format.
   692      *
   702      *
   693      * <p>This method is equivalent to the following call.
   703      * <p>This method is equivalent to the following call.
   694      * <blockquote><pre>
   704      * <blockquote><pre>{@code
   695      *  getCalendar().setLenient(lenient)
   705      * getCalendar().setLenient(lenient)
   696      * </pre></blockquote>
   706      * }</pre></blockquote>
   697      *
   707      *
   698      * <p>This leniency value is overwritten by a call to {@link
   708      * <p>This leniency value is overwritten by a call to {@link
   699      * #setCalendar(java.util.Calendar) setCalendar()}.
   709      * #setCalendar(java.util.Calendar) setCalendar()}.
   700      *
   710      *
   701      * @param lenient when {@code true}, parsing is lenient
   711      * @param lenient when {@code true}, parsing is lenient
   707     }
   717     }
   708 
   718 
   709     /**
   719     /**
   710      * Tell whether date/time parsing is to be lenient.
   720      * Tell whether date/time parsing is to be lenient.
   711      * This method is equivalent to the following call.
   721      * This method is equivalent to the following call.
   712      * <blockquote><pre>
   722      * <blockquote><pre>{@code
   713      *  getCalendar().isLenient()
   723      * getCalendar().isLenient()
   714      * </pre></blockquote>
   724      * }</pre></blockquote>
   715      *
   725      *
   716      * @return {@code true} if the {@link #calendar} is lenient;
   726      * @return {@code true} if the {@link #calendar} is lenient;
   717      *         {@code false} otherwise.
   727      *         {@code false} otherwise.
   718      * @see java.util.Calendar#isLenient()
   728      * @see java.util.Calendar#isLenient()
   719      */
   729      */