jdk8中新增的日期处理类LocalDate,LocalTime,LocalDateTime,ZoneId,ZonedDateTime详解

基本概念

时刻

  • 所有计算机系统内部都用一个整数表示时刻,这个整数是距离格林尼治标准时间1970年1月1日0时0分0秒的毫秒数,可以理解时刻就是绝对时间,它与时区无关;
  • 不同时区对同一时刻的解读,即年月日时分秒是不一样的;

时区
同一时刻,世界上各个地区的时间可能是不一样的,具体时间与时区有关,一共有24个时区,英国格林尼治是0时区,北京是东八区,也就是说格林尼治凌晨1点,北京是早上9点;

年历
我们都知道,中国有公历和农历之分,公历和农历都是年历,不同的年历,一年有多少月,每月有多少天,甚至一天有多少小时,这些可能都是不一样的,我们主要讨论公历。

LocalDate

获取当前日期

LocalDate today = LocalDate.now();

指定日期

LocalDate specifyDate = LocalDate.of(2014, 12, 25);

根据字符串获取

LocalDate endOfFeb = LocalDate.parse("2014-02-28");

取本月第1天

LocalDate firstDayOfThisMonth = today.with(TemporalAdjusters.firstDayOfMonth());

取本月第2天

LocalDate secondDayOfThisMonth = today.withDayOfMonth(2);

取本月最后一天,再也不用计算是28,29,30还是31

LocalDate lastDayOfThisMonth = today.with(TemporalAdjusters.lastDayOfMonth());

取下一天

LocalDate nextDay = today.plusDays(1);

取2017年1月第一个周一

LocalDate firstMondayOf2015 = LocalDate.parse(“2017-01-01”).with(TemporalAdjusters.firstInMonth(DayOfWeek.MONDAY));

取当年第一天

today.with(TemporalAdjusters.firstDayOfYear())

取当年最后一天

today.with(TemporalAdjusters.lastDayOfYear())

LocalTime

获取当前时间

LocalTime now = LocalTime.now();

指定时间

LocalTime mid = LocalTime.parse("12:00:00");

LocalDateTime

表示与时区无关的日期和时间信息,不直接对应时刻,需要通过时区转换

LocalDateTime转为指定格式的String

DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
LocalDateTime time = LocalDateTime.now();
String localTime = df.format(time);

指定格式String转为LocalDateTime

LocalDateTime ldt = LocalDateTime.parse("2017-09-28 17:07:05",df);

LocalDateTime时间差计算(计算天数)

DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
String pubTime="2021-03-27 16:49:15";
String pubEndTime="2022-03-27 16:48:15";

LocalDateTime startTime = LocalDateTime.parse(pubTime, df);
LocalDateTime endTime = LocalDateTime.parse(pubEndTime, df);
Duration duration = Duration.between(startTime,endTime);
long count = duration.toMillis();
System.out.println(count);
System.out.println("相差天数:" + count / (3600 * 24 *1000));

ZoneId

默认时区

ZoneId.systemDefault()

指定时区

ZoneId.of("America/New_York")

可用时区

ZoneId.getAvailableZoneIds()

ZonedDateTime

创建ZonedDateTime,表示一个带有时区的时间

ZonedDateTime zbj = ZonedDateTime.now(); // 默认时区
ZonedDateTime zny = ZonedDateTime.now(ZoneId.of("America/New_York")); // 用指定时区获取当前时间

LocalDateTime转为带有时区的ZonedDateTime

LocalDateTime ldt = LocalDateTime.of(2019, 9, 15, 15, 16, 17);
ZonedDateTime zbj = ldt.atZone(ZoneId.systemDefault());//默认时区
ZonedDateTime zny = ldt.atZone(ZoneId.of("America/New_York"));//纽约时区

时区转换

中国时区转纽约时间

ZonedDateTime zbj = ZonedDateTime.now(ZoneId.of("Asia/Shanghai"));
ZonedDateTime zny = zbj.withZoneSameInstant(ZoneId.of("America/New_York"));

ZonedDateTime转本地时间

ZonedDateTime zdt = ZonedDateTime.now();
LocalDateTime ldt = zdt.toLocalDateTime();//直接丢弃了时区信息

字符串转 ZonedDateTime

String zonedDateTime = "2022-07-02T16:00:00.000Z";
DateTimeFormatter sdf2 = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'").withZone(ZoneId.of("Asia/Shanghai"));
或
DateTimeFormatter sdf2 = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'").withZone(ZoneId.systemDefault());
ZonedDateTime dateTime = ZonedDateTime.parse(zonedDateTime,sdf2);

ZonedDateTime转字符串

ZonedDateTime zonedDateTime = ZonedDateTime.now();
DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss").withZone(ZoneId.systemDefault());
String localTime = df.format(zonedDateTime);
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

大佬腿好粗

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值