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

你可能感兴趣的:(Date)