That's a common error, based on the misconception that dates have formats - but they actually don't.
Date/time objects have only values, and those values - usually numerical - represent the concept of a date (a specific point in the calendar) and a time (a specific moment of the day).
If you have a String
, then you don't actually have a date. You have a text (a sequence of characters) that represents a date. Note that all of the strings below are different (they have a different sequence of characters), but all represent the same date (the same values, the same point in the calendar):
2018-03-20 09:31:31
03/20/2018 9:31:31 AM
(using USA's format: month/day/year)
Tuesday, March 20th 2018, 09:31:31 am
- and many others...
What you want to do is to get one format (one String
, one text representing a date) and transform it to another format (anoter String
, another different sequence of characters that represents the same date).
In Java (and in many other languages - if not all - btw) you must do it in 2 steps:
- convert the
String
to a date/time object (convert the text to the numerical values) - that's what the parse
method does
- convert the date/time object to another format (convert the numerical values to another text)
That said, when you call the parse
method, you're trying to transform a String
(a text, a sequence of characters) into a date/time object. This means that the DateTimeFormatter
must have a pattern that matches the input.
The input is 2018-03-20 09:31:31
, which is year-month-day hour:minute:second
. And the formatter you used to parse it has the pattern MM/dd/yyyy HH:mm:ss a
(month/day/year hour:minute:second am/pm
).
You used the output pattern (the one that should be used in step 2) to parse the input. That's why you've got an exception: the formatter tried to parse a month with 2 digits followed by a /
when the input actually contains a year with 4 digits followed by a -
.
You must use a different DateTimeFormatter
for each step, using the correct pattern for each case. YCF_L's answer has the code that does the job, I'd just like to add one little detail. The formatter used for the output (step 2) is:
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(
"MM/dd/yyyy HH:mm:ss a", Locale.ENGLISH
);
Note that HH
is used for the hours. Take a look at the javadoc and you'll see that uppercase HH
represents the hour-of-day fields (values from 0 to 23 - so 1 AM is printed as 01
and 1 PM is printed as 13
).
But you're also printing the AM/PM field (the a
in the pattern), so maybe what you need is actually the lowercase hh
, which is the clock-hour-of-am-pm (values from 1 to 12) or even KK
(hour-of-am-pm (values from 0 to 11)).