simpledateformat.html
来自「API資料大全」· HTML 代码 · 共 709 行 · 第 1/3 页
HTML
709 行
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Frameset//EN""http://www.w3.org/TR/REC-html40/frameset.dtd"><!--NewPage--><HTML><HEAD><!-- Generated by javadoc on Thu Apr 27 23:36:41 PDT 2000 --><TITLE>Java 2 Platform SE v1.3: Class SimpleDateFormat</TITLE><LINK REL ="stylesheet" TYPE="text/css" HREF="../../stylesheet.css" TITLE="Style"></HEAD><BODY BGCOLOR="white"><!-- ========== START OF NAVBAR ========== --><A NAME="navbar_top"><!-- --></A><TABLE BORDER="0" WIDTH="100%" CELLPADDING="1" CELLSPACING="0"><TR><TD COLSPAN=2 BGCOLOR="#EEEEFF" CLASS="NavBarCell1"><A NAME="navbar_top_firstrow"><!-- --></A><TABLE BORDER="0" CELLPADDING="0" CELLSPACING="3"> <TR ALIGN="center" VALIGN="top"> <TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1"> <A HREF="../../overview-summary.html"><FONT CLASS="NavBarFont1"><B>Overview</B></FONT></A> </TD> <TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1"> <A HREF="package-summary.html"><FONT CLASS="NavBarFont1"><B>Package</B></FONT></A> </TD> <TD BGCOLOR="#FFFFFF" CLASS="NavBarCell1Rev"> <FONT CLASS="NavBarFont1Rev"><B>Class</B></FONT> </TD> <TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1"> <A HREF="class-use/SimpleDateFormat.html"><FONT CLASS="NavBarFont1"><B>Use</B></FONT></A> </TD> <TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1"> <A HREF="package-tree.html"><FONT CLASS="NavBarFont1"><B>Tree</B></FONT></A> </TD> <TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1"> <A HREF="../../deprecated-list.html"><FONT CLASS="NavBarFont1"><B>Deprecated</B></FONT></A> </TD> <TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1"> <A HREF="../../index-files/index-1.html"><FONT CLASS="NavBarFont1"><B>Index</B></FONT></A> </TD> <TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1"> <A HREF="../../help-doc.html"><FONT CLASS="NavBarFont1"><B>Help</B></FONT></A> </TD> </TR></TABLE></TD><TD ALIGN="right" VALIGN="top" ROWSPAN=3><EM><b>Java<sup><font size=-2>TM</font></sup> 2 Platform<br>Std. Ed. v1.3</b></EM></TD></TR><TR><TD BGCOLOR="white" CLASS="NavBarCell2"><FONT SIZE="-2"> <A HREF="../../java/text/RuleBasedCollator.html"><B>PREV CLASS</B></A> <A HREF="../../java/text/StringCharacterIterator.html"><B>NEXT CLASS</B></A></FONT></TD><TD BGCOLOR="white" CLASS="NavBarCell2"><FONT SIZE="-2"> <A HREF="../../index.html" TARGET="_top"><B>FRAMES</B></A> <A HREF="SimpleDateFormat.html" TARGET="_top"><B>NO FRAMES</B></A></FONT></TD></TR><TR><TD VALIGN="top" CLASS="NavBarCell3"><FONT SIZE="-2"> SUMMARY: INNER | <A HREF="#fields_inherited_from_class_java.text.DateFormat">FIELD</A> | <A HREF="#constructor_summary">CONSTR</A> | <A HREF="#method_summary">METHOD</A></FONT></TD><TD VALIGN="top" CLASS="NavBarCell3"><FONT SIZE="-2">DETAIL: FIELD | <A HREF="#constructor_detail">CONSTR</A> | <A HREF="#method_detail">METHOD</A></FONT></TD></TR></TABLE><!-- =========== END OF NAVBAR =========== --><HR><!-- ======== START OF CLASS DATA ======== --><H2><FONT SIZE="-1">java.text</FONT><BR>Class SimpleDateFormat</H2><PRE><A HREF="../../java/lang/Object.html">java.lang.Object</A> | +--<A HREF="../../java/text/Format.html">java.text.Format</A> | +--<A HREF="../../java/text/DateFormat.html">java.text.DateFormat</A> | +--<B>java.text.SimpleDateFormat</B></PRE><DL><DT><B>All Implemented Interfaces:</B> <DD><A HREF="../../java/lang/Cloneable.html">Cloneable</A>, <A HREF="../../java/io/Serializable.html">Serializable</A></DD></DL><HR><DL><DT>public class <B>SimpleDateFormat</B><DT>extends <A HREF="../../java/text/DateFormat.html">DateFormat</A></DL><P><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 <A HREF="../../java/text/DateFormat.html"><CODE>DateFormat</CODE></A>. <p> <strong>Time Format Syntax:</strong> <p> To specify the time format use a <em>time pattern</em> string. In this pattern, all ASCII letters are reserved as pattern letters, which are defined as the following: <blockquote> <pre> Symbol Meaning Presentation Example ------ ------- ------------ ------- G era designator (Text) AD y year (Number) 1996 M month in year (Text & Number) July & 07 d day in month (Number) 10 h hour in am/pm (1~12) (Number) 12 H hour in day (0~23) (Number) 0 m minute in hour (Number) 30 s second in minute (Number) 55 S millisecond (Number) 978 E day in week (Text) Tuesday D day in year (Number) 189 F day of week in month (Number) 2 (2nd Wed in July) w week in year (Number) 27 W week in month (Number) 2 a am/pm marker (Text) PM k hour in day (1~24) (Number) 24 K hour in am/pm (0~11) (Number) 0 z time zone (Text) Pacific Standard Time ' escape for text (Delimiter) '' single quote (Literal) ' </pre> </blockquote> The count of pattern letters determine the format. <p> <strong>(Text)</strong>: 4 or more pattern letters--use full form, < 4--use short or abbreviated form if one exists. <p> <strong>(Number)</strong>: the minimum number of digits. Shorter numbers are zero-padded to this amount. Year is handled specially; that is, if the count of 'y' is 2, the Year will be truncated to 2 digits. <p> <strong>(Text & Number)</strong>: 3 or over, use text, otherwise use number. <p> Any characters in the pattern that are not in the ranges of ['a'..'z'] and ['A'..'Z'] will be treated as quoted text. For instance, characters like ':', '.', ' ', '#' and '@' will appear in the resulting time text even they are not embraced within single quotes. <p> A pattern containing any invalid pattern letter will result in a thrown exception during formatting or parsing. <p> <strong>Examples Using the US Locale:</strong> <blockquote> <pre> Format Pattern Result -------------- ------- "yyyy.MM.dd G 'at' hh:mm:ss z" ->> 1996.07.10 AD at 15:08:56 PDT "EEE, MMM d, ''yy" ->> Wed, July 10, '96 "h:mm a" ->> 12:08 PM "hh 'o''clock' a, zzzz" ->> 12 o'clock PM, Pacific Daylight Time "K:mm a, z" ->> 0:00 PM, PST "yyyyy.MMMMM.dd GGG hh:mm aaa" ->> 1996.July.10 AD 12:08 PM </pre> </blockquote> <strong>Code Sample:</strong> <blockquote> <pre> SimpleTimeZone pdt = new SimpleTimeZone(-8 * 60 * 60 * 1000, "PST"); pdt.setStartRule(DateFields.APRIL, 1, DateFields.SUNDAY, 2*60*60*1000); pdt.setEndRule(DateFields.OCTOBER, -1, DateFields.SUNDAY, 2*60*60*1000); <br> // Format the current time. SimpleDateFormat formatter = new SimpleDateFormat ("yyyy.MM.dd G 'at' hh:mm:ss a zzz"); Date currentTime_1 = new Date(); String dateString = formatter.format(currentTime_1); <br> // Parse the previous string back into a Date. ParsePosition pos = new ParsePosition(0); Date currentTime_2 = formatter.parse(dateString, pos); </pre> </blockquote> In the example, the time value <code>currentTime_2</code> obtained from parsing will be equal to <code>currentTime_1</code>. However, they may not be equal if the am/pm marker 'a' is left out from the format pattern while the "hour in am/pm" pattern symbol is used. This information loss can happen when formatting the time in PM. <p> When parsing a date string using the abbreviated year pattern ("y" or "yy"), SimpleDateFormat 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 SimpleDateFormat instance is created. For example, using a pattern of "MM/dd/yy" and a SimpleDateFormat 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 <A HREF="../../java/lang/Character.html#isDigit(char)"><CODE>Character.isDigit(char)</CODE></A>, 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. <p> If the year pattern has more than two 'y' characters, 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 time zones that have no names, use strings GMT+hours:minutes or GMT-hours:minutes. <p> The calendar defines what is the first day of the week, the first week of the year, whether hours are zero based or not (0 vs 12 or 24), and the time zone. There is one common decimal format to handle all the numbers; the digit count is handled programmatically according to the pattern.<P><DL><DT><B>See Also: </B><DD><A HREF="../../java/util/Calendar.html"><CODE>Calendar</CODE></A>, <A HREF="../../java/util/GregorianCalendar.html"><CODE>GregorianCalendar</CODE></A>, <A HREF="../../java/util/TimeZone.html"><CODE>TimeZone</CODE></A>, <A HREF="../../java/text/DateFormat.html"><CODE>DateFormat</CODE></A>, <A HREF="../../java/text/DateFormatSymbols.html"><CODE>DateFormatSymbols</CODE></A>, <A HREF="../../java/text/DecimalFormat.html"><CODE>DecimalFormat</CODE></A>, <A HREF="../../serialized-form.html#java.text.SimpleDateFormat">Serialized Form</A></DL><HR><P><!-- ======== INNER CLASS SUMMARY ======== --><!-- =========== FIELD SUMMARY =========== --><A NAME="fields_inherited_from_class_java.text.DateFormat"><!-- --></A><TABLE BORDER="1" CELLPADDING="3" CELLSPACING="0" WIDTH="100%"><TR BGCOLOR="#EEEEFF" CLASS="TableSubHeadingColor"><TD><B>Fields inherited from class java.text.<A HREF="../../java/text/DateFormat.html">DateFormat</A></B></TD></TR><TR BGCOLOR="white" CLASS="TableRowColor"><TD><CODE><A HREF="../../java/text/DateFormat.html#AM_PM_FIELD">AM_PM_FIELD</A>, <A HREF="../../java/text/DateFormat.html#calendar">calendar</A>, <A HREF="../../java/text/DateFormat.html#DATE_FIELD">DATE_FIELD</A>, <A HREF="../../java/text/DateFormat.html#DAY_OF_WEEK_FIELD">DAY_OF_WEEK_FIELD</A>, <A HREF="../../java/text/DateFormat.html#DAY_OF_WEEK_IN_MONTH_FIELD">DAY_OF_WEEK_IN_MONTH_FIELD</A>, <A HREF="../../java/text/DateFormat.html#DAY_OF_YEAR_FIELD">DAY_OF_YEAR_FIELD</A>, <A HREF="../../java/text/DateFormat.html#DEFAULT">DEFAULT</A>, <A HREF="../../java/text/DateFormat.html#ERA_FIELD">ERA_FIELD</A>, <A HREF="../../java/text/DateFormat.html#FULL">FULL</A>, <A HREF="../../java/text/DateFormat.html#HOUR_OF_DAY0_FIELD">HOUR_OF_DAY0_FIELD</A>, <A HREF="../../java/text/DateFormat.html#HOUR_OF_DAY1_FIELD">HOUR_OF_DAY1_FIELD</A>, <A HREF="../../java/text/DateFormat.html#HOUR0_FIELD">HOUR0_FIELD</A>, <A HREF="../../java/text/DateFormat.html#HOUR1_FIELD">HOUR1_FIELD</A>, <A HREF="../../java/text/DateFormat.html#LONG">LONG</A>, <A HREF="../../java/text/DateFormat.html#MEDIUM">MEDIUM</A>, <A HREF="../../java/text/DateFormat.html#MILLISECOND_FIELD">MILLISECOND_FIELD</A>, <A HREF="../../java/text/DateFormat.html#MINUTE_FIELD">MINUTE_FIELD</A>, <A HREF="../../java/text/DateFormat.html#MONTH_FIELD">MONTH_FIELD</A>, <A HREF="../../java/text/DateFormat.html#numberFormat">numberFormat</A>, <A HREF="../../java/text/DateFormat.html#SECOND_FIELD">SECOND_FIELD</A>, <A HREF="../../java/text/DateFormat.html#SHORT">SHORT</A>, <A HREF="../../java/text/DateFormat.html#TIMEZONE_FIELD">TIMEZONE_FIELD</A>, <A HREF="../../java/text/DateFormat.html#WEEK_OF_MONTH_FIELD">WEEK_OF_MONTH_FIELD</A>, <A HREF="../../java/text/DateFormat.html#WEEK_OF_YEAR_FIELD">WEEK_OF_YEAR_FIELD</A>, <A HREF="../../java/text/DateFormat.html#YEAR_FIELD">YEAR_FIELD</A></CODE></TD></TR></TABLE>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?