所有类


java.text
类 DateFormat

java.lang.Object
  继承者 java.text.Format
      继承者 java.text.DateFormat
所有已实现的接口:
Serializable, Cloneable
直接已知子类:
SimpleDateFormat

public abstract class DateFormat
   
   
   
   
   
extends Format

DateFormat 是日期/时间格式化子类的抽象类,它以与语言无关的方式格式化并分析日期或时间。日期/时间格式化子类(如 SimpleDateFormat)允许进行格式化(也就是日期 -> 文本)、分析(文本-> 日期)和标准化。将日期表示为 Date 对象,或者表示为从 GMT(格林尼治标准时间)1970 年,1 月 1 日 00:00:00 这一刻开始的毫秒数。

DateFormat 提供了很多类方法,以获得基于默认或给定语言环境和多种格式化风格的默认日期/时间 Formatter。格式化风格包括 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 完全为数字,如 12.13.52 或 3:30pm
  • MEDIUM 较长,如 Jan 12, 1952
  • LONG 更长,如 January 12, 1952 或 3:30:32pm
  • FULL 是完全指定,如 Tuesday, April 12, 1952 AD 或 3:30:42pm PST。

如果愿意,还可以在格式上设置时区。如果想对格式化或分析施加更多的控制(或者给予用户更多的控制),可以尝试将从工厂方法所获得的 DateFormat 强制转换为 SimpleDateFormat。这适用于大多数国家/地区;只是要记住将其放入一个 try 代码块中,以防遇到特殊的格式。

还可以使用借助 ParsePosition 和 FieldPosition 的分析和格式化方法形式来

  • 逐步地分析字符串的各部分。
  • 对齐任意特定的字段,或者找出字符串在屏幕上的选择位置。

同步

DateFormat 不是同步的。建议为每个线程创建独立的格式实例。如果多个线程同时访问一个格式,则它必须保持外部同步。

另请参见:
Format, NumberFormat, SimpleDateFormat, Calendar, GregorianCalendar, TimeZone, 序列化表格

嵌套类摘要
static classDateFormat.Field
          定义了各种常量,这些常量用作 DateFormat.formatToCharacterIterator 所返回的 AttributedCharacterIterator 中的属性键和 FieldPosition 中的字段标识符。
 
字段摘要
static intAM_PM_FIELD
          用于对齐 AM_PM 字段的有用常量。
protected  Calendarcalendar
          DateFormat 使用 calendar 来生成实现日期和时间格式化所需的时间字段值。
static intDATE_FIELD
          用于对齐 DATE 字段的有用常量。
static intDAY_OF_WEEK_FIELD
          用于对齐 DAY_OF_WEEK 字段的有用常量。
static intDAY_OF_WEEK_IN_MONTH_FIELD
          用于对齐 DAY_OF_WEEK_IN_MONTH 字段的有用常量。
static intDAY_OF_YEAR_FIELD
          用于对齐 DAY_OF_YEAR 字段的有用常量。
static intDEFAULT
          用于默认模式的常量。
static intERA_FIELD
          用于对齐 ERA 字段的有用常量。
static intFULL
          用于 FULL 模式的常量。
static intHOUR_OF_DAY0_FIELD
          用于对齐从 0 开始的 HOUR_OF_DAY 字段的有用常量。
static intHOUR_OF_DAY1_FIELD
          用于对齐从 1 开始的 HOUR_OF_DAY 字段的有用常量。
static intHOUR0_FIELD
          用于对齐从 0 开始的 HOUR 字段的有用常量。
static intHOUR1_FIELD
          用于对齐从 1 开始的 HOUR 字段的有用常量。
static intLONG
          用于 LONG 模式的常量。
static intMEDIUM
          用于 MEDIUM 模式的常量。
static intMILLISECOND_FIELD
          用于对齐 MILLISECOND 字段的有用常量。
static intMINUTE_FIELD
          用于对齐 MINUTE 字段的有用常量。
static intMONTH_FIELD
          用于对齐 MONTH 字段的有用常量。
protected  NumberFormatnumberFormat
          数字 formatter,DateFormat 用其来格式化日期和时间中的数字。
static intSECOND_FIELD
          用于对齐 SECOND 字段的有用常量。
static intSHORT
          用于 SHORT 模式的常量。
static intTIMEZONE_FIELD
          用于对齐 TIMEZONE 字段的有用常量。
static intWEEK_OF_MONTH_FIELD
          用于对齐 WEEK_OF_MONTH 字段的有用常量。
static intWEEK_OF_YEAR_FIELD
          用于对齐 WEEK_OF_YEAR 字段的有用常量。
static intYEAR_FIELD
          用于对齐 YEAR 字段的有用常量。
 
构造方法摘要
protected DateFormat()
          创建一个新的 DateFormat。
 
方法摘要
 Objectclone()
          重写 Cloneable
 booleanequals(Object obj)
          重写 equals
 Stringformat(Date date)
          将一个 Date 格式化为日期/时间字符串。
abstract  StringBufferformat(Date date, StringBuffer toAppendTo, FieldPosition fieldPosition)
          将一个 Date 格式化为日期/时间字符串。
 StringBufferformat(Object obj, StringBuffer toAppendTo, FieldPosition fieldPosition)
          重写 Format。
static Locale[]getAvailableLocales()
          返回所有语言环境的数组,此类的 get*Instance 方法可以为其返回已本地化的实例。
 CalendargetCalendar()
          获得与此日期/时间 formatter 关联的日历。
static DateFormatgetDateInstance()
          获得日期 formatter,该 formatter 具有默认语言环境的默认格式化风格。
static DateFormatgetDateInstance(int style)
          获得日期 formatter,该 formatter 具有默认语言环境的给定格式化风格。
static DateFormatgetDateInstance(int style, Locale aLocale)
          获得日期 formatter,该 formatter 具有给定语言环境的给定格式化风格。
static DateFormatgetDateTimeInstance()
          获得日期/时间 formatter,该 formatter 具有默认语言环境的默认格式化风格。
static DateFormatgetDateTimeInstance(int dateStyle, int timeStyle)
          获得日期/时间 formatter,该 formatter 具有默认语言环境的给定日期和时间格式化风格。
static DateFormatgetDateTimeInstance(int dateStyle, int timeStyle, Locale aLocale)
          获得日期/时间 formatter,该 formatter 具有给定语言环境的给定格式化风格。
static DateFormatgetInstance()
          获得为日期和时间使用 SHORT 风格的默认日期/时间 formatter。
 NumberFormatgetNumberFormat()
          获得此日期/时间 formatter 用于格式化和分析时间的数字 formatter。
static DateFormatgetTimeInstance()
          获得时间 formatter,该 formatter 具有默认语言环境的默认格式化风格。
static DateFormatgetTimeInstance(int style)
          获得时间 formatter,该 formatter 具有默认语言环境的给定格式化风格。
static DateFormatgetTimeInstance(int style, Locale aLocale)
          获得时间 formatter,该 formatter 具有给定语言环境的给定格式化风格。
 TimeZonegetTimeZone()
          获得时区。
 inthashCode()
          重写 hashCode
 booleanisLenient()
          告知日期/时间分析是否为不严格的。
 Dateparse(String source)
          从给定字符串的开始分析文本,以生成一个日期。
abstract  Dateparse(String source, ParsePosition pos)
          根据给定的分析位置开始分析日期/时间字符串。
 ObjectparseObject(String source, ParsePosition pos)
          分析字符串中的文本,以生成一个 Date
 voidsetCalendar(Calendar newCalendar)
          设置此日期格式所使用的日历。
 voidsetLenient(boolean lenient)
          指定日期/时间分析是否不严格。
 voidsetNumberFormat(NumberFormat newNumberFormat)
          允许用户设置数字 formatter。
 voidsetTimeZone(TimeZone zone)
          为此 DateFormat 对象的日历设置时区。
 
从类 java.text.Format 继承的方法
format, formatToCharacterIterator, parseObject
 
从类 java.lang.Object 继承的方法
finalize, getClass, notify, notifyAll, toString, wait, wait, wait
 

字段详细信息

calendar

protected Calendar calendar
DateFormat 使用 calendar 来生成实现日期和时间格式化所需的时间字段值。子类应将此 calendar 初始化为适合于与此 DateFormat 关联的语言环境的日历。


numberFormat

protected NumberFormat numberFormat
数字 formatter,DateFormat 用其来格式化日期和时间中的数字。子类应将此 numberFormat 初始化为适合于与此 DateFormat 关联的语言环境的数字格式。


ERA_FIELD

public static final int ERA_FIELD
用于对齐 ERA 字段的有用常量。用于日期/时间格式化的 FieldPosition 中。

另请参见:
常量字段值

YEAR_FIELD

public static final int YEAR_FIELD
用于对齐 YEAR 字段的有用常量。用于日期/时间格式化的 FieldPosition 中。

另请参见:
常量字段值

MONTH_FIELD

public static final int MONTH_FIELD
用于对齐 MONTH 字段的有用常量。用于日期/时间格式化的 FieldPosition 中。

另请参见:
常量字段值

DATE_FIELD

public static final int DATE_FIELD
用于对齐 DATE 字段的有用常量。用于日期/时间格式化的 FieldPosition 中。

另请参见:
常量字段值

HOUR_OF_DAY1_FIELD

public static final int HOUR_OF_DAY1_FIELD
用于对齐从 1 开始的 HOUR_OF_DAY 字段的有用常量。用于日期/时间格式化的 FieldPosition 中。HOUR_OF_DAY1_FIELD 用于从 1 开始的 24 小时时钟。例如,23:59 + 01:00 的结果是 24:59。

另请参见:
常量字段值