2018-04-29T10:15:30.00Z对应2018-04-29T12:15:30+02:00,OffsetDateTime
的LocalDateTime
为2018-04-29T12:15:30;您的LocalDateTime.now(c)
也是.
Demo:个个
import java.time.*;
public class Main {
public static void main(String[] args) {
Instant instant = Instant.parse("2018-04-29T10:15:30.00Z");
ZoneId zoneId = ZoneId.of("Europe/Berlin");
Clock c= Clock.fixed(instant, zoneId);
// 2018-04-29T10:15:30.00Z corresponds to 2018-04-29T12:15:30+02:00
OffsetDateTime odt = instant.atZone(zoneId).toOffsetDateTime();
System.out.println(odt);
// LocalDateTime from this OffsetDateTime results in 2018-04-29T12:15:30
System.out.println(odt.toLocalDateTime());
// So does LocalDateTime.now(c)
System.out.println(LocalDateTime.now(c));
}
}
Output:个个
2018-04-29T12:15:30+02:00
2018-04-29T12:15:30
2018-04-29T12:15:30
如何获得您想要的输出?
您需要使用UTC偏移量来获得与给定Instant
具有相同值的LocalDateTime
.
Demo:个个
import java.time.*;
public class Main {
public static void main(String[] args) {
Clock c = Clock.fixed(Instant.parse("2018-04-29T10:15:30.00Z"), ZoneId.of("Europe/Berlin"));
LocalDateTime ldt = Instant.now(c).atZone(ZoneOffset.UTC).toLocalDateTime();
System.out.println(ldt);
}
}
Output:个个
2018-04-29T10:15:30
Online Demo个
从100了解现代Date-Time API.