如何使用 Java 上的 LocalDateTime.now() 获取当前的 LocalDateTime,包括秒数 8

How to get the current LocalDateTime including seconds with LocalDateTime.now() on Java 8

我在 Java 8 中使用 LocalDateTime.now() 获取 LocalDateTime。但有时这个 now() 函数 returns 以没有秒的格式向我显示时间。我以为是因为秒为零,但我需要秒。

代码:

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

这是 Java 8 中 LocalDateTime 的一个特性——如果秒数是“00”,那么它将被丢弃。

你可以了解更多

一种方法是为输出创建特定的 DateTimeFormatter,因为将 LocalDateTimeString 连接使用 toString() 方法,该方法会截断零毫秒或甚至秒。

这样做,例如:

public static void main(String[] args) {
    DateTimeFormatter isoDtf = DateTimeFormatter.ISO_LOCAL_DATE_TIME;
    List <LocalDateTime> times =
        Arrays.asList(LocalDateTime.now(),
            LocalDateTime.parse("2020-09-13T20:53", isoDtf),
            LocalDateTime.parse(LocalDateTime.now().format(isoDtf)));
                                
    // specify the output format, require the units you need explicitly
    DateTimeFormatter outputFormatter = DateTimeFormatter
                                            .ofPattern("uuuu-MM-dd'T'HH:mm:ss.SSS");
    // and explicitly output the formatter LocalDateTime
    for (LocalDateTime time: times)
        System.out.println("Time: "+ time.format(outputFormatter));
}

这输出

Time: 2020-09-15T15:09:08.011
Time: 2020-09-13T20:53:00.000
Time: 2020-09-15T15:09:08.027

如果你只想要秒数,built-in 格式化程序(你用于解析的那个)就足够了,因为下面的代码

public static void main(String[] args) {
    DateTimeFormatter isoDtf = DateTimeFormatter.ISO_LOCAL_DATE_TIME;
    List <LocalDateTime> times =
        Arrays.asList(LocalDateTime.now(),
            LocalDateTime.parse("2020-09-13T20:53", isoDtf),
            LocalDateTime.parse(LocalDateTime.now().format(isoDtf)));
    // and explicitly output the formatter LocalDateTime
    for (LocalDateTime time: times)
        System.out.println("Time: "+ time.format(isoDtf));
}

产出

Time: 2020-09-15T15:12:55.592
Time: 2020-09-13T20:53:00
Time: 2020-09-15T15:12:55.623

并仅截断秒的零小数部分,但不触及秒。