JAVA Date


java.time.Instant 时间线上的一个时刻。

java.time.LocalDate 一个符合ISO-8601规范的、不包含时区信息的对象,包括yy-MM-dd信息

java.time.LocalDateTime 一个符合ISO-8601规范的、不包含时区信息的对象,包括yy-MM-dd-hh-mm-ss信息

java.time.LocalTime一个符合ISO-8601规范的、不包含时区信息的对象,包括hh-mm-ss信息


Local*,当地时间,更多代表着日常从钟表中看到的时间信息,作为日期的一种描述方式。如果没有具体的时区(ZoneID)或者时区偏移量(ZoneOffset)信息,Local*不能够代表一个Instant对象。Instant在不同的时区代表着不同的Local*.


java.time.ZonedDateTime一个符合ISO-8601规范的、包含时区信息的对象, 包括LocalDateTime  ZoneOffset ZoneId信息。利用 ZonedDateTime可以实现Local*和Instant之间的转换。 

使用java.time(JDK1.8),相较于使用java.util.Date (JDK1.0  一个具体到毫秒级的时刻), 是更为方便实现Time Globalization的方式。


Instant instant=Instant.now();
System.out.println("Instant: "+instant);

ZonedDateTime zonedDateTime1=ZonedDateTime.ofInstant(instant,ZoneId.of("Asia/Shanghai"));
System.out.println("ZonedDateTime1: "+zonedDateTime1);
LocalDateTime localDateTime1=zonedDateTime1.toLocalDateTime();
System.out.println("LocalDateTime1: "+localDateTime1);

ZonedDateTime zonedDateTime2=ZonedDateTime.ofInstant(instant,ZoneId.of("America/New_York"));
System.out.println("ZonedDateTime2: "+zonedDateTime2);
LocalDateTime localDateTime2=zonedDateTime2.toLocalDateTime();
System.out.println("LocalDateTime2: "+localDateTime2);
Output:
Instant: 2018-05-27T07:34:55.294Z
ZonedDateTime1: 2018-05-27T15:34:55.294+08:00[Asia/Shanghai]
LocalDateTime1: 2018-05-27T15:34:55.294
ZonedDateTime2: 2018-05-27T03:37:55.243-04:00[America/New_York]
LocalDateTime2: 2018-05-27T03:37:55.243

T代表“Time designator",Z代表“zero UTC offset”

2018-05-27T07:34:55.294Z

https://docs.oracle.com/javase/8/docs/api/java/time/package-summary.html

https://en.wikipedia.org/wiki/ISO_8601



原文地址:https://www.cnblogs.com/cnsec/p/13547594.html