Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Date Format Issue With DateTimeFormatter [duplicate]

I have a date in the following format: 1/1/2020 3:4:7 AM I am trying to format it using DateTimeFormatter.

I have the following code with a formatter to parse it, but it doesn't work.

LocalDateTime date = LocalDateTime.parse("1/1/2020 3:4:7 AM", DateTimeFormatter.ofPattern("MM/dd/yyyy hh:mm:ss a"));

I get the following exception:

java.time.format.DateTimeParseException: Text '1/1/2020 3:4:7 AM' could not be parsed at index 0

Can anyone help me?

like image 814
user2254180 Avatar asked Dec 01 '22 08:12

user2254180


2 Answers

Two separate problems:

Wrong counts

You're using e.g. MM which is an explicit: Always the full amount of digits, zero-padded. Your string is not like that, it's just the number. So, make that M/d/uuuu h:m:s a.

EDIT: Changed yyyy to uuuu, thanks, @deHaar. Reasoning: yyyy or uuuu rarely matters, but note that this mean 4 digits are required. The difference kicks in for years before 0: uuuu goes negative, yyyy does not and expects you to use e.g. GG so that you get 44 BC instead of -44. In that way, uuuu is just more correct, even though usually the difference is not going to come up.

Missing locale

The second problem is that you should pretty much never use this version of ofPattern - it has a bug, which you can't catch with unit tests, which makes that a bug that is thousands of times 'heavier', and thus, a real problem.

You need to specify locale. Without it, 'AM' is not going to parse unless your platform default locale is english.

Putting it together

LocalDateTime date = LocalDateTime.parse("1/1/2020 3:4:7 AM",
  DateTimeFormatter.ofPattern("M/d/uuuu h:m:s a", Locale.ENGLISH));

works great.

like image 118
rzwitserloot Avatar answered Dec 20 '22 13:12

rzwitserloot


In your snippet:

LocalDateTime
    .parse("1/1/2020 3:4:7 AM", DateTimeFormatter.ofPattern("MM/dd/yyyy hh:mm:ss a"));
  • 1 - does not match MM
  • 1 - does not match dd
  • 3 - does not match hh
  • 4 - does not match mm
  • 7 - does not match ss

i.e. the lengths of the formatter pattern parts (e.g. MM) and their respective parts from the string text (e.g. 1) do not match.

You can match them in a few ways, e.g. you can either change the string text to match the formatter pattern or other way around.

You can try this instead:

LocalDateTime
    .parse("01/01/2020 03:04:07 AM", DateTimeFormatter.ofPattern("MM/dd/yyyy hh:mm:ss a"));

Additionally, have a look at the Pattern Letters and Symbols.

like image 25
Giorgi Tsiklauri Avatar answered Dec 20 '22 13:12

Giorgi Tsiklauri