深入学习 Java 8 全新日期时间库 java.time(五)

基本概念我们已经大概的了解了,接下来我们就深入探讨一下时间日期的格式化。类库中 java.time.format 包中就是我们所需的一些概念,其中最主要也是用的最多的就是DateTimeFormatter这个类。

DateTimeFormatter

DateTimeFormatter 是用于打印和解析日期时间对象的格式化程序。

通常日期时间对象都提供了如下两个方法:

  • parse(CharSequence text, DateTimeFormatter formatter):使用特定的格式化程序从文本字符串中获取日期时间的实例。
  • String format(DateTimeFormatter formatter):使用指定的格式化程序格式化此日期时间。

DateTimeFormatter 为我们提供了许多预定义的DateTimeFormatter:

  • BASIC_ISO_DATE: The ISO date formatter that formats or parses a date without an offset, such as ‘20111203’.
  • ISO_DATE: The ISO date formatter that formats or parses a date with the offset if available, such as ‘2011-12-03’ or ‘2011-12-03+01:00’.
  • ISO_DATE_TIME: The ISO-like date-time formatter that formats or parses a date-time with the offset and zone if available, such as ‘2011-12-03T10:15:30’, ‘2011-12-03T10:15:30+01:00’ or ‘2011-12-03T10:15:30+01:00[Europe/Paris]’.
  • ISO_INSTANT: The ISO instant formatter that formats or parses an instant in UTC, such as ‘2011-12-03T10:15:30Z’.
  • ISO_LOCAL_DATE: The ISO date formatter that formats or parses a date without an offset, such as ‘2011-12-03’.
  • ISO_LOCAL_DATE_TIME: The ISO date-time formatter that formats or parses a date-time without an offset, such as ‘2011-12-03T10:15:30’.
  • ISO_LOCAL_TIME: The ISO time formatter that formats or parses a time without an offset, such as ‘10:15’ or ‘10:15:30’.
  • ISO_OFFSET_DATE: The ISO date formatter that formats or parses a date with an offset, such as ‘2011-12-03+01:00’.
  • ISO_OFFSET_DATE_TIME: The ISO date-time formatter that formats or parses a date-time with an offset, such as ‘2011-12-03T10:15:30+01:00’.
  • ISO_OFFSET_TIME: The ISO time formatter that formats or parses a time with an offset, such as ‘10:15+01:00’ or ‘10:15:30+01:00’.
  • ISO_ORDINAL_DATE: The ISO date formatter that formats or parses the ordinal date without an offset, such as ‘2012-337’.
  • ISO_TIME: The ISO time formatter that formats or parses a time, with the offset if available, such as ‘10:15’, ‘10:15:30’ or ‘10:15:30+01:00’.
  • ISO_WEEK_DATE: The ISO date formatter that formats or parses the week-based date without an offset, such as ‘2012-W48-6’.
  • ISO_ZONED_DATE_TIME: The ISO-like date-time formatter that formats or parses a date-time with offset and zone, such as ‘2011-12-03T10:15:30+01:00[Europe/Paris]’.
  • RFC_1123_DATE_TIME: The RFC-1123 date-time formatter, such as ‘Tue, 3 Jun 2008 11:05:30 GMT’.

DateTimeFormatter 为我们提供了 of*with* 静态工厂方法来构造DateTimeFormatter,我们容易理解且最方便自定义的就是 ofPattern(String pattern) 方法了。

LocalDate.parse("1-12-03", DateTimeFormatter.ofPattern("u-M-d")); // 0001-12-03
LocalDate.parse("1-12-03", DateTimeFormatter.ofPattern("uu-M-d")); // 异常
LocalDate.parse("2011-2-03", DateTimeFormatter.ofPattern("u-M-d")); // 2011-02-03

从上面的代码中不难发现:u表示年,且u出现的数量也是解析的时候要匹配的最小数量,而年我们知道当前都是四位,因此我们正确使用应该是uuuu

LocalDate d = LocalDate.parse("2011-12-03", DateTimeFormatter.ofPattern("u-M-d")); // 2011-12-03
d.format(DateTimeFormatter.ofPattern("d MMMM uuuu")); // 3 十二月 2011
d.format(DateTimeFormatter.ofPattern("d MMM uuuu")); // 3 十二月 2011
d.format(DateTimeFormatter.ofPattern("d MM uuuu")); // 3 12 2011
d.format(DateTimeFormatter.ofPattern("d M uuuu")); // 3 12 2011
d.format(DateTimeFormatter.ofPattern("d M uu")); // 3 12 11
d.format(DateTimeFormatter.ofPattern("dd M uuuu")); // 03 12 2011

从上面代码中我们可以看出:字母出现的数量就是我们格式化时可以出现的最小数量,不足时使用0补齐。

DateTimeFormatter italianFormatter = DateTimeFormatter.ofPattern("d. MMMM yyyy", Locale.ITALIAN);//指定了Locale
LocalDate date = LocalDate.of(2014, 3, 18);
String formattedDate = date.format(italianFormatter); // 18. marzo 2014
LocalDate date2 = LocalDate.parse(formattedDate, italianFormatter);

类库还为我们提供了一个建造者模式DateTimeFormatterBuilder类来创建DateTimeFormatter,这个类与其它静态工厂方法一样涉及的知识点较为广泛,且不常用我们暂且不讨论了待日后空闲了再做补充!【?】

DateTimeFormatter italianFormatter = new DateTimeFormatterBuilder()
		.appendText(ChronoField.DAY_OF_MONTH)
		.appendLiteral(". ")
		.appendText(ChronoField.MONTH_OF_YEAR)
		.appendLiteral(" ")
		.appendText(ChronoField.YEAR)
		.parseCaseInsensitive()
		.toFormatter(Locale.CHINA);

赞赏

已标记关键词 清除标记
相关推荐
©️2020 CSDN 皮肤主题: 书香水墨 设计师:CSDN官方博客 返回首页