2

I have found myself repeating over and over again the same code pattern. I'm using some sort of date / time object (Java 8 API) and I end up having to roll out my own DateTimeFormatter:

DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")
DateTimeFormatter.ofPattern("yyyy-MM-dd")
DateTimeFormatter.ofPattern("HH:mm:ss")

which is fine but.. it gets me wondering whether there are not already pre-made ones done for me? I took a glance at the ones provided in the DateTimeFormatter singleton but they don't seem to be of much help.

2

2 Answers 2

4

I think the documentation of DateTimeFormatter is quite clear. The concrete patterns you have mentioned are supported by following predefined constants:

yyyy-MM-dd => ISO_LOCAL_DATE

HH:mm:ss => ISO_LOCAL_TIME

Only your first example "yyyy-MM-dd HH:mm:ss" is not matched by any predefined formatter so you can simply construct it by specifying the pattern (as you have already done). ISO_LOCAL_DATE_TIME is very similar but replaces the space by ISO-literal "T".

3

yyyy-MM-dd HH:mm:ss: assemble from built-in parts

As a supplement to the good answer by Meno Hochschild.

DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")

While not built in as it stands (even though occurring regularly), my preference is for assembling it from the two predefined formatters mentioned in that other answer rather than writing my own format pattern string:

    DateTimeFormatter formatter = new DateTimeFormatterBuilder()
            .append(DateTimeFormatter.ISO_LOCAL_DATE)
            .appendLiteral(' ')
            .append(DateTimeFormatter.ISO_LOCAL_TIME)
            .toFormatter();

ISO_LOCAL_TIME accepts and prints other formats than HH:mm:ss, though. On one hand it accepts a format without any seconds (HH:mm), on the other hand seconds with a fraction of up to 9 decimals (for example HH:mm:ss.SSSSSSSSS). It is often an advantage; but if you want to do strict validation of the parsed string or you want string output in a consistent format, it is a drawback, of course.

Or use String.replace(char, char)

Others would format and parse LocalDateTime objects to and from yyyy-MM-dd HH:mm:ss format without any explicit formatter, relying on its resemblance to the ISO 8601 format also mentioned by Meno Hochschild.

    String str = "2020-04-17 20:23:30";
    LocalDateTime ldt = LocalDateTime.parse(str.replace(' ', 'T'));
    System.out.println(ldt);

Output:

2020-04-17T20:23:30

And the other way:

    LocalDateTime now = LocalDateTime.now(ZoneId.of("Asia/Tehran"));
    String str = now.truncatedTo(ChronoUnit.SECONDS)
            .toString()
            .replace('T', ' ');
    System.out.println(str);

2020-04-18 09:35:38

2
  • 1
    The greater flexibility of ISO_LOCAL_TIME regarding formats like "HH:mm:ss.SSSSSSSSS" is of course correct. Well, it is up to the OPs needs what is better for him, to use that predefined constant or a strict pattern. Apr 18, 2020 at 5:04
  • I have been using the "replace by T" approach until now. I was not aware of the existence of DateTimeFormatterBuilder but that's why I created this SO question. Apr 20, 2020 at 8:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.