Java 8 新特性—日期时间格式化

作者简介:大家好,我是smart哥,前中兴通讯、美团架构师,现某互联网公司CTO

联系qq:184480602,加我进群,大家一起学习,一起进步,一起对抗互联网寒冬

学习必须往深处挖,挖的越深,基础越扎实!

阶段1、深入多线程

阶段2、深入多线程设计模式

阶段3、深入juc源码解析


阶段4、深入jdk其余源码解析


阶段5、深入jvm源码解析

码哥源码部分

码哥讲源码-原理源码篇【2024年最新大厂关于线程池使用的场景题】

码哥讲源码【炸雷啦!炸雷啦!黄光头他终于跑路啦!】

码哥讲源码-【jvm课程前置知识及c/c++调试环境搭建】

​​​​​​码哥讲源码-原理源码篇【揭秘join方法的唤醒本质上决定于jvm的底层析构函数】

码哥源码-原理源码篇【Doug Lea为什么要将成员变量赋值给局部变量后再操作?】

码哥讲源码【你水不是你的错,但是你胡说八道就是你不对了!】

码哥讲源码【谁再说Spring不支持多线程事务,你给我抽他!】

终结B站没人能讲清楚红黑树的历史,不服等你来踢馆!

打脸系列【020-3小时讲解MESI协议和volatile之间的关系,那些将x86下的验证结果当作最终结果的水货们请闭嘴】

通过前一篇文章(日期时间 API)我们知道如何在 Java 8 中得到我们需要的日期和时间,但是有时候我们需要将日期和时间对象转换为字符串又或者将字符串解析为日期时间对象,这个时候我们需要用到 Java 8 提供的日期时间格式化工具:DateTimeFormatter。

DateTimeFormatter

DateTimeFormatter 用于格式化和解析日期和时间,它能够轻松地将日期时间对象转换为字符串以及将字符串解析为日期时间对象。而且它是不可变的,线程安全的。

创建 DateTimeFormatter

DateTimeFormatter 提供了 ofPattern() 静态方法用于构建 DateTimeFormatter 对象:

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

其他静态方法如下:

  • ofPattern(String pattern, Locale locale):使用给定的模式和区域设置创建格式化器。
  • ofLocalizedDate(FormatStyle dateStyle):创建具有当地特定日期格式的格式化器。FormatStyle是一个枚举,其值可以是FULL, LONG, MEDIUM, SHORT。
  • ofLocalizedDateTime(FormatStyle dateTimeStyle):创建具有特定地区日期时间(date-time)格式的格式化器。
  • ofLocalizedDateTime(FormatStyle dateStyle, FormatStyle timeStyle): 创建具有特定地区日期时间(date-time)格式的格式化器。我们需要为日期和时间分别传递FormatStyle。例如,日期可以是LONG,时间可以是SHORT。
  • ofLocalizedTime(FormatStyle timeStyle): 创建具有当地特定时间格式的格式化器。

我们也可以使用预定义的格式化器,DateTimeFormatter 提供了大量的预定义格式化器,如下:

FormatterExample
BASIC_ISO_DATE‘20181203’
ISO_LOCAL_DATE‘2018-12-03’
ISO_OFFSET_DATE‘2018-12-03+01:00’
ISO_DATE‘2018-12-03+01:00’; ‘2018-12-03’
ISO_LOCAL_TIME‘11:15:30’
ISO_OFFSET_TIME‘11:15:30+01:00’
ISO_TIME‘11:15:30+01:00’; ‘11:15:30’
ISO_LOCAL_DATE_TIME‘2018-12-03T11:15:30’
ISO_OFFSET_DATE_TIME‘2018-12-03T11:15:30+01:00’
ISO_ZONED_DATE_TIME‘2018-12-03T11:15:30+01:00[Europe/Paris]’
ISO_DATE_TIME‘2018-12-03T11:15:30+01:00[Europe/Paris]’
ISO_ORDINAL_DATE‘2018-337’
ISO_WEEK_DATE‘2018-W48-6’
ISO_INSTANT‘2018-12-03T11:15:30Z’
RFC_1123_DATE_TIME‘Tue, 3 Jun 2018 11:05:30 GMT’

例如,我们使用 ISO_DATE_TIME 格式化一个日期时间:

DateTimeFormatter formatter = DateTimeFormatter.ISO_DATE_TIME;
String dateTime = formatter.format(LocalDateTime.now()); //2023-10-10T15:36:03.626

如果我们需要的不再是简单的 yyyy-MM-dd 格式的日期,而是更加复杂的日期格式,我们可以使用 DateTimeFormatterBuilder 来构建更加复杂的日期格式。

格式转换

将日期时间对象转换为字符串

为了格式化一个日期、时间或日期时间,DateTimeFormatter提供了两个 format 方法:

  • format(TemporalAccessor temporal):将日期时间对象格式化为字符串,返回的是一个字符串对象。
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
System.out.println(dateTimeFormatter.format(LocalDateTime.now()));   //2023-10-09 22:12:55

  • formatTo(TemporalAccessor temporal, Appendable appendable):将日期时间对象格式化,但是它没有返回值,而是将结果附加到给定的Appendable对象中。Appendable对象可以是StringBuffer、StringBuilder等的实例,这样可以提供性能,因为它避免创建了不必要的字符串对象。
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
StringBuilder stringBuilder = new StringBuilder();
dateTimeFormatter.formatTo(LocalDateTime.now(),stringBuilder);
System.out.println(stringBuilder.toString());                      // 2023-10-09 22:23:43

也可以将日期时间格式的字符串转换为 LocalDateTimeZonedDateTime 等日期时间对象:

DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
System.out.println(LocalDateTime.parse("2023-10-09 22:12:55",dateTimeFormatter));  //2023-10-09T22:12:55

将字符串解析为日期时间对象

DateTimeFormatter 用于将字符串解析为日期时间对象的方法主要是 parse(),但是它返回的是 TemporalAccessor,我们需要再次将 TemporalAccessor 进一步转换:

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/MM/dd");
TemporalAccessor temporalAccessor = formatter.parse("2023-10-10");
LocalDate localDate = LocalDate.from(temporalAccessor);

有点儿繁琐,所以如果我们明确了要解析的类型,可以直接使用 来将字符串解析为指定的日期时间对象:

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/MM/dd");
LocalDate localDate = formatter.parse("2023-10-10", LocalDate::from);

或者直接使用具体的日期时间对象的 parse() 来转换:

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/MM/dd");
LocalDate localDate = LocalDate.parse("2023-10-10",formatter);

DateTimeFormatterBuilder

DateTimeFormatter 虽然能够帮忙我们格式化标准的日期时间,但是有时候我们需要更加复杂的日期时间格式,比如这种格式:Day is:17, month is:10, and year:2014 with the time:23:35 ,你使用 DateTimeFormatter 是会报错的:

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("Day is:dd, month is:MM, and year:yyyy with the time:HH:mm");
String dateTime = formatter.format(LocalDateTime.now());

报错如下:

碰到这种比较灵活,个性化的日期格式时,DateTimeFormatter 无法处理或者处理起来比较麻烦时,我们可以使用更加强大灵活的 DateTimeFormatterBuilder。

DateTimeFormatterBuilder 是 Java 中用于构建自定义日期时间的格式化神器。它允许我们按照自己的需求来构建日期和时间,包括自定义的日期和时间分隔符时区、日期元素的顺序等等。

DateTimeFormatter 中的预定义格式化器就是采用 DateTimeFormatterBuilder 来构建的,例如 ISO_DATE_TIME:

    public static final DateTimeFormatter ISO_DATE_TIME;
    static {
        ISO_DATE_TIME = new DateTimeFormatterBuilder()
                .append(ISO_LOCAL_DATE_TIME)
                .optionalStart()
                .appendOffsetId()
                .optionalStart()
                .appendLiteral('[')
                .parseCaseSensitive()
                .appendZoneRegionId()
                .appendLiteral(']')
                .toFormatter(ResolverStyle.STRICT, IsoChronology.INSTANCE);
    }

当我们通过构造函数构建出一个 DateTimeFormatterBuilder 实例对象后,就可以通过它的实例方法来添加不同的元素构造成我们的格式化器。常见的方法有:

方法描述
appendPattern(String pattern)根据传入的模式字符串添加格式化元素。
appendLiteral(char literal)添加字面文字。
appendValue(TemporalField field, int width)添加字段值。
appendText(TemporalField field)添加字段的文本表示。
appendZoneId()添加时区标识。
appendOffset(String pattern, String noOffsetText)添加偏移量信息。

示例:

builder.appendPattern("yyyy-MM-dd HH:mm:ss");
builder.appendLiteral('T');
builder.appendValue(ChronoField.HOUR_OF_DAY, 2);
builder.appendLiteral(':');
builder.appendValue(ChronoField.MINUTE_OF_HOUR, 2);

当我们构建完毕后,使用 toFormatter() 方法就可以将其转换为 DateTimeFormatter。下面我们就用 DateTimeFormatterBuilder 来完成上面那个示例。

Day is: 这种是字面文字,所以使用 appendLiteral(char literal) 直接添加就可以了。年月日这样的实际值我们需要 appendValue(TemporalField field, int width) 来添加,该方法中的 width 表示字面的宽度,即显示字段的位数,比如 9 月份,如果 width 为 2 ,则显示为 09,如果不设置的话则显示为 9,代码如下:

    @Test
    public void test() {
        DateTimeFormatter formatter = new DateTimeFormatterBuilder().appendLiteral("Day is:")
                .appendValue(ChronoField.DAY_OF_MONTH,2)
                .appendLiteral(", month is:")
                .appendValue(ChronoField.MONTH_OF_YEAR,2)
                .appendLiteral(", and year:")
                .appendValue(ChronoField.YEAR,4)
                .appendLiteral(" with the time:")
                .appendValue(ChronoField.HOUR_OF_DAY)
                .appendLiteral(":")
                .appendValue(ChronoField.MINUTE_OF_HOUR)
                .toFormatter();
        LocalDateTime dateTime = LocalDateTime.of(2023,9,13,22,34,25,0);
        String str = dateTime.format(formatter);
        System.out.println(str);
    }
// 结果......
Day is:13, month is:09, and year:2023 with the time:22:34

是不是很完美地呈现出来了?

DateTimeFormatterBuilder 是一个非常灵活的格式化工具,我们可以利用它创建各种各样的自定义日期时间格式化器,以满足我们的特定需求。

  • 11
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值