Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DateTimeFormatter Support for Single Digit Day of Month and Month of Year

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?

like image 893
Mark Maxey Avatar asked Dec 19 '14 17:12

Mark Maxey


People also ask

How do I use DateTimeFormatter with date?

DateTimeFormatter fmt = DateTimeFormatter. ofPattern("yyyy-MM-dd'T'HH:mm:ss"); System. out. println(ldt.

What is the difference between DateTimeFormatter and SimpleDateFormat?

DateTimeFormatter is a replacement for the old SimpleDateFormat that is thread-safe and provides additional functionality.

What is DateTimeFormatter Iso_date_time?

ISO_DATE_TIME. The ISO-like date-time formatter that formats or parses a date-time with the offset and zone if available, such as '2011-12-03T10:15:30', '2011-12-03T10:15:30+01:00' or '2011-12-03T10:15:30+01:00[Europe/Paris]'. static DateTimeFormatter.

Is Java Time format DateTimeFormatter thread-safe?

Yes, it is: DateTimeFormat is thread-safe and immutable, and the formatters it returns are as well.


3 Answers

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).

like image 74
Holger Avatar answered Oct 02 '22 00:10

Holger


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)); 
like image 28
Joginder Malik Avatar answered Oct 02 '22 00:10

Joginder Malik


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.

like image 44
Vikash Kumar Avatar answered Oct 02 '22 00:10

Vikash Kumar