public class DateFormat
extends Object
| java.lang.Object | |
| android.text.format.DateFormat | |
用于生成格式化日期/时间字符串的实用工具类。
大部分来电者应避免提供自己的格式字符串这一类format方法和依赖于系统提供的正确本地化的。 这个类的工厂方法返回适当的本地化的DateFormat实例,适用于格式化和解析日期。 有关格式字符串的规范文档,请参阅SimpleDateFormat 。
如果系统没有提供合适的模式,本课程提供 getBestDateTimePattern(Locale, String)方法。
format中的format方法实现Unicode UTS #35模式的子集。 该类当前支持的子集包括以下格式字符: acdEHhLKkLMmsyz 。 截至API级别17,仅支持adEhkMmszy 。 请注意,这个类错误地实现了k ,就好像它是H的向后兼容性一样。
有关SimpleDateFormat的更多文档,或者需要更完整或正确的实现,请参阅SimpleDateFormat 。 请注意,该类中的非format方法由SimpleDateFormat实现。
Public constructors |
|
|---|---|
DateFormat() |
|
公共方法(Public methods) |
|
|---|---|
static CharSequence |
format(CharSequence inFormat, Date inDate) 给定一个格式字符串和一个 |
static CharSequence |
format(CharSequence inFormat, Calendar inDate) 给定格式字符串和 |
static CharSequence |
format(CharSequence inFormat, long inTimeInMillis) 给定格式字符串和自1970年1月1日GMT以来的时间(以毫秒为单位),返回包含请求日期的CharSequence。 |
static String |
getBestDateTimePattern(Locale locale, String skeleton) 返回给定语言环境给定框架的最佳可能本地化形式。 |
static DateFormat |
getDateFormat(Context context) 返回一个 |
static char[] |
getDateFormatOrder(Context context) 获取存储为char数组的当前日期格式。 |
static DateFormat |
getLongDateFormat(Context context) 返回一个 |
static DateFormat |
getMediumDateFormat(Context context) 返回 |
static DateFormat |
getTimeFormat(Context context) 返回 |
static boolean |
is24HourFormat(Context context) 如果用户首选项设置为24小时格式,则返回true。 |
继承方法(Inherited methods) |
|
|---|---|
java.lang.Object
|
|
CharSequence format (CharSequence inFormat, Date inDate)
给定格式字符串和 Date对象,返回包含请求日期的CharSequence。
| 参数(Parameters) | |
|---|---|
inFormat |
CharSequence: the format string, as described in DateFormat |
inDate |
Date: the date to format |
| 返回(Returns) | |
|---|---|
CharSequence |
a CharSequence containing the requested text |
CharSequence format (CharSequence inFormat, Calendar inDate)
给定一个格式字符串和一个 Calendar对象,返回一个包含请求日期的CharSequence。
| 参数(Parameters) | |
|---|---|
inFormat |
CharSequence: the format string, as described in DateFormat |
inDate |
Calendar: the date to format |
| 返回(Returns) | |
|---|---|
CharSequence |
a CharSequence containing the requested text |
CharSequence format (CharSequence inFormat, long inTimeInMillis)
给定格式字符串和自1970年1月1日GMT以来的时间(以毫秒为单位),返回包含请求日期的CharSequence。
| 参数(Parameters) | |
|---|---|
inFormat |
CharSequence: the format string, as described in DateFormat |
inTimeInMillis |
long: in milliseconds since Jan 1, 1970 GMT |
| 返回(Returns) | |
|---|---|
CharSequence |
a CharSequence containing the requested text |
String getBestDateTimePattern (Locale locale, String skeleton)
返回给定语言环境给定框架的最佳可能本地化形式。 骨架与Unicode UTS #35模式类似,并使用相同的格式字符。
一个不同之处在于订单无关紧要。 例如,“MMMMd”将在返回“MMMM d” en_US语言环境中,但“d。MMMM” de_CH区域。
还要注意在第二个例子中添加了德语的必要标点符号。 对于es_ES的相同输入,我们会有更多的额外文本:“d'de'MMMM”。
该方法将自动修正语法上的必要性。 给定相同的“MMMMd”输入,此方法将返回fa_IR语言环境中的“d LLLL”,其中需要独立的月份。 在有意义的地方保存长度,所以“Md”会给“MMMd”一个不同的结果,例如,除了在ja_JP这样一个只有一个月的长度的地方。
此方法只会返回CLDR中的模式,并且在您知道要在格式字符串中使用哪些元素但不想让您的代码专用于任何一种语言环境时很有用。
| 参数(Parameters) | |
|---|---|
locale |
Locale: the locale into which the skeleton should be localized |
skeleton |
String: a skeleton as described above |
| 返回(Returns) | |
|---|---|
String |
a string pattern suitable for use with SimpleDateFormat. |
DateFormat getDateFormat (Context context)
返回一个 DateFormat对象,该对象可以根据当前语言环境以简短形式格式化日期。
| 参数(Parameters) | |
|---|---|
context |
Context: the application context |
| 返回(Returns) | |
|---|---|
DateFormat |
the DateFormat object that properly formats the date. |
char[] getDateFormatOrder (Context context)
获取存储为char数组的当前日期格式。 按照用户的格式首选项指定的顺序返回包含日期( 'd' ),月份( 'M' )和年份( 'y' )的3元素数组。 请注意,此订单仅适用于全数字日期; 拼写出(中等和长)日期通常会包含其他标点符号,空格或单词,而不仅仅是日,月和年,并且不一定按照这里返回的相同顺序。
| 参数(Parameters) | |
|---|---|
context |
Context
|
| 返回(Returns) | |
|---|---|
char[] |
|
DateFormat getLongDateFormat (Context context)
返回一个 DateFormat对象,该对象可以为当前语言环境格式化长格式的日期(如 Monday, January 3, 2000 )。
| 参数(Parameters) | |
|---|---|
context |
Context: the application context |
| 返回(Returns) | |
|---|---|
DateFormat |
the DateFormat object that formats the date in long form. |
DateFormat getMediumDateFormat (Context context)
返回一个 DateFormat对象,该对象可以格式化当前语言环境的中等格式的日期(如 Jan 3, 2000 )。
| 参数(Parameters) | |
|---|---|
context |
Context: the application context |
| 返回(Returns) | |
|---|---|
DateFormat |
the DateFormat object that formats the date in long form. |
DateFormat getTimeFormat (Context context)
返回 DateFormat对象,该对象可以根据当前区域设置和用户的 DateFormat小时时钟首选项来格式化时间。
| 参数(Parameters) | |
|---|---|
context |
Context: the application context |
| 返回(Returns) | |
|---|---|
DateFormat |
the DateFormat object that properly formats the time. |
boolean is24HourFormat (Context context)
如果用户首选项设置为24小时格式,则返回true。
| 参数(Parameters) | |
|---|---|
context |
Context: the context to use for the content resolver |
| 返回(Returns) | |
|---|---|
boolean |
true if 24 hour time format is selected, false otherwise. |