【Java1.8新特性--->更新换代日期操作】LocalDateTime和ZonedDateTime日期时间操作

一、简介

JDK1.0中使用java.util.Date类 —> 第一批日期时间API
JDK1.1引入Calendar类 —> 第二批日期时间API

缺陷:

  • 可变性:像日期和时间这样的类应该是不可变的。
  • 偏移性:Date中的年份是从1900开始的,而月份都从0开始。
  • 格式化:格式化只对Date有用,Calendar则不行。

JDK1.8新增日期时间API —> 第三批日期时间API

1、LocalDateTime简介

LocalDateTime是一个不可变的日期-时间对象,表示日期-时间,通常被视为年月日时分秒或其他日期和时间字段。

例如:一年中的某一天、一周中的某几天和一年中某几周,也可以访问。

时间以纳秒的精度表示。

2、ZonedDateTime简介

ZonedDateTime是带有时区的日期时间的不可变表示,以纳秒的精度存储所有日期和时间字段以及时区,其中区域偏移用于处理不明确的本地日期时间。

例如:2007年10月2日13:45.30.123456789+02:00(欧洲/巴黎时区),可以存储在ZonedDateTime中。

ZonedDateTime处理从 LocalDateTime的本地时间行的转换到Instant的即时时间行,两条时间线之间的差异是:UTC/Greenwich的偏移,由ZoneOffset表示。在两条时间线之间转换包括使用从ZoneId访问的ZoneRules规则。

二、常用方法

1、LocalDateTime

1.1、now()

从默认时区的系统时钟获取当前日期时间。(包括日期和时间)

在这里插入图片描述

1.2、now(ZoneId zone)

从指定时区的系统时钟获取当前日期时间。(包括日期和时间)

在这里插入图片描述

1.3、of方法

获取指定年份、月份、月中天数、小时、分钟、秒和纳秒的LocalDateTime实例。

日期必须对年份和月份有效,否则将引发DateTimeException异常。

在这里插入图片描述

1.4、get方法

获取日期有关的值。

在这里插入图片描述

1.5、with方法

设置指定的年份、月份、月中天数、小时、分钟、秒和纳秒

在这里插入图片描述

1.6、plus方法

加上指定的年份、月份、月中天数、小时、分钟、秒和纳秒

在这里插入图片描述

1.7、minus方法

减去指定的年份、月份、月中天数、小时、分钟、秒和纳秒

在这里插入图片描述

1.8、format方法

使用指定的格式,格式化此日期时间。

在这里插入图片描述

1.9、parse方法

使用指定的格式,将String类型的日期时间字符串解析为LocalDateTime

在这里插入图片描述

1.10、isBeforeisAfterisEqual方法

判断此日期时间是否在指定日期时间之前、之后或者相等。

在这里插入图片描述

2、ZonedDateTime

ZonedDateTimeLocalDateTime相似,多了个指定时区的功能。

3、ZonedDateTime配合DateTimeFormatter使用

@Test
public void test02() {
    String dateTimeStr = "2023-05-09 08:00:00";
    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss").withZone(ZoneId.systemDefault());
    ZonedDateTime parse = ZonedDateTime.parse(dateTimeStr, formatter);
    System.out.println("parse = " + parse);

    DateTimeFormatter formatter1 = DateTimeFormatter.ofPattern("HH:mm:ss").withZone(ZoneId.systemDefault());
    String format = formatter1.format(parse);
    System.out.println("format = " + format);
}

在这里插入图片描述

4、DateTimeFormatter

public static void main(String[] args) {
        
    // 方式 1:预定义标准 如:ISO_LOCAL_DATE_TIME;ISO_LOCAL_DATE;ISO_LOCAL_TIME   
    DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ISO_LOCAL_DATE_TIME;
    // LocalDatetime --> String
    LocalDateTime now = LocalDateTime.now();
    String formatStr = dateTimeFormatter.format(now);
    System.out.println("formatStr = " + formatStr);
    
    // String --> LocalDateTime
    TemporalAccessor parse = dateTimeFormatter.parse("2022-12-25T15:44:25.481");
    System.out.println("parse = " + parse);
    
    System.out.println("==========================================================");
    
    // 方式 2:本地化相关格式,如 ofLocalizedDateTime
    // FormatStyle.LONG:2022年12月25日 下午04时26分55秒
    // FormatStyle.MEDIUM:2022-12-25 16:27:38
    // FormatStyle.SHORT:22-12-25 下午4:28
    DateTimeFormatter formatter = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.MEDIUM);
    // LocalDateTime --> String
    LocalDateTime now1 = LocalDateTime.now();
    String format = formatter.format(now1);
    System.out.println("format = " + format);
    
    // String --> LocalDateTime
    TemporalAccessor parse1 = formatter.parse("2022-12-25 16:27:38");
    System.out.println("parse1.get(ChronoField.DAY_OF_YEAR) = " + parse1.get(ChronoField.DAY_OF_YEAR));
    System.out.println("parse1 = " + parse1);

    System.out.println("==========================================================");

    
    // 方式3:自定义的格式。如: ofPattern( "yyyy-MM-dd hh:mm:ss") ---》重点,以后常用
    DateTimeFormatter df3 = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
    //LocalDateTime-->String:
    LocalDateTime now2 = LocalDateTime.now();
    String formatDate = df3.format(now2);
    System.out.println("formatDate = " + formatDate);//2020-06-15 03:22:03
    //String--->LocalDateTime
    // 方式1:
    LocalDateTime dateTime = LocalDateTime.parse("2020-06-15 03:22:03", df3);
    System.out.println("dateTime = " + dateTime);
    // 方式2:
    TemporalAccessor parse2 = df3.parse("2020-06-15 03:22:03");
    LocalDateTime from = LocalDateTime.from(parse2);
    System.out.println("from = " + from);
    
}

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值