java1.8常用时间类格式化DateTimeFormatter

public final class DateTimeFormatter
extends Object

Formatter for printing and parsing date-time objects.

This class provides the main application entry point for printing and parsing and provides common implementations of DateTimeFormatter:

  • Using predefined constants, such as ISO_LOCAL_DATE
  • Using pattern letters, such as uuuu-MMM-dd
  • Using localized styles, such as long or medium

More complex formatters are provided by DateTimeFormatterBuilder.

The main date-time classes provide two methods - one for formatting, format(DateTimeFormatter formatter), and one for parsing, parse(CharSequence text, DateTimeFormatter formatter).

For example:

  LocalDate date = LocalDate.now();
  String text = date.format(formatter);
  LocalDate parsedDate = LocalDate.parse(text, formatter);
 

In addition to the format, formatters can be created with desired Locale, Chronology, ZoneId, and DecimalStyle.

The withLocale method returns a new formatter that overrides the locale. The locale affects some aspects of formatting and parsing. For example, the ofLocalizedDate provides a formatter that uses the locale specific date format.

The withChronology method returns a new formatter that overrides the chronology. If overridden, the date-time value is converted to the chronology before formatting. During parsing the date-time value is converted to the chronology before it is returned.

The withZone method returns a new formatter that overrides the zone. If overridden, the date-time value is converted to a ZonedDateTime with the requested ZoneId before formatting. During parsing the ZoneId is applied before the value is returned.

The withDecimalStyle method returns a new formatter that overrides the DecimalStyle. The DecimalStyle symbols are used for formatting and parsing.

Some applications may need to use the older java.text.Format class for formatting. The toFormat() method returns an implementation of java.text.Format.

Predefined Formatters

FormatterDescriptionExample
ofLocalizedDate(dateStyle)Formatter with date style from the locale'2011-12-03'
ofLocalizedTime(timeStyle)Formatter with time style from the locale'10:15:30'
ofLocalizedDateTime(dateTimeStyle)Formatter with a style for date and time from the locale'3 Jun 2008 11:05:30'
ofLocalizedDateTime(dateStyle,timeStyle)Formatter with date and time styles from the locale'3 Jun 2008 11:05'
BASIC_ISO_DATEBasic ISO date'20111203'
ISO_LOCAL_DATEISO Local Date'2011-12-03'
ISO_OFFSET_DATEISO Date with offset'2011-12-03+01:00'
ISO_DATEISO Date with or without offset'2011-12-03+01:00'; '2011-12-03'
ISO_LOCAL_TIMETime without offset'10:15:30'
ISO_OFFSET_TIMETime with offset'10:15:30+01:00'
ISO_TIMETime with or without offset'10:15:30+01:00'; '10:15:30'
ISO_LOCAL_DATE_TIMEISO Local Date and Time'2011-12-03T10:15:30'
ISO_OFFSET_DATE_TIMEDate Time with Offset2011-12-03T10:15:30+01:00'
ISO_ZONED_DATE_TIMEZoned Date Time'2011-12-03T10:15:30+01:00[Europe/Paris]'
ISO_DATE_TIMEDate and time with ZoneId'2011-12-03T10:15:30+01:00[Europe/Paris]'
ISO_ORDINAL_DATEYear and day of year'2012-337'
ISO_WEEK_DATEYear and Week2012-W48-6'
ISO_INSTANTDate and Time of an Instant'2011-12-03T10:15:30Z'
RFC_1123_DATE_TIMERFC 1123 / RFC 822'Tue, 3 Jun 2008 11:05:30 GMT'

Patterns for Formatting and Parsing

Patterns are based on a simple sequence of letters and symbols. A pattern is used to create a Formatter using the ofPattern(String) and ofPattern(String, Locale) methods. For example, "d MMM uuuu" will format 2011-12-03 as '3 Dec 2011'. A formatter created from a pattern can be used as many times as necessary, it is immutable and is thread-safe.

For example:

  LocalDate date = LocalDate.now();
  DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy MM dd");
  String text = date.format(formatter);
  LocalDate parsedDate = LocalDate.parse(text, formatter);
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值