public class SimpleDateFormat extends DateFormat
SimpleDateFormat是一个具体的类,用于以区域设置敏感的方式格式化和解析日期。
它允许格式化(日期文本),解析(文本日期)和归一化。
SimpleDateFormat允许您从选择日期时间格式化的任何用户定义的模式开始。 不过,建议您创建一个日期-时间格式有两种getTimeInstance , getDateInstance ,或getDateTimeInstance在DateFormat 。 这些类方法中的每一个都可以返回使用默认格式模式初始化的日期/时间格式化程序。 您可以根据需要使用applyPattern方法修改格式模式。 有关使用这些方法的更多信息,请参见DateFormat 。
日期和时间格式由日期和时间模式字符串指定。 在日期和时间模式字符串中,从'A'到'Z'和从'a'到'z'的非引号的字母被解释为表示日期或时间字符串的组件的模式字母。 可以使用单引号( ' )引用文本,以避免解释。 "''"代表单引号。 所有其他字符不被解释; 在格式化过程中,它们只是复制到输出字符串中,或者在解析过程中与输入字符串匹配。
定义了以下图案字母(所有其他字符从'A'到'Z'和从'a'到'z'被保留):
模式字母通常重复,因为它们的数字确定了准确的表示:
Letter Date or Time Component Presentation Examples GEra designator Text ADyYear Year 1996;96YWeek year Year 2009;09MMonth in year (context sensitive) Month July;Jul;07LMonth in year (standalone form) Month July;Jul;07wWeek in year Number 27WWeek in month Number 2DDay in year Number 189dDay in month Number 10FDay of week in month Number 2EDay name in week Text Tuesday;TueuDay number of week (1 = Monday, ..., 7 = Sunday) Number 1aAm/pm marker Text PMHHour in day (0-23) Number 0kHour in day (1-24) Number 24KHour in am/pm (0-11) Number 0hHour in am/pm (1-12) Number 12mMinute in hour Number 30sSecond in minute Number 55SMillisecond Number 978zTime zone General time zone Pacific Standard Time;PST;GMT-08:00ZTime zone RFC 822 time zone -0800XTime zone ISO 8601 time zone -08;-0800;-08:00
Calendar是公历,则应用以下规则。 SimpleDateFormat必须解释相对于某个世纪的缩写年份。 它是通过将日期调整为在创建SimpleDateFormat实例之后的80年之前和20年之后进行的。 例如,使用1997年1月1日创建的“MM / dd / yy”模式和SimpleDateFormat实例,字符串“01/11/12”将被解释为2012年1月11日,而字符串“05/04 / 64“将被解释为1964年5月4日。在解析期间,只有由Character.isDigit(char)定义的两个数字组成的字符串将被解析为默认世纪。 任何其他数字字符串,例如一位数字字符串,三位或三位以上数字字符串,或两位数字字符串(不全部为数字)(例如“-1”),均按字面解释。 所以“01/02/3”或“01/02/003”的解析方式与公元3年1月2日相同。 同样,“01/02 / -3”在公元前4年1月2日被解析。 'Y'被指定,并且calendar不支持任何week years ,则使用日历年( 'y' )。 可以通过致电getCalendar()测试周年的支持。 isWeekDateSupported() 。 DateFormatSymbols已经与构造明确设置SimpleDateFormat(String, DateFormatSymbols)或方法setDateFormatSymbols(DateFormatSymbols) ,由给定月份名称DateFormatSymbols被使用。 GMTOffsetTimeZone:
GMT Sign Hours : Minutes
Sign: one of
+ -
Hours:
Digit
Digit Digit
Minutes:
Digit Digit
Digit: one of
0 1 2 3 4 5 6 7 8 9 小时数必须在0到23之间, 分钟必须介于00和59之间。格式不区分大小写,数字必须取自Unicode标准的基本拉丁方块。 为了解析, RFC 822 time zones也被接受。
RFC822TimeZone:
Sign TwoDigitHours Minutes
TwoDigitHours:
Digit Digit TwoDigitHours必须介于00和23之间。其他定义与general time zones相同 。 为了解析, general time zones也被接受。
ISO8601TimeZone:
OneLetterISO8601TimeZone
TwoLetterISO8601TimeZone
ThreeLetterISO8601TimeZone
OneLetterISO8601TimeZone:
Sign TwoDigitHours
Z
TwoLetterISO8601TimeZone:
Sign TwoDigitHours Minutes
Z
ThreeLetterISO8601TimeZone:
Sign TwoDigitHours : Minutes
Z 其他定义与general time zones或RFC 822 time zones相同 。 对于格式化,如果GMT的偏移值为0,则生成"Z" 。 如果模式字母的数量为1,则忽略小时数。 例如,如果模式为"X" ,时区为"GMT+05:30" ,则生成"+05" 。
为了解析, "Z"被解析为UTC时区指示符。 General time zones 不被接受。
如果模式字母的数目是4或更多, IllegalArgumentException被构建时抛出SimpleDateFormat或applying a pattern 。
SimpleDateFormat还支持本地化的日期和时间模式字符串。
在这些字符串中,上述的图案字母可以被替换为其他与区域相关的图案字母。
SimpleDateFormat不处理除图案字母之外的文本的本地化;
这取决于课堂的客户。
Date and Time Pattern Result "yyyy.MM.dd G 'at' HH:mm:ss z"2001.07.04 AD at 12:08:56 PDT"EEE, MMM d, ''yy"Wed, Jul 4, '01"h:mm a"12:08 PM"hh 'o''clock' a, zzzz"12 o'clock PM, Pacific Daylight Time"K:mm a, z"0:08 PM, PDT"yyyyy.MMMMM.dd GGG hh:mm aaa"02001.July.04 AD 12:08 PM"EEE, d MMM yyyy HH:mm:ss Z"Wed, 4 Jul 2001 12:08:56 -0700"yyMMddHHmmssZ"010704120856-0700"yyyy-MM-dd'T'HH:mm:ss.SSSZ"2001-07-04T12:08:56.235-0700"yyyy-MM-dd'T'HH:mm:ss.SSSXXX"2001-07-04T12:08:56.235-07:00"YYYY-'W'ww-u"2001-W27-3
日期格式不同步。 建议为每个线程创建单独的格式实例。 如果多个线程同时访问格式,则必须在外部进行同步。
Calendar , TimeZone , DateFormat , DateFormatSymbols , Serialized Form
DateFormat.FieldAM_PM_FIELD, calendar, DATE_FIELD, DAY_OF_WEEK_FIELD, DAY_OF_WEEK_IN_MONTH_FIELD, DAY_OF_YEAR_FIELD, DEFAULT, ERA_FIELD, FULL, HOUR_OF_DAY0_FIELD, HOUR_OF_DAY1_FIELD, HOUR0_FIELD, HOUR1_FIELD, LONG, MEDIUM, MILLISECOND_FIELD, MINUTE_FIELD, MONTH_FIELD, numberFormat, SECOND_FIELD, SHORT, TIMEZONE_FIELD, WEEK_OF_MONTH_FIELD, WEEK_OF_YEAR_FIELD, YEAR_FIELD| Constructor and Description |
|---|
SimpleDateFormat()
构造一个
SimpleDateFormat使用默认模式和日期格式符号为默认的
FORMAT区域设置。
|
SimpleDateFormat(String pattern)
使用给定模式
SimpleDateFormat并使用默认的
FORMAT语言环境的默认日期格式符号。
|
SimpleDateFormat(String pattern, DateFormatSymbols formatSymbols)
使用给定的模式和日期格式符号构造一个
SimpleDateFormat 。
|
SimpleDateFormat(String pattern, Locale locale)
构造一个
SimpleDateFormat使用给定的模式和给定的区域设置的默认日期格式符号。
|
| Modifier and Type | Method and Description |
|---|---|
void |
applyLocalizedPattern(String pattern)
将给定的本地化模式字符串应用于此日期格式。
|
void |
applyPattern(String pattern)
将给定的模式字符串应用于此日期格式。
|
Object |
clone()
创建一个这个
SimpleDateFormat的副本。
|
boolean |
equals(Object obj)
将给定的对象与此
SimpleDateFormat进行比较以获得相等性。
|
StringBuffer |
format(Date date, StringBuffer toAppendTo, FieldPosition pos)
将给定的
Date成日期/时间字符串,并将结果追加到给定的
StringBuffer 。
|
AttributedCharacterIterator |
formatToCharacterIterator(Object obj)
格式化一个对象,生成一个
AttributedCharacterIterator 。
|
Date |
get2DigitYearStart()
返回100年期间的开始日期,2位数年份被解释为在内。
|
DateFormatSymbols |
getDateFormatSymbols()
获取此日期格式的日期和时间格式符号的副本。
|
int |
hashCode()
返回此
SimpleDateFormat对象的哈希码值。
|
Date |
parse(String text, ParsePosition pos)
从字符串中解析文本以产生一个
Date 。
|
void |
set2DigitYearStart(Date startDate)
设置100年期间,两位数年份将被解释为在用户指定的日期开始。
|
void |
setDateFormatSymbols(DateFormatSymbols newFormatSymbols)
设置此日期格式的日期和时间格式符号。
|
String |
toLocalizedPattern()
返回描述此日期格式的本地化模式字符串。
|
String |
toPattern()
返回描述此日期格式的模式字符串。
|
format, format, getAvailableLocales, getCalendar, getDateInstance, getDateInstance, getDateInstance, getDateTimeInstance, getDateTimeInstance, getDateTimeInstance, getInstance, getNumberFormat, getTimeInstance, getTimeInstance, getTimeInstance, getTimeZone, isLenient, parse, parseObject, setCalendar, setLenient, setNumberFormat, setTimeZoneformat, parseObjectpublic SimpleDateFormat()
public SimpleDateFormat(String pattern)
SimpleDateFormat并使用默认的FORMAT语言环境的默认日期格式符号。
注意:此构造函数可能不支持所有区域设置。
要全面覆盖,请使用DateFormat类中的工厂方法。
这相当于调用SimpleDateFormat(pattern, Locale.getDefault(Locale.Category.FORMAT)) 。
pattern - 描述日期和时间格式的模式
NullPointerException - 如果给定的模式为空
IllegalArgumentException - 如果给定的模式无效
Locale.getDefault(java.util.Locale.Category) ,
Locale.Category.FORMAT
public SimpleDateFormat(String pattern, Locale locale)
pattern - 描述日期和时间格式的模式
locale - 应使用日期格式符号的区域设置
NullPointerException - 如果给定的模式或区域设置为空
IllegalArgumentException - 如果给定的模式无效
public SimpleDateFormat(String pattern, DateFormatSymbols formatSymbols)
SimpleDateFormat 。
pattern - 描述日期和时间格式的模式
formatSymbols - 要用于格式化的日期格式符号
NullPointerException - 如果给定的模式或formatSymbols为空
IllegalArgumentException - 如果给定的模式无效
public void set2DigitYearStart(Date startDate)
startDate - 在解析期间,两位数字将被放置在
startDate到
startDate + 100 years的范围内。
get2DigitYearStart()
public Date get2DigitYearStart()
set2DigitYearStart(java.util.Date)
public StringBuffer format(Date date, StringBuffer toAppendTo, FieldPosition pos)
Date成日期/时间字符串,并将结果附加到给定的
StringBuffer 。
format在
DateFormat
date - 要格式化为日期时间字符串的日期时间值。
toAppendTo - 要附加新的日期时间文本。
pos - 格式化位置。
在输入:对齐字段,如果需要。
输出:对齐字段的偏移量。
NullPointerException - 如果给定的
date是
null 。
public AttributedCharacterIterator formatToCharacterIterator(Object obj)
AttributedCharacterIterator 。
您可以使用返回的AttributedCharacterIterator构建生成的字符串,以及确定有关生成的字符串的信息。
AttributedCharacterIterator的每个属性的关键将是类型的DateFormat.Field ,与相应的属性值是一样的属性键。
formatToCharacterIterator在
Format
obj - 要格式化的对象
NullPointerException - 如果obj为空。
IllegalArgumentException - 如果格式化格式化给定对象,或格式的模式字符串无效。
public Date parse(String text, ParsePosition pos)
Date 。
该方法尝试从pos给出的索引开始解析文本。 如果解析成功,那么pos的索引将在使用最后一个字符之后更新为索引(解析不一定使用字符串末尾的所有字符),并返回解析的日期。 更新的pos可用于指示下一次调用此方法的起始点。 如果发生错误,则pos的索引不改变, pos的错误索引设置为发生错误的pos的索引,返回null。
这个解析操作使用calendar来生成一个Date 。 在解析之前,所有calendar的日期时间字段为cleared ,日期时间字段的默认值为calendar用于任何缺少的日期时间信息。 例如,被分析的年份值Date是1970年GregorianCalendar如果没有年份值从解析操作说明。 所述TimeZone值可以根据给定的模式和在时区值被覆盖, text 。 任何TimeZone先前已通过调用设定值setTimeZone可能需要恢复的进一步行动。
parse在
DateFormat
text - A
String ,其中一部分应解析。
pos - 具有
ParsePosition的索引和错误索引信息的
ParsePosition对象。
Date从字符串中解析出来。
万一出错,返回null。
NullPointerException - 如果
text或
pos为空。
public String toPattern()
public String toLocalizedPattern()
public void applyPattern(String pattern)
pattern - 此日期格式的新日期和时间模式
NullPointerException - 如果给定的模式为空
IllegalArgumentException - 如果给定的模式无效
public void applyLocalizedPattern(String pattern)
pattern - 要映射到此格式的新日期和时间格式模式的字符串
NullPointerException - 如果给定的模式为空
IllegalArgumentException - 如果给定的模式无效
public DateFormatSymbols getDateFormatSymbols()
setDateFormatSymbols(java.text.DateFormatSymbols)
public void setDateFormatSymbols(DateFormatSymbols newFormatSymbols)
newFormatSymbols - 新的日期和时间格式符号
NullPointerException - 如果给定的newFormatSymbols为空
getDateFormatSymbols()
public Object clone()
SimpleDateFormat的副本。
这也克隆了格式的日期格式符号。
clone在
DateFormat
SimpleDateFormat一个克隆
Cloneable
public int hashCode()
SimpleDateFormat对象的哈希码值。
hashCode在
DateFormat
SimpleDateFormat对象的哈希码值。
Object.equals(java.lang.Object) ,
System.identityHashCode(java.lang.Object)
public boolean equals(Object obj)
SimpleDateFormat进行比较以获得相等性。
equals在
DateFormat
obj - 与之比较的参考对象。
SimpleDateFormat
Object.hashCode(),
HashMap
Submit a bug or feature
For further API reference and developer documentation, see Java SE Documentation. That documentation contains more detailed, developer-targeted descriptions, with conceptual overviews, definitions of terms, workarounds, and working code examples.
Copyright © 1993, 2014, Oracle and/or its affiliates. All rights reserved.