Instant instant = ldt.toInstant(ZoneOffset.UTC);
Date date = Date.from(instant);
例子:
考虑到:
Date date = new Date();
System.out.println(date + " long: " + date.getTime());
(1) LocalDateTime <# EYZ1< & lt;# EYZ2:
从Date创建Instant:
Instant instant = Instant.ofEpochMilli(date.getTime());
System.out.println("Instant from Date:\n" + instant);
从Instant创建Date(不是必需的,但用于说明):
date = Date.from(instant);
System.out.println("Date from Instant:\n" + date + " long: " + date.getTime());
从Instant创建LocalDateTime
LocalDateTime ldt = LocalDateTime.ofInstant(instant, ZoneOffset.UTC);
System.out.println("LocalDateTime from Instant:\n" + ldt);
(2) Date <# EYZ1 & lt; & lt;# EYZ2
从LocalDateTime创建Instant:
instant = ldt.toInstant(ZoneOffset.UTC);
System.out.println("Instant from LocalDateTime:\n" + instant);
从Instant创建Date:
date = Date.from(instant);
System.out.println("Date from Instant:\n" + date + " long: " + date.getTime());
输出结果为:
Fri Nov 01 07:13:04 PDT 2013 long: 1383315184574
Instant from Date:
2013-11-01T14:13:04.574Z
Date from Instant:
Fri Nov 01 07:13:04 PDT 2013 long: 1383315184574
LocalDateTime from Instant:
2013-11-01T14:13:04.574
Instant from LocalDateTime:
2013-11-01T14:13:04.574Z
Date from Instant:
Fri Nov 01 07:13:04 PDT 2013 long: 1383315184574
Date in = new Date();
LocalDateTime ldt = LocalDateTime.ofInstant(in.toInstant(), ZoneId.systemDefault());
Date out = Date.from(ldt.atZone(ZoneId.systemDefault()).toInstant());