📄 simpledateformat.java
字号:
/* * @(#)SimpleDateFormat.java 1.74 03/01/27 * * Copyright 2003 Sun Microsystems, Inc. All rights reserved. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. *//* * (C) Copyright Taligent, Inc. 1996 - All Rights Reserved * (C) Copyright IBM Corp. 1996-1998 - All Rights Reserved * * The original version of this source code and documentation is copyrighted * and owned by Taligent, Inc., a wholly-owned subsidiary of IBM. These * materials are provided under terms of a License Agreement between Taligent * and Sun. This technology is protected by multiple US and International * patents. This notice and attribution to Taligent may not be removed. * Taligent is a registered trademark of Taligent, Inc. * */package java.text;import java.util.TimeZone;import java.util.Calendar;import java.util.Date;import java.util.Locale;import java.util.ResourceBundle;import java.util.SimpleTimeZone;import java.util.GregorianCalendar;import java.io.ObjectInputStream;import java.io.InvalidObjectException;import java.io.IOException;import java.lang.ClassNotFoundException;import java.util.Hashtable;import java.lang.StringIndexOutOfBoundsException;import sun.text.resources.LocaleData;/** * <code>SimpleDateFormat</code> is a concrete class for formatting and * parsing dates in a locale-sensitive manner. It allows for formatting * (date -> text), parsing (text -> date), and normalization. * * <p> * <code>SimpleDateFormat</code> allows you to start by choosing * any user-defined patterns for date-time formatting. However, you * are encouraged to create a date-time formatter with either * <code>getTimeInstance</code>, <code>getDateInstance</code>, or * <code>getDateTimeInstance</code> in <code>DateFormat</code>. Each * of these class methods can return a date/time formatter initialized * with a default format pattern. You may modify the format pattern * using the <code>applyPattern</code> methods as desired. * For more information on using these methods, see * {@link DateFormat}. * * <h4>Date and Time Patterns</h4> * <p> * Date and time formats are specified by <em>date and time pattern</em> * strings. * Within date and time pattern strings, unquoted letters from * <code>'A'</code> to <code>'Z'</code> and from <code>'a'</code> to * <code>'z'</code> are interpreted as pattern letters representing the * components of a date or time string. * Text can be quoted using single quotes (<code>'</code>) to avoid * interpretation. * <code>"''"</code> represents a single quote. * All other characters are not interpreted; they're simply copied into the * output string during formatting or matched against the input string * during parsing. * <p> * The following pattern letters are defined (all other characters from * <code>'A'</code> to <code>'Z'</code> and from <code>'a'</code> to * <code>'z'</code> are reserved): * <blockquote> * <table border=0 cellspacing=3 cellpadding=0 summary="Chart shows pattern letters, date/time component, presentation, and examples."> * <tr bgcolor="#ccccff"> * <th align=left>Letter * <th align=left>Date or Time Component * <th align=left>Presentation * <th align=left>Examples * <tr> * <td><code>G</code> * <td>Era designator * <td><a href="#text">Text</a> * <td><code>AD</code> * <tr bgcolor="#eeeeff"> * <td><code>y</code> * <td>Year * <td><a href="#year">Year</a> * <td><code>1996</code>; <code>96</code> * <tr> * <td><code>M</code> * <td>Month in year * <td><a href="#month">Month</a> * <td><code>July</code>; <code>Jul</code>; <code>07</code> * <tr bgcolor="#eeeeff"> * <td><code>w</code> * <td>Week in year * <td><a href="#number">Number</a> * <td><code>27</code> * <tr> * <td><code>W</code> * <td>Week in month * <td><a href="#number">Number</a> * <td><code>2</code> * <tr bgcolor="#eeeeff"> * <td><code>D</code> * <td>Day in year * <td><a href="#number">Number</a> * <td><code>189</code> * <tr> * <td><code>d</code> * <td>Day in month * <td><a href="#number">Number</a> * <td><code>10</code> * <tr bgcolor="#eeeeff"> * <td><code>F</code> * <td>Day of week in month * <td><a href="#number">Number</a> * <td><code>2</code> * <tr> * <td><code>E</code> * <td>Day in week * <td><a href="#text">Text</a> * <td><code>Tuesday</code>; <code>Tue</code> * <tr bgcolor="#eeeeff"> * <td><code>a</code> * <td>Am/pm marker * <td><a href="#text">Text</a> * <td><code>PM</code> * <tr> * <td><code>H</code> * <td>Hour in day (0-23) * <td><a href="#number">Number</a> * <td><code>0</code> * <tr bgcolor="#eeeeff"> * <td><code>k</code> * <td>Hour in day (1-24) * <td><a href="#number">Number</a> * <td><code>24</code> * <tr> * <td><code>K</code> * <td>Hour in am/pm (0-11) * <td><a href="#number">Number</a> * <td><code>0</code> * <tr bgcolor="#eeeeff"> * <td><code>h</code> * <td>Hour in am/pm (1-12) * <td><a href="#number">Number</a> * <td><code>12</code> * <tr> * <td><code>m</code> * <td>Minute in hour * <td><a href="#number">Number</a> * <td><code>30</code> * <tr bgcolor="#eeeeff"> * <td><code>s</code> * <td>Second in minute * <td><a href="#number">Number</a> * <td><code>55</code> * <tr> * <td><code>S</code> * <td>Millisecond * <td><a href="#number">Number</a> * <td><code>978</code> * <tr bgcolor="#eeeeff"> * <td><code>z</code> * <td>Time zone * <td><a href="#timezone">General time zone</a> * <td><code>Pacific Standard Time</code>; <code>PST</code>; <code>GMT-08:00</code> * <tr> * <td><code>Z</code> * <td>Time zone * <td><a href="#rfc822timezone">RFC 822 time zone</a> * <td><code>-0800</code> * </table> * </blockquote> * Pattern letters are usually repeated, as their number determines the * exact presentation: * <ul> * <li><strong><a name="text">Text:</a></strong> * For formatting, if the number of pattern letters is 4 or more, * the full form is used; otherwise a short or abbreviated form * is used if available. * For parsing, both forms are accepted, independent of the number * of pattern letters. * <li><strong><a name="number">Number:</a></strong> * For formatting, the number of pattern letters is the minimum * number of digits, and shorter numbers are zero-padded to this amount. * For parsing, the number of pattern letters is ignored unless * it's needed to separate two adjacent fields. * <li><strong><a name="year">Year:</a></strong> * For formatting, if the number of pattern letters is 2, the year * is truncated to 2 digits; otherwise it is interpreted as a * <a href="#number">number</a>. * <p>For parsing, if the number of pattern letters is more than 2, * the year is interpreted literally, regardless of the number of * digits. So using the pattern "MM/dd/yyyy", "01/11/12" parses to * Jan 11, 12 A.D. * <p>For parsing with the abbreviated year pattern ("y" or "yy"), * <code>SimpleDateFormat</code> must interpret the abbreviated year * relative to some century. It does this by adjusting dates to be * within 80 years before and 20 years after the time the <code>SimpleDateFormat</code> * instance is created. For example, using a pattern of "MM/dd/yy" and a * <code>SimpleDateFormat</code> instance created on Jan 1, 1997, the string * "01/11/12" would be interpreted as Jan 11, 2012 while the string "05/04/64" * would be interpreted as May 4, 1964. * During parsing, only strings consisting of exactly two digits, as defined by * {@link Character#isDigit(char)}, will be parsed into the default century. * Any other numeric string, such as a one digit string, a three or more digit * string, or a two digit string that isn't all digits (for example, "-1"), is * interpreted literally. So "01/02/3" or "01/02/003" are parsed, using the * same pattern, as Jan 2, 3 AD. Likewise, "01/02/-3" is parsed as Jan 2, 4 BC. * <li><strong><a name="month">Month:</a></strong> * If the number of pattern letters is 3 or more, the month is * interpreted as <a href="#text">text</a>; otherwise, * it is interpreted as a <a href="#number">number</a>. * <li><strong><a name="timezone">General time zone:</a></strong> * Time zones are interpreted as <a href="#text">text</a> if they have * names. For time zones representing a GMT offset value, the * following syntax is used: * <pre> * <a name="GMTOffsetTimeZone"><i>GMTOffsetTimeZone:</i></a> * <code>GMT</code> <i>Sign</i> <i>Hours</i> <code>:</code> <i>Minutes</i> * <i>Sign:</i> one of * <code>+ -</code> * <i>Hours:</i> * <i>Digit</i> * <i>Digit</i> <i>Digit</i> * <i>Minutes:</i> * <i>Digit</i> <i>Digit</i> * <i>Digit:</i> one of * <code>0 1 2 3 4 5 6 7 8 9</code></pre> * <i>Hours</i> must be between 0 and 23, and <i>Minutes</i> must be between * 00 and 59. The format is locale independent and digits must be taken * from the Basic Latin block of the Unicode standard. * <p>For parsing, <a href="#rfc822timezone">RFC 822 time zones</a> are also * accepted. * <li><strong><a name="rfc822timezone">RFC 822 time zone:</a></strong> * For formatting, the RFC 822 4-digit time zone format is used: * <pre> * <i>RFC822TimeZone:</i> * <i>Sign</i> <i>TwoDigitHours</i> <i>Minutes</i> * <i>TwoDigitHours:</i> * <i>Digit Digit</i></pre> * <i>TwoDigitHours</i> must be between 00 and 23. Other definitions * are as for <a href="#timezone">general time zones</a>. * <p>For parsing, <a href="#timezone">general time zones</a> are also * accepted. * </ul> * <code>SimpleDateFormat</code> also supports <em>localized date and time * pattern</em> strings. In these strings, the pattern letters described above * may be replaced with other, locale dependent, pattern letters. * <code>SimpleDateFormat</code> does not deal with the localization of text * other than the pattern letters; that's up to the client of the class. * <p> * * <h4>Examples</h4> * * The following examples show how date and time patterns are interpreted in * the U.S. locale. The given date and time are 2001-07-04 12:08:56 local time * in the U.S. Pacific Time time zone. * <blockquote> * <table border=0 cellspacing=3 cellpadding=0 summary="Examples of date and time patterns interpreted in the U.S. locale"> * <tr bgcolor="#ccccff"> * <th align=left>Date and Time Pattern * <th align=left>Result * <tr> * <td><code>"yyyy.MM.dd G 'at' HH:mm:ss z"</code> * <td><code>2001.07.04 AD at 12:08:56 PDT</code> * <tr bgcolor="#eeeeff"> * <td><code>"EEE, MMM d, ''yy"</code> * <td><code>Wed, Jul 4, '01</code> * <tr> * <td><code>"h:mm a"</code> * <td><code>12:08 PM</code> * <tr bgcolor="#eeeeff"> * <td><code>"hh 'o''clock' a, zzzz"</code> * <td><code>12 o'clock PM, Pacific Daylight Time</code> * <tr> * <td><code>"K:mm a, z"</code> * <td><code>0:08 PM, PDT</code> * <tr bgcolor="#eeeeff"> * <td><code>"yyyyy.MMMMM.dd GGG hh:mm aaa"</code> * <td><code>02001.July.04 AD 12:08 PM</code> * <tr> * <td><code>"EEE, d MMM yyyy HH:mm:ss Z"</code> * <td><code>Wed, 4 Jul 2001 12:08:56 -0700</code> * <tr bgcolor="#eeeeff"> * <td><code>"yyMMddHHmmssZ"</code> * <td><code>010704120856-0700</code> * </table> * </blockquote> * * <h4><a name="synchronization">Synchronization</a></h4> * * <p> * Date formats are not synchronized. * It is recommended to create separate format instances for each thread. * If multiple threads access a format concurrently, it must be synchronized * externally. * * @see <a href="http://java.sun.com/docs/books/tutorial/i18n/format/simpleDateFormat.html">Java Tutorial</a> * @see java.util.Calendar * @see java.util.TimeZone * @see DateFormat * @see DateFormatSymbols * @version 1.74, 01/27/03 * @author Mark Davis, Chen-Lieh Huang, Alan Liu */public class SimpleDateFormat extends DateFormat { // the official serial version ID which says cryptically // which version we're compatible with static final long serialVersionUID = 4774881970558875024L; // the internal serial version which says which version was written // - 0 (default) for version up to JDK 1.1.3 // - 1 for version from JDK 1.1.4, which includes a new field static final int currentSerialVersion = 1; /** * The version of the serialized data on the stream. Possible values: * <ul> * <li><b>0</b> or not present on stream: JDK 1.1.3. This version * has no <code>defaultCenturyStart</code> on stream. * <li><b>1</b> JDK 1.1.4 or later. This version adds * <code>defaultCenturyStart</code>. * </ul> * When streaming out this class, the most recent format * and the highest allowable <code>serialVersionOnStream</code> * is written. * @serial * @since JDK1.1.4
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -