Java 将时间戳 -923130000 和 -923130001 解析为 LocalDateTime 失败。两个结果之间的秒数是3599,太诡异了。
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.temporal.ChronoUnit;
public class TestLocalDateTime {
public static void main(String[] args) {
System.out.println("-923130000: " + toLocalDateTime(-923130000L));
System.out.println("-923130001: " + toLocalDateTime(-923130001L));
System.out.println(ChronoUnit.SECONDS.between(toLocalDateTime(-923130000L), toLocalDateTime(-923130001L)));
}
private static LocalDateTime toLocalDateTime(Long timestamp) {
return LocalDateTime.ofInstant(Instant.ofEpochSecond(timestamp), ZoneId.systemDefault());
}
}
输出:
-923130000: 1940-09-30T23:00
-923130001: 1940-09-30T23:59:59
3599
爪哇版:
$ java -version
java version "1.8.0_191"
Java(TM) SE Runtime Environment (build 1.8.0_191-b12)
Java HotSpot(TM) 64-Bit Server VM (build 25.191-b12, mixed mode)
代码在 macOS 上执行。