我使用 LocalDateTime.now()在Java 8中获得 LocalDateTime。但有时这个 now() 函数会以不带秒的格式将时间返回给我。我认为这是因为秒为零,但我需要秒。
代码:
List <LocalDateTime> times = Arrays.asList(
LocalDateTime.now(),
LocalDateTime.parse("2020-09-13T20:53", DateTimeFormatter.ISO_LOCAL_DATE_TIME),
LocalDateTime.parse(LocalDateTime.now().format(DateTimeFormatter.ISO_LOCAL_DATE_TIME))
);
for (LocalDateTime time: times)
System.out.println("Time: " + time);
安慰:
Time: 2020-09-13T18:42:25.775
Time: 2020-09-13T20:53
Time: 2020-09-13T18:42:25.779
Time: 2020-09-13T20:53
已经没有秒了。
PD:我有一个调度程序,每 30 秒运行一次“LocalDatetime.now ()”。这就是它在控制台中显示的内容。
2020-09-10T09:14:00.001
2020-09-10T09:14:30.001
2020-09-10T09:15:00.001
2020-09-10T09:15:30.001
2020-09-10T09:16:00.001
2020-09-10T09:16:30
2020-09-10T09:17
2020-09-10T09:17:30.001
2020-09-10T09:18:00.001
2020-09-10T09:18:30
2020-09-10T09:19:00.001
2020-09-10T09:19:30.001
2020-09-10T09:20:00.001
2020-09-10T09:20:30
2020-09-10T09:21:00.001
2020-09-10T09:21:30
2020-09-10T09:22:00.001
2020-09-10T09:22:30.001
2020-09-10T09:23
2020-09-10T09:23:30.001
2020-09-10T09:24
2020-09-10T09:24:30
2020-09-10T09:25
2020-09-10T09:25:30
2020-09-10T09:26:00.001
2020-09-10T09:26:30