Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

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.

like image 651
John Du Avatar asked Aug 07 '19 11:08

John Du


People also ask

How do you add leading zeros to a date?

Show activity on this post. var MyDate = new Date(); var MyDateString; MyDate. setDate(MyDate. getDate() + 20); MyDateString = ('0' + MyDate.

How do you remove leading zeros from a date in Java?

For a Regular Expression approach, you can use: String shipDate = '04/06/2022'; // Option 1: shipDate = shipDate. replaceAll('\\b0(\\d)','$1'); // Option 2: shipDate = shipDate. replaceAll('\\b0',''); System.

Should dates have leading zeros?

In many -- possibly most -- calendar date picker widgets for U.S. users which display the selected date in month/day/year format, leading zeroes are shown when showing a single-digit month and/or day value. For example, the date March 1, 2018 would be rendered as 03/01/2018 .

How do you remove leading zeros from a date in python?

Answer #1: Actually I had the same problem and I realized that, if you add a hyphen between the % and the letter, you can remove the leading zero. For example %Y/%-m/%-d .


3 Answers

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.

like image 148
Ole V.V. Avatar answered Nov 15 '22 20:11

Ole V.V.


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.

like image 25
Clint Munden Avatar answered Nov 15 '22 22:11

Clint Munden


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");
}
like image 41
Mick Avatar answered Nov 15 '22 22:11

Mick