DateTimeFormatter with multiple pattern not working

92 Views Asked by At

Both of these patterns working fine when tested separately but they are not working together in a single ofPattern { "[]" + "[]"} statement.

Please guide what am I missing here

import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;

public class SampleProgram {
    
    private static final DateTimeFormatter dateTimeFormats = DateTimeFormatter.ofPattern(
            "[yyyy-MM-dd'T'HH:mm:ss.SSS]" + //: Year, month, day, 'T' separator, hour, minute, second, and milliseconds (e.g., 2023-05-21T12:34:56.789)
            "[yyyy-MM-dd'T'HH:mm:ss.SSSSSS]"  //: Year, month, day, 'T' separator, hour, minute, second, and microseconds (e.g., 2023-05-21T12:34:56.123456)
    );

    private static final String[] inputArray = new String[]{
        "2021-01-30T23:45:00.123",
        "2023-08-15T12:34:56.789123",
    };

    
    public static void main(String[] args) {
        
        for (var input : inputArray) {
            var ignore = LocalDateTime.parse(input, dateTimeFormats);
        }
        System.out.println("No exception occured!");
    }
}

Output:

Exception in thread "main" java.time.format.DateTimeParseException: Text '2023-08-15T12:34:56.789123' could not be parsed, unparsed text found at index 23
at java.base/java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:2049)
    at java.base/java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1948)at java.base/java.time.LocalDateTime.parse(LocalDateTime.java:492)
    at SampleProgram.main(SampleProgram.java:25)
2

There are 2 best solutions below

0
Arvind Kumar Avinash On BEST ANSWER

You do not need any DateTimeFormatter because both of your date-time strings are in ISO 8601 format which is the default format used by java.time types.

public static void main(String[] args) {
    for (var input : inputArray) {
        var ignore = LocalDateTime.parse(input);
    }
    System.out.println("No exception occured!");
}

Output:

No exception occurred!

In case, you want to use your DateTimeFormatter, change it to make just the fraction-of-second part optional.

DateTimeFormatter dateTimeFormats = DateTimeFormatter.ofPattern(
                    "yyyy-MM-dd'T'HH:mm:ss[.[SSSSSS][SSS]]");

Online Demo

Learn more about the modern Date-Time API from Trail: Date Time.

0
Ch.K. On

If you change the order of the pattern to

private static final DateTimeFormatter dateTimeFormats = DateTimeFormatter.ofPattern( "[yyyy-MM-dd'T'HH:mm:ss.SSSSSS]" + //: Year, month, day, 'T' separator, hour, minute, second, and microseconds (e.g., 2023-05-21T12:34:56.123456) "[yyyy-MM-dd'T'HH:mm:ss.SSS]" //: Year, month, day, 'T' separator, hour, minute, second, and milliseconds (e.g., 2023-05-21T12:34:56.789) );

this should do the trick.

So the timestamp with microseconds will be handled first with the parser for microseconds. In our example the microsecond timestamp is handled first by the millisecond parser, which complaints about the microsecond part he cannot handle.