How can I make a leading zero in DateTime-Pattern optional
Asked Answered
F

3

13

I have a user input field and would like to parse his date, whatever he puts in.

The user might provide his date with a leading zero or without one, so I wanna be able to parse an input like this

02.05.2019

and also this

2.5.2019

But as far as I can tell there is no way to make the leading zero optional, either always have 2 digits like 01, 03, 12 and so on, or only have the necessary digits like 1, 3, 12.

So apparently I have to decide whether to allow leading zeros or not, but is there seriously no way to make the leading zero optional ?


Well, I tested a pattern that included a leading zero dd.MM.uuuu and I tested a pattern that did not include a leading zero d.M.uuuu and when I parsed the wrong input with the wrong pattern exceptions were thrown.

Therefore my question is if there is a way to make the leading zero optional.

Frolic answered 7/8, 2019 at 11:54 Comment(2)
What pattern did you try? When you tested it, what happened?Thine
I think it should be possible with DateTimeFormatterBuilder.appendValue() and explicitely setting the min length of the field to 1 and maxlength to 2Nissen
L
25

This is trivial when you know it. One pattern letter, for example d or M, will accept either one or two digits (or for year up to 9 digits).

    DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("d.M.u");
    System.out.println(LocalDate.parse("02.05.2019", dateFormatter));
    System.out.println(LocalDate.parse("3.5.2019", dateFormatter));
    System.out.println(LocalDate.parse("4.05.2019", dateFormatter));
    System.out.println(LocalDate.parse("06.5.2019", dateFormatter));
    System.out.println(LocalDate.parse("15.12.2019", dateFormatter));

Output:

2019-05-02
2019-05-03
2019-05-04
2019-05-06
2019-12-15

I searched for this information in the documentation and didn’t find it readily. I don’t think it is well documented.

Leisaleiser answered 7/8, 2019 at 12:5 Comment(0)
C
6

You can create a DateTimeFormatter with a custom format like this

DateTimeFormatter.ofPattern("d.M.yyyy")

Then you can parse dates if they provide 1 or 2 digits for the day and month.

String input = "02.5.2019";
LocalDate date = LocalDate.parse(input, DateTimeFormatter.ofPattern("d.M.yyyy"));

I've used LocalDate here from the new java.time package so I'm assuming that your java version is recent.

Cobra answered 7/8, 2019 at 12:7 Comment(0)
P
2

Your suggested date format should work - just as this test:

@Test
public void test() throws ParseException {
    SimpleDateFormat f = new SimpleDateFormat("d.M.yyyy");
    f.parse("7.8.2019");
    f.parse("07.08.2019");
    f.parse("007.008.002019");
}

The DateTimeFormatter will not accept leading zeros for year in comparison, but leading zeros for day and month are not an issue:

@Test
public void test2() throws ParseException {
    DateTimeFormatterBuilder builder = new DateTimeFormatterBuilder();
    DateTimeFormatter f = builder.appendPattern("d.M.yyyy").toFormatter();
    f.parse("7.8.2019");
    f.parse("07.08.2019");
    f.parse("007.008.2019");
}
Paraesthesia answered 7/8, 2019 at 12:10 Comment(2)
The question mentions the pattern dd.MM.uuuu. I assume it’s uuuu for year, which means they can’t be using SimpleDateFormat (where lowercase u means Day number of week). Which is for the better since the SimpleDateFormat class is notoriously troublesome and long outdated.Leisaleiser
It’s interesting that it accepts 3 digits for day and month, which can never be more than 31 and 12, but rejects a leading zero in year, which can be up to 999 999 999. It should solve the questioner’’s issue though. Apparently when you say that year is 4 digits, it’s taken as exactly 4 digits, not three or five.Leisaleiser

© 2022 - 2024 — McMap. All rights reserved.