5

I'm using DateTimeFormatter to parse a date:

private final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy");
LocalDate.parse("05/12/2015", parser); // it's ok
LocalDate.parse("5/12/2015", parser); // Exception

Unfortunately it doesn't parse correctly dates with a single day digit, like "5/12/2015".

I read some other post as this but the proposed solution doesn't work for me. I need a way to parse dates that can have single or double digits day.

Community
  • 1
  • 1
drenda
  • 5,846
  • 11
  • 68
  • 141
  • 1
    "The proposed solution doesn't work for me" - in what way? What went wrong when you tried "d/M/yyyy"? It works for me... please show a short but complete program with it *not* working. – Jon Skeet Nov 12 '15 at 07:53

1 Answers1

11

Just use d instead of dd - that still allows a leading 0, but doesn't require it. I suspect you'd want to do the same for months as well - it would be odd to have "1-or-2 digit" days but not months...

import java.time.*;
import java.time.format.*;

public class Test {
    public static void main(String[] args) throws Exception {
        DateTimeFormatter parser = DateTimeFormatter.ofPattern("d/M/yyyy");
        System.out.println(LocalDate.parse("05/1/2015", parser));
        System.out.println(LocalDate.parse("05/01/2015", parser));
        System.out.println(LocalDate.parse("05/12/2015", parser));
        System.out.println(LocalDate.parse("5/12/2015", parser));
    }
}
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • 1
    Note: I wrote this answer before following the link in the question. It looks like it should be fine, but the OP claimed that the (equivalent) answer didn't work. We'll see what happens. I may delete this answer, or need to modify it... – Jon Skeet Nov 12 '15 at 07:54
  • Thanks your suggestion works! – drenda Nov 12 '15 at 08:17
  • 1
    @drenda: So what's different between this and the suggestion of using single letters (d instead of dd, M instead of MM) in the post that you said *didn't* work for you? – Jon Skeet Nov 12 '15 at 08:18
  • The single letters (d instead of dd....) works fine, instead in the question I linked I was referring to the response of use multiple parser to solve the problem (the second response). – drenda Nov 12 '15 at 11:35
  • 1
    @drenda: Any reason you only looked at the second solution (0 votes) rather than the first solution (6 votes, and basically the same as this)? If you're going to refer to "the proposed solution" I'd expect that to be an accepted or highest-voted one. – Jon Skeet Nov 12 '15 at 11:41
  • because before it had 1 vote, and the first one was not marked as correct response and it had no code. Probably my fault....but in any case the second response was wrong :-) – drenda Nov 12 '15 at 15:14
  • @drenda: Well, it had "the format specifier you want is `M/d/yyyy`" which was the only change in code needed to make the original code work. It doesn't seem like a reason to not bother reading or trying it, personally... – Jon Skeet Nov 12 '15 at 15:55
  • you are right. Sorry for this duplicated question. – drenda Nov 12 '15 at 16:03
  • More generic answer can be found in the below thread. https://stackoverflow.com/questions/27571377/datetimeformatter-support-for-single-digit-day-of-month-and-month-of-year/60082557#60082557 – Joginder Malik Feb 15 '21 at 16:24