Java8 教程

Java8 DateTimeFormatter(格式化和解析)

DateTimeFormatter 是 Java8 中引入的一个类,它属于 java.time.format 包,专门用于日期和时间的格式化和解析。

在 Java8 之前,我们通常使用 SimpleDateFormat 来处理日期和时间的格式化和解析,但 SimpleDateFormat 并不是线程安全的。为了解决这个问题,Java8 引入了全新的日期时间 API,其中 DateTimeFormatter 类作为该 API 的一部分,提供了线程安全且灵活的日期时间格式化和解析功能。

DateTimeFormatter 类的主要特性如下:

  • 线程安全:在多线程环境中,可以安全地使用 DateTimeFormatter 类的实例,无需额外的同步措施。

  • 不可变性:DateTimeFormatter 的实例创建后是不可变的,这进一步保证了其线程安全性。

  • 支持多种格式:支持多种日期时间格式,包括预定义的格式(如 ISO_DATE、ISO_TIME 等)和自定义格式。

  • 扩展性:使用 DateTimeFormatterBuilder 类可以创建自定义的日期时间格式。

DateTimeFormatter 内置格式

DateTimeFormatter 提了如下内置格式:

  • BASIC_ISO_DATE:格式化或解析没有偏移量的日期的 ISO 日期格式化程序,例如“20111203”

  • ISO_DATE:ISO日期格式化程序格式化或解析具有偏移量的日期(如可用),例如“2011-12-03”或“2011-12-03 + 01:00” 

  • ISO_DATE_TIME:类似 ISO 的日期格式化程序,用于格式化或解析具有偏移量和区域(如果有的话)的日期时间,例如“2011-12-03T10:15:30”

  • ISO_INSTANTISO即时格式化程序,用于格式化或解析UTC中的即时消息,例如“2011-12-03T10:15:30Z”  

  • ISO_LOCAL_DATE:ISO日期格式化程序格式化或解析没有偏移量的日期,例如“2011-12-03”

  • ISO_LOCAL_DATE_TIME:ISO日期格式化程序格式化或解析没有偏移量的日期时间,例如“2011-12-03T10:15:30”

  • ISO_LOCAL_TIME:ISO时间格式化程序格式化或解析一个没有偏移量的时间,例如“10:15”或“10:15:30”

  • ISO_OFFSET_DATE:ISO日期格式化程序格式化或解析具有偏移量的日期,例如“2011-12-03 + 01:00”

  • ISO_OFFSET_DATE_TIME:ISO日期格式化程序格式化或解析具有偏移量的日期时间,例如“2011-12-03T10:15:30 + 01:00”

  • ISO_OFFSET_TIME:格式化或解析时间偏移的ISO时间格式化程序,如“10:15 + 01:00”或“10:15:30 + 01:00”

  • ISO_ORDINAL_DATE:ISO日期格式化程序格式化或解析没有偏移量的序数日期,例如“2012-337”

  • ISO_TIME:式化或解析时间的ISO时间格式化程序,如果可用的偏移量,例如“10:15”,“10:15:30”或“10:15:30 + 01:00”

  • ISO_WEEK_DATE:ISO日期格式化程序,用于格式化或解析不带偏移量的基于周的日期,例如“2012-W48-6”

  • ISO_ZONED_DATE_TIME:类似ISO的日期格式化程序,用于格式化或解析具有偏移和区域的日期时间,例如“2011-12-03T10:15:30 + 01:00 [Europe / Paris]”

  • RFC_1123_DATE_TIME:RFC-1123日期格式化程序,例如“星期二,2008年6月3日11:05:30 GMT”

示例:

package com.hxstrive.jdk8.date_time.dateTimeFormatter;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * DateTimeFormatter 类
 * @author hxstrive.com
 */
public class DateTimeFormatterDemo1 {

    public static void main(String[] args) {
        LocalDateTime localDateTime = LocalDateTime.now();
        System.out.println(localDateTime); //2024-06-30T22:42:29.141624700

        // 格式化后的数据
        System.out.println(localDateTime.format(DateTimeFormatter.ISO_TIME));
        //22:42:29.1416247
        System.out.println(localDateTime.format(DateTimeFormatter.ISO_DATE));
        //2024-06-30
        System.out.println(localDateTime.format(DateTimeFormatter.ISO_DATE_TIME));
        //2024-06-30T22:42:29.1416247
        System.out.println(localDateTime.format(DateTimeFormatter.ISO_LOCAL_DATE));
        //2024-06-30
        System.out.println(localDateTime.format(DateTimeFormatter.ISO_LOCAL_DATE_TIME));
        //2024-06-30T22:42:29.1416247
    }

}

DateTimeFormatter 示例

构建 DateTimeFormatter 对象

使用 ofPattern 静态方法,该方法接受一个字符串参数,用于定义日期时间格式。例如:

package com.hxstrive.jdk8.date_time.dateTimeFormatter;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * DateTimeFormatter 类
 * @author hxstrive.com
 */
public class DateTimeFormatterDemo2 {

    public static void main(String[] args) {
        LocalDateTime localDateTime = LocalDateTime.now();
        System.out.println(localDateTime); //2024-06-30T22:42:29.141624700

        // 格式化日期
        DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        System.out.println(localDateTime.format(dateFormatter)); //2024-06-30
    }

}

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

使用 parse 方法将字符串解析为日期时间对象。但需要注意的是,parse 方法返回的是一个解析结果的引用,需要进一步处理(如转换为具体的日期时间类型)才能使用。例如:

package com.hxstrive.jdk8.date_time.dateTimeFormatter;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAccessor;

/**
 * DateTimeFormatter 类
 * @author hxstrive.com
 */
public class DateTimeFormatterDemo3 {

    public static void main(String[] args) {
        DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        String dateTimeString = "2022-02-14";
        TemporalAccessor parsed = dateFormatter.parse(dateTimeString);
        LocalDate date = LocalDate.from(parsed);
        System.out.println("year=" + date.getYear()); //year=2022
        System.out.println("month=" + date.getMonthValue()); //month=2
        System.out.println("day=" + date.getDayOfMonth()); //day=14
    }

}

上面例子中的“yyyy-MM-dd”是一个日期格式模式字符串,常用的格式模式字母及其含义如下:

  • y:年

  • M:月

  • d:日

  • E:星期

  • H:以 24 小时为周期的小时(范围 0-23)

  • h:以 12 小时为周期的小时(范围 1-12)

  • m:分钟

  • s:秒

注意:与 SimpleDateFormat 相比,DateTimeFormatter 是线程安全的,因此可以在多线程环境中放心使用。合理使用 DateTimeFormatterBuilder 类创建自定义格式,以满足应用程序的特定需求。

说说我的看法
全部评论(
没有评论
关于
本网站属于个人的非赢利性网站,转载的文章遵循原作者的版权声明,如果原文没有版权声明,请来信告知:hxstrive@outlook.com
公众号