DateTimeFormatter Support for Single Digit Day of Month and Month of Year
Asked Answered
S

4

74

DateTimeFormmater doesn't seem to handle single digit day of the month:

String format = "MM/dd/yyyy";
String date   = "5/3/1969";
System.out.println(new SimpleDateFormat(format).parse(date));
System.out.println(LocalDate.parse(date, DateTimeFormatter.ofPattern(format)));

In this example, SimpleDateFormat correctly parses the date, but DateTimeFormatter throws an exception. If I were to use zero padded dates, e.g., "05/03/1969", both work. However, if either the day of month or the month of year are single digit, then DateTimeFormatter throws an exception.

What is the right DateTimeFormatter format to parse both one and two digit day of month and month of year?

Sonja answered 19/12, 2014 at 17:54 Comment(1)
Have you resolved this issue? I'm also looking into a java8 solution to parse 1 or 2 digits month, or 2 or 4 digits year.Carniola
P
111

From the documentation:

Number: If the count of letters is one, then the value is output using the minimum number of digits and without padding.

So the format specifier you want is M/d/yyyy, using single letter forms. Of course, it will still parse date Strings like "12/30/1969" correctly as for these day/month values, two digits are the “minimum number of digits”.

The important difference is that MM and dd require zero padding, not that M and d can’t handle values greater than 9 (that would be a bit… unusual).

Poundage answered 19/12, 2014 at 18:47 Comment(2)
I faced the same issue in Dart, but with above solution implemented using intl package, it is working properly now. Thank you.Cogent
This one worked, well explained @Holger. Link for documentation was helpful too.Tirado
E
16

In Java 8 Date Time API, I recently used

DateTimeFormatter formatter = new DateTimeFormatterBuilder()
            .appendOptional(DateTimeFormatter.ofPattern("M/dd/yyyy"))
            .toFormatter();

System.out.println(LocalDate.parse("10/22/2020", formatter));
System.out.println(LocalDate.parse("2/21/2020", formatter));
Erythrocytometer answered 5/2, 2020 at 19:7 Comment(3)
Why include the second pattern? What value would you expect to be parsed by the second but not the first?Intransitive
@JonSkeet very good suggestion by you. I have just removed the second pattern as single pattern is able to parse both type of values like 10/22/2020 and 2/22/2020Erythrocytometer
this is an great solution. I could add multiple patterns. thanksCalc
C
3

The best approach to deal with this kind of problem , That is number of different digits in Date (in day , month or year ) is to use this pattern : (M/d/[uuuu][uu]) .

Example:

String date = "7/7/2021"; // or "07/07/2021" or "07/7/21" etc
LocalDate localDate = LocalDate.parse(
date,DateTimeFormatter.ofPattern("M/d/[uuuu][uu]"));

Here uuuu/uu handle four and two digits year.

Chellean answered 7/12, 2021 at 12:4 Comment(0)
D
0
private final static DateTimeFormatter BIRTH_DAY_FORMATTER = DateTimeFormatter.ofPattern("yyyy-M-d");

public static LocalDate convertDate(String dateString) {
    return LocalDate.parse(dateString, BIRTH_DAY_FORMATTER);
}

You can use this way in your Utils class and since DateTimeFormatter is threadsafe, you can use it as a field in your Utils class

Decedent answered 25/12, 2022 at 10:51 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.