It may very well be possible with SimpleDateFormat
, but you will probably prefer to use java.time
, the modern Java date and time API:
DateTimeFormatter formatter
= DateTimeFormatter.ofPattern("MMM dd uuuu hh:mm:ssa", Locale.ENGLISH);
String dateTimeString = "Dec 12 2001 11:59:59PM";
LocalDateTime dateTime = LocalDateTime.parse(dateTimeString, formatter);
System.out.println(dateTime);
Output:
2001-12-12T23:59:59
As others have said, your problem was not with the lack of a space between seconds and AM/PM marker, but with using uppercase HH
for the hours. Uppercase HH
is for hour of day from 00 through 23, where what you wanted was lowercase hh
for hour within AM or PM from 01 through 12.
And as yet others have said, there are issues with using SimpleDateFormat
and its friend Date
:
- Those classes are long outdated.
- Those classes are poorly designed, and
SimpleDateFormat
in particular is renowned for being troublesome. Your experience is typical and certainly not unusual.
- Getting a correct result from
SimpleDateFormat
requires that either the JVM time zone setting agrees with the time zone understood in the string, or you set the time zone of the SimpleDateFormat
to the relevant time zone. The former is hard to guarantee since the time zone setting can be changed any time from another part of your program or from other programs running in the same JVM.
This also means that if you do require an instance of the outdated Date
class (for example for a legacy API that you don’t want to change just now), you will need to decide on a time zone for the conversion. Then convert for example like this:
Instant inst = dateTime.atZone(ZoneId.of("America/Metlakatla")).toInstant();
Date oldfashionedDate = Date.from(inst);
System.out.println(oldfashionedDate);
I hesitate to show you the output because Date
shows a quite surprising behaviour here.
Thu Dec 13 08:59:59 CET 2001
08:59? On December 13? The conversion has given you the correct point in time. When I print the Date
, its toString
method is invoked. This in turn uses my JVM’s time zone setting for producing the string, so the output is in a completely different time zone from the one where the conversion happened. So apparently when it’s 23:59 in Metlakatla, it’s already 08:59 the next day in Copenhagen (my time zone; CET in the output is for Central European Time). Had my JVM’s time zone setting been America/Metlakatla too, the output would have agreed more with the expected:
Wed Dec 12 23:59:59 AKST 2001
java.time is more helpful
What you asked SimpleDateFormat
to do was to parse a time that had hour of day 11 and PM. This is really self contradictory since PM only begins at hour of day 12. So it would be reasonable to expect an exception from the request. A SimpleDateFormat
with standard settings doesn’t give you that. It’s very typical for SimpleDateFOrmat
to give you a wrong result and pretend all is well. However let’s for a moment try my modern code with your format pattern string of MMM dd yyyy HH:mm:ssa
. Then we get:
Exception in thread "main" java.time.format.DateTimeParseException:
Text 'Dec 12 2001 11:59:59PM' could not be parsed: Conflict found:
Field AmPmOfDay 0 differs from AmPmOfDay 1 derived from 11:59:59
I don’t claim I understand exactly why it is worded like this, but it is mentioning a conflict in the AM/PM, which is exactly what we have.
PS
I hadn’t thought at first that I’d contribute an answer, but in the end I was provoked by on one hand bohemian’s comment that only Joop Eggen’s answer was correct and on the other hand a couple of comments by Basil Bourque claiming that you could not use the SimpleDateFormat
that Joop Eggen was using. So I wanted to set things straight.
Link
Oracle tutorial: Date Time explaining how to use java.time
.
SimpleDateFormat
to parse this input lacking any indication of time zone or offset-from-UTC. Any result will be injecting a time zone where it does not belong. See the correct Answer that usesLocalDateTime
class.SimpleDateFormat
class. It is not only long outdated, it is also notoriously troublesome. Today we have so much better injava.time
, the modern Java date and time API and itsDateTimeFormatter
. AlsoDateTimeFormatter
will inform you of your error if you try parsing using the pattern in the question.