java.util.Date转换LocalDateTime

Date date = new Date();
ZoneId zoneId = ZoneId.of(ZoneId.SHORT_IDS.get("PST"));
LocalDateTime localDateTime1 = LocalDateTime.ofInstant(date.toInstant(), zoneId);
System.out.println(localDateTime1);

// 年月日时分秒初始化LocalDateTime,并设置UTC时区 
LocalDateTime localDateTime2 = LocalDateTime.of(2019,01,21,15,35,40);
ZonedDateTime zonedDateTime = localDateTime2.atZone(ZoneId.of("UTC"));
// 时间不变,按照UTC格式输出时间
System.out.println(localDateTime2);
System.out.println(zonedDateTime);

// date的时间戳初始化LocalDateTime ,并设置时区
LocalDateTime localDateTime3 = LocalDateTime.ofInstant(date.toInstant(), ZoneId.of("UTC"));
localDateTime3.atZone(ZoneId.of("UTC"));
// 当前系统时间转换为UTC时间,然后按照UTC格式输出
System.out.println(localDateTime3);

输出:

2019-01-20T23:57:53.464
2019-01-21T15:35:40
2019-01-21T15:35:40Z[UTC]
2019-01-21T07:57:53.464

 

设置项目默认时区:

TimeZone.setDefault(TimeZone.getTimeZone("UTC"));

时区转换:

http://www.thetimezoneconverter.com/

Java 8中处理日期和时间示例:

https://www.jianshu.com/p/2949db9c3df5

Java 8新特性(四):新的时间和日期API

https://lw900925.github.io/java/java8-newtime-api.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值