public abstract class DateFormat
extends Format
| java.lang.Object | ||
| java.text.Format | ||
| java.text.DateFormat | ||
| |
DateFormat是日期/时间格式化子类的抽象类,它以独立于语言的方式格式化和解析日期或时间。 日期/时间格式化子类(例如SimpleDateFormat )允许格式化(即日期 - >文本),解析(文本 - >日期)和规范化。 日期表示为Date对象或自1970年1月1日00:00:00 GMT以来的毫秒。
DateFormat提供了许多基于默认或给定语言环境和一些格式化样式获取默认日期/时间格式化程序的类方法。 格式化风格包括FULL , LONG , MEDIUM ,并SHORT 。 方法描述中提供了更多细节和使用这些样式的示例。
DateFormat可帮助您为任何区域设置格式和解析日期。 您的代码可以完全独立于语言环境约定数月,一周中的几天甚至日历格式:月球与太阳能。
要格式化当前语言环境的日期,请使用以下静态工厂方法之一:
myString = DateFormat.getDateInstance().format(myDate);
如果您格式化多个日期,获取格式并多次使用格式会更有效,以便系统不必多次获取有关本地语言和国家/地区惯例的信息。
DateFormat df = DateFormat.getDateInstance();
for (int i = 0; i < myDate.length; ++i) {
output.println(df.format(myDate[i]) + "; ");
}
要格式化其他区域设置的日期,请在 getDateInstance()的调用中指定它。
DateFormat df = DateFormat.getDateInstance(DateFormat.LONG, Locale.FRANCE);
你也可以使用DateFormat解析。
myDate = df.parse(myString);
使用getDateInstance获取该国家的正常日期格式。 还有其他静态工厂方法可用。 使用getTimeInstance获取该国家的时间格式。 使用getDateTimeInstance获取日期和时间格式。 您可以将不同选项传递给这些工厂方法以控制结果的长度; 从SHORT到MEDIUM到LONG到FULL 。 确切的结果取决于语言环境,但通常:
SHORT is completely numeric, such as 12.13.52 or 3:30pm MEDIUM is longer, such as Jan 12, 1952 LONG is longer, such as January 12, 1952 or 3:30:32pm FULL is pretty completely specified, such as Tuesday, April 12, 1952 AD or 3:30:42pm PST. 如果您愿意,您还可以根据格式设置时区。 如果你想在格式或分析更精确的控制,(或者希望给用户更多的控制),可以尝试铸造DateFormat你从工厂方法到获得SimpleDateFormat 。 这对大多数国家都有效; 只要记得把它放在try区域,以防遇到不寻常的问题。
您也可以使用 ParsePosition和 FieldPosition的解析和格式方法的 FieldPosition来让您
日期格式不同步。 建议为每个线程创建单独的格式实例。 如果多个线程同时访问一个格式,它必须在外部同步。
Nested classes |
|
|---|---|
class |
DateFormat.Field 定义了用于作为属性键常数 |
常量(Constants) |
|
|---|---|
int |
AM_PM_FIELD AM_PM字段对齐的常用常量。 |
int |
DATE_FIELD DATE字段对齐的常用常量。 |
int |
DAY_OF_WEEK_FIELD DAY_OF_WEEK字段对齐的常用常量。 |
int |
DAY_OF_WEEK_IN_MONTH_FIELD DAY_OF_WEEK_IN_MONTH字段对齐的常用常量。 |
int |
DAY_OF_YEAR_FIELD DAY_OF_YEAR字段对齐的常用常量。 |
int |
DEFAULT 常量默认样式模式。 |
int |
ERA_FIELD ERA字段对齐的常用常数。 |
int |
FULL 不变的全样式模式。 |
int |
HOUR0_FIELD 基于零的HOUR字段对齐的有用常量。 |
int |
HOUR1_FIELD 基于一个小时字段对齐的有用常量。 |
int |
HOUR_OF_DAY0_FIELD 基于零的HOUR_OF_DAY字段对齐的有用常量。 |
int |
HOUR_OF_DAY1_FIELD 一种基于HOUR_OF_DAY字段对齐的有用常量。 |
int |
LONG 用于长型图案的常量。 |
int |
MEDIUM 中等风格模式的常量。 |
int |
MILLISECOND_FIELD MILLISECOND字段对齐的有用常量。 |
int |
MINUTE_FIELD MINUTE字段对齐的常用常量。 |
int |
MONTH_FIELD MONTH字段对齐的常用常量。 |
int |
SECOND_FIELD SECOND字段对齐的有用常量。 |
int |
SHORT 用于短样式的常量。 |
int |
TIMEZONE_FIELD TIMEZONE字段对齐的常用常量。 |
int |
WEEK_OF_MONTH_FIELD WEEK_OF_MONTH字段对齐的常用常量。 |
int |
WEEK_OF_YEAR_FIELD WEEK_OF_YEAR字段对齐的常用常量。 |
int |
YEAR_FIELD YEAR字段对齐的常用常量。 |
Fields |
|
|---|---|
protected Calendar |
calendar 用于计算日期时间字段和时间的 |
protected NumberFormat |
numberFormat
|
Protected constructors |
|
|---|---|
DateFormat() 创建一个新的日期格式。 |
|
公共方法(Public methods) |
|
|---|---|
Object |
clone() 覆盖可复制 |
boolean |
equals(Object obj) 覆盖等于 |
final StringBuffer |
format(Object obj, StringBuffer toAppendTo, FieldPosition fieldPosition) 覆盖格式。 |
abstract StringBuffer |
format(Date date, StringBuffer toAppendTo, FieldPosition fieldPosition) 将日期格式化为日期/时间字符串。 |
final String |
format(Date date) 将日期格式化为日期/时间字符串。 |
static Locale[] |
getAvailableLocales() 返回 |
Calendar |
getCalendar() 获取与此日期/时间格式化程序关联的日历。 |
static final DateFormat |
getDateInstance(int style, Locale aLocale) 使用给定语言环境的给定格式样式获取日期格式程序。 |
static final DateFormat |
getDateInstance(int style) 获取默认语言环境的给定格式样式的日期格式化程序。 |
static final DateFormat |
getDateInstance() 获取默认语言环境的默认格式设置的日期格式化程序。 |
static final DateFormat |
getDateTimeInstance(int dateStyle, int timeStyle) 使用默认语言环境的给定日期和时间格式化样式获取日期/时间格式化程序。 |
static final DateFormat |
getDateTimeInstance(int dateStyle, int timeStyle, Locale aLocale) 使用给定语言环境的给定格式样式获取日期/时间格式器。 |
static final DateFormat |
getDateTimeInstance() 获取默认语言环境的默认格式样式的日期/时间格式化程序。 |
static final DateFormat |
getInstance() 获取日期和时间都使用SHORT样式的默认日期/时间格式器。 |
NumberFormat |
getNumberFormat() 获取此日期/时间格式化程序用于格式化和解析时间的数字格式化程序。 |
static final DateFormat |
getTimeInstance(int style) 使用默认语言环境的给定格式样式获取时间格式器。 |
static final DateFormat |
getTimeInstance(int style, Locale aLocale) 使用给定语言环境的给定格式样式获取时间格式器。 |
static final DateFormat |
getTimeInstance() 获取默认语言环境的默认格式样式的时间格式化程序。 |
TimeZone |
getTimeZone() 获取时区。 |
int |
hashCode() 重写hashCode |
boolean |
isLenient() 告诉日期/时间解析是否宽松。 |
Date |
parse(String source) 从给定字符串的开头解析文本以生成日期。 |
abstract Date |
parse(String source, ParsePosition pos) 根据给定的解析位置解析日期/时间字符串。 |
Object |
parseObject(String source, ParsePosition pos) 解析字符串中的文本以生成 |
void |
setCalendar(Calendar newCalendar) 设置该日期格式使用的日历。 |
void |
setLenient(boolean lenient) 指定日期/时间解析是否是宽松的。 |
void |
setNumberFormat(NumberFormat newNumberFormat) 允许您设置数字格式器。 |
void |
setTimeZone(TimeZone zone) 设置此 |
继承方法(Inherited methods) |
|
|---|---|
java.text.Format
|
|
java.lang.Object
|
|
int AM_PM_FIELD
AM_PM字段对齐的常用常量。 用于日期/时间格式的FieldPosition。
常量值:14(0x0000000e)
int DATE_FIELD
DATE字段对齐的常用常量。 用于日期/时间格式的FieldPosition。
常量值:3(0x00000003)
int DAY_OF_WEEK_FIELD
DAY_OF_WEEK字段对齐的常用常量。 用于日期/时间格式的FieldPosition。
常量值:9(0x00000009)
int DAY_OF_WEEK_IN_MONTH_FIELD
DAY_OF_WEEK_IN_MONTH字段对齐的常用常量。 用于日期/时间格式的FieldPosition。
常量值:11(0x0000000b)
int DAY_OF_YEAR_FIELD
DAY_OF_YEAR字段对齐的常用常量。 用于日期/时间格式的FieldPosition。
常量值:10(0x0000000a)
int ERA_FIELD
ERA字段对齐的常用常数。 用于日期/时间格式的FieldPosition。
常量值:0(0x00000000)
int HOUR0_FIELD
基于零的HOUR字段对齐的有用常量。 用于日期/时间格式的FieldPosition。 HOUR0_FIELD用于基于零的12小时时钟。 例如,下午11:30 + 1小时产生00:30 AM。
常量值:16(0x00000010)
int HOUR1_FIELD
基于一个小时字段对齐的有用常量。 用于日期/时间格式的FieldPosition。 HOUR1_FIELD用于一个基于12小时制的时钟。 例如,上午12:30下午11:30 PM + 1小时。
常量值:15(0x0000000f)
int HOUR_OF_DAY0_FIELD
基于零的HOUR_OF_DAY字段对齐的有用常量。 用于日期/时间格式的FieldPosition。 HOUR_OF_DAY0_FIELD用于基于零的24小时时钟。 例如,23:59 + 01:00的结果是00:59。
常量值:5(0x00000005)
int HOUR_OF_DAY1_FIELD
一种基于HOUR_OF_DAY字段对齐的有用常量。 用于日期/时间格式的FieldPosition。 HOUR_OF_DAY1_FIELD用于基于一个24小时制的时钟。 例如,23:59 + 01:00在24:59结果。
常量值:4(0x00000004)
int MILLISECOND_FIELD
MILLISECOND字段对齐的有用常量。 用于日期/时间格式的FieldPosition。
常量值:8(0x00000008)
int MINUTE_FIELD
MINUTE字段对齐的常用常量。 用于日期/时间格式的FieldPosition。
常数值:6(0x00000006)
int MONTH_FIELD
MONTH字段对齐的常用常量。 用于日期/时间格式的FieldPosition。
常量值:2(0x00000002)
int SECOND_FIELD
SECOND字段对齐的有用常量。 用于日期/时间格式的FieldPosition。
常量值:7(0x00000007)
int TIMEZONE_FIELD
TIMEZONE字段对齐的常用常量。 用于日期/时间格式的FieldPosition。
常量值:17(0x00000011)
int WEEK_OF_MONTH_FIELD
WEEK_OF_MONTH字段对齐的常用常量。 用于日期/时间格式的FieldPosition。
常量值:13(0x0000000d)
int WEEK_OF_YEAR_FIELD
WEEK_OF_YEAR字段对齐的常用常量。 用于日期/时间格式的FieldPosition。
常量值:12(0x0000000c)
int YEAR_FIELD
YEAR字段对齐的常用常量。 用于日期/时间格式的FieldPosition。
常数值:1(0x00000001)
NumberFormat numberFormat
DateFormat用于在日期和时间格式化数字的数字格式器。 子类应将其初始化为适合与此DateFormat关联的语言环境的数字格式。
boolean equals (Object obj)
覆盖等于
| 参数(Parameters) | |
|---|---|
obj |
Object: the reference object with which to compare. |
| 返回(Returns) | |
|---|---|
boolean |
true if this object is the same as the obj argument; false otherwise. |
StringBuffer format (Object obj, StringBuffer toAppendTo, FieldPosition fieldPosition)
覆盖格式。 将时间对象格式化为时间字符串。 时间对象的例子是以毫秒表示的时间值和Date对象。
| 参数(Parameters) | |
|---|---|
obj |
Object: must be a Number or a Date. |
toAppendTo |
StringBuffer: the string buffer for the returning time string. |
fieldPosition |
FieldPosition: keeps track of the position of the field within the returned string. On input: an alignment field, if desired. On output: the offsets of the alignment field. For example, given a time text "1996.07.10 AD at 15:08:56 PDT", if the given fieldPosition is DateFormat.YEAR_FIELD, the begin index and end index of fieldPosition will be set to 0 and 4, respectively. Notice that if the same time field appears more than once in a pattern, the fieldPosition will be set for the first occurrence of that time field. For instance, formatting a Date to the time string "1 PM PDT (Pacific Daylight Time)" using the pattern "h a z (zzzz)" and the alignment field DateFormat.TIMEZONE_FIELD, the begin index and end index of fieldPosition will be set to 5 and 8, respectively, for the first occurrence of the timezone pattern character 'z'. |
| 返回(Returns) | |
|---|---|
StringBuffer |
the string buffer passed in as toAppendTo, with formatted text appended. |
也可以看看:
StringBuffer format (Date date, StringBuffer toAppendTo, FieldPosition fieldPosition)
将日期格式化为日期/时间字符串。
| 参数(Parameters) | |
|---|---|
date |
Date: a Date to be formatted into a date/time string. |
toAppendTo |
StringBuffer: the string buffer for the returning date/time string. |
fieldPosition |
FieldPosition: keeps track of the position of the field within the returned string. On input: an alignment field, if desired. On output: the offsets of the alignment field. For example, given a time text "1996.07.10 AD at 15:08:56 PDT", if the given fieldPosition is DateFormat.YEAR_FIELD, the begin index and end index of fieldPosition will be set to 0 and 4, respectively. Notice that if the same time field appears more than once in a pattern, the fieldPosition will be set for the first occurrence of that time field. For instance, formatting a Date to the time string "1 PM PDT (Pacific Daylight Time)" using the pattern "h a z (zzzz)" and the alignment field DateFormat.TIMEZONE_FIELD, the begin index and end index of fieldPosition will be set to 5 and 8, respectively, for the first occurrence of the timezone pattern character 'z'. |
| 返回(Returns) | |
|---|---|
StringBuffer |
the string buffer passed in as toAppendTo, with formatted text appended. |
String format (Date date)
将日期格式化为日期/时间字符串。
| 参数(Parameters) | |
|---|---|
date |
Date: the time value to be formatted into a time string. |
| 返回(Returns) | |
|---|---|
String |
the formatted time string. |
Locale[] getAvailableLocales ()
返回get*Instance方法可返回本地化实例的所有语言环境的数组。 返回的数组表示Java运行时支持的语言环境联合以及安装的DateFormatProvider实现。 它必须包含至少一个等于Locale.US的Locale实例。
| 返回(Returns) | |
|---|---|
Locale[] |
An array of locales for which localized DateFormat instances are available. |
Calendar getCalendar ()
获取与此日期/时间格式化程序关联的日历。
| 返回(Returns) | |
|---|---|
Calendar |
the calendar associated with this date/time formatter. |
DateFormat getDateInstance (int style, Locale aLocale)
使用给定语言环境的给定格式样式获取日期格式程序。
| 参数(Parameters) | |
|---|---|
style |
int: the given formatting style. For example, SHORT for "M/d/yy" in the US locale. |
aLocale |
Locale: the given locale. |
| 返回(Returns) | |
|---|---|
DateFormat |
a date formatter. |
DateFormat getDateInstance (int style)
获取默认语言环境的给定格式样式的日期格式化程序。
| 参数(Parameters) | |
|---|---|
style |
int: the given formatting style. For example, SHORT for "M/d/yy" in the US locale. |
| 返回(Returns) | |
|---|---|
DateFormat |
a date formatter. |
DateFormat getDateInstance ()
获取默认语言环境的默认格式设置的日期格式化程序。
| 返回(Returns) | |
|---|---|
DateFormat |
a date formatter. |
DateFormat getDateTimeInstance (int dateStyle, int timeStyle)
使用默认语言环境的给定日期和时间格式化样式获取日期/时间格式化程序。
| 参数(Parameters) | |
|---|---|
dateStyle |
int: the given date formatting style. For example, SHORT for "M/d/yy" in the US locale. |
timeStyle |
int: the given time formatting style. For example, SHORT for "h:mm a" in the US locale. |
| 返回(Returns) | |
|---|---|
DateFormat |
a date/time formatter. |
DateFormat getDateTimeInstance (int dateStyle, int timeStyle, Locale aLocale)
使用给定语言环境的给定格式样式获取日期/时间格式器。
| 参数(Parameters) | |
|---|---|
dateStyle |
int: the given date formatting style. |
timeStyle |
int: the given time formatting style. |
aLocale |
Locale: the given locale. |
| 返回(Returns) | |
|---|---|
DateFormat |
a date/time formatter. |
DateFormat getDateTimeInstance ()
获取默认语言环境的默认格式样式的日期/时间格式化程序。
| 返回(Returns) | |
|---|---|
DateFormat |
a date/time formatter. |
DateFormat getInstance ()
获取日期和时间都使用SHORT样式的默认日期/时间格式器。
| 返回(Returns) | |
|---|---|
DateFormat |
|
NumberFormat getNumberFormat ()
获取此日期/时间格式化程序用于格式化和解析时间的数字格式化程序。
| 返回(Returns) | |
|---|---|
NumberFormat |
the number formatter which this date/time formatter uses. |
DateFormat getTimeInstance (int style)
使用默认语言环境的给定格式样式获取时间格式器。
| 参数(Parameters) | |
|---|---|
style |
int: the given formatting style. For example, SHORT for "h:mm a" in the US locale. |
| 返回(Returns) | |
|---|---|
DateFormat |
a time formatter. |
DateFormat getTimeInstance (int style, Locale aLocale)
使用给定语言环境的给定格式样式获取时间格式器。
| 参数(Parameters) | |
|---|---|
style |
int: the given formatting style. For example, SHORT for "h:mm a" in the US locale. |
aLocale |
Locale: the given locale. |
| 返回(Returns) | |
|---|---|
DateFormat |
a time formatter. |
DateFormat getTimeInstance ()
获取默认语言环境的默认格式样式的时间格式化程序。
| 返回(Returns) | |
|---|---|
DateFormat |
a time formatter. |
TimeZone getTimeZone ()
获取时区。 此方法等同于以下调用。
getCalendar().getTimeZone()
| 返回(Returns) | |
|---|---|
TimeZone |
the time zone associated with the calendar of DateFormat. |
int hashCode ()
重写hashCode
| 返回(Returns) | |
|---|---|
int |
a hash code value for this object. |
boolean isLenient ()
告诉日期/时间解析是否宽松。 此方法等同于以下调用。
getCalendar().isLenient()
| 返回(Returns) | |
|---|---|
boolean |
true if the calendar is lenient; false otherwise. |
也可以看看:
Date parse (String source)
从给定字符串的开头解析文本以生成日期。 该方法可能不会使用给定字符串的整个文本。
有关日期解析的更多信息,请参阅 parse(String, ParsePosition)方法。
| 参数(Parameters) | |
|---|---|
source |
String: A String whose beginning should be parsed. |
| 返回(Returns) | |
|---|---|
Date |
A Date parsed from the string. |
| 抛出异常(Throws) | |
|---|---|
ParseException |
if the beginning of the specified string cannot be parsed. |
Date parse (String source, ParsePosition pos)
根据给定的解析位置解析日期/时间字符串。 例如,一个时间文字"07/10/96 4:5 PM, PDT"将被解析成Date ,它等效于Date(837039900000L) 。
默认情况下,解析是宽松的:如果输入不在此对象格式方法使用的表单中,但仍然可以解析为日期,则解析成功。 客户可以通过拨打setLenient(false)坚持严格遵守格式。
此解析操作使用calendar生成Date 。 因此,根据子类实现, calendar的日期时间字段和TimeZone值可能已被覆盖。 可能需要恢复以前通过调用setTimeZone设置的任何TimeZone值才能进一步操作。
| 参数(Parameters) | |
|---|---|
source |
String: The date/time string to be parsed |
pos |
ParsePosition: On input, the position at which to start parsing; on output, the position at which parsing terminated, or the start position if the parse failed. |
| 返回(Returns) | |
|---|---|
Date |
A Date, or null if the input could not be parsed |
Object parseObject (String source, ParsePosition pos)
解析字符串中的文本以生成 Date 。
该方法尝试解析从pos给出的索引开始的文本。 如果解析成功,则在使用最后一个字符(解析不一定使用到字符串末尾的所有字符)之后,将索引pos更新为索引,并返回解析的日期。 更新的pos可用于指示下一次调用此方法的起点。 如果发生错误,则pos的索引不会更改,将错误索引pos设置为发生错误的字符的索引,并返回null。
有关日期解析的更多信息,请参阅 parse(String, ParsePosition)方法。
| 参数(Parameters) | |
|---|---|
source |
String: A String, part of which should be parsed. |
pos |
ParsePosition: A ParsePosition object with index and error index information as described above. |
| 返回(Returns) | |
|---|---|
Object |
A Date parsed from the string. In case of error, returns null. |
| 抛出异常(Throws) | |
|---|---|
NullPointerException |
if pos is null. |
void setCalendar (Calendar newCalendar)
设置该日期格式使用的日历。 最初,使用指定或默认区域设置的默认日历。
以前设置的任何 TimeZone和 leniency值将被覆盖 newCalendar的值。
| 参数(Parameters) | |
|---|---|
newCalendar |
Calendar: the new Calendar to be used by the date format |
void setLenient (boolean lenient)
指定日期/时间解析是否是宽松的。 通过宽松的解析,解析器可以使用启发式来解释不精确匹配此对象格式的输入。 通过严格的解析,输入必须匹配这个对象的格式。
此方法等同于以下调用。
getCalendar().setLenient(lenient)
这个宽大的价值被 setCalendar()的电话覆盖。
| 参数(Parameters) | |
|---|---|
lenient |
boolean: when true, parsing is lenient |
也可以看看:
void setNumberFormat (NumberFormat newNumberFormat)
允许您设置数字格式器。
| 参数(Parameters) | |
|---|---|
newNumberFormat |
NumberFormat: the given new NumberFormat. |
void setTimeZone (TimeZone zone)
设置此DateFormat对象的日历的时区。 此方法等同于以下调用。
getCalendar().setTimeZone(zone)
由此方法设置的 TimeZone被 setCalendar调用覆盖。
由此方法设置的 TimeZone可能会因调用解析方法而被覆盖。
| 参数(Parameters) | |
|---|---|
zone |
TimeZone: the given new time zone. |