Search code examples
javaspringlocaldatelocaldatetimedatetimeformatter

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


I get LocalDateTime in Java 8 with LocalDateTime.now(). But sometimes this now() function returns the time to me in a format without seconds. I assumed it is because the second are zero, but I need the seconds.

Code:

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);

Console:

Time: 2020-09-13T18:42:25.775
Time: 2020-09-13T20:53
Time: 2020-09-13T18:42:25.779

Time: 2020-09-13T20:53 has not seconds.

PD: I have a scheduler that runs "LocalDatetime.now ()" every 30 seconds. This is what it shows in console.

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

Solution

  • One way would be to create a specific DateTimeFormatter for the output because concatenating the LocalDateTime with a String uses the toString() method, which truncates zero millis or even seconds.

    Do it like this, for example:

    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));
    }
    

    This outputs

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

    If you just want the seconds, a built-in formatter (the one you are using for parsing) would be sufficient, because the following code

    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));
    }
    

    outputs

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

    and truncates zero fractions of second only, but doesn't touch seconds.