tl;dr
LocalDateTime.parse(
"27/3/2015 2:47:08 AM" ,
DateTimeFormatter.ofPattern( "d/M/uuuu h:m:s a" , Locale.US )
)
2015-03-27T02:47:08
java.time
Your input string seems especially odd. The hour does not have a padding zero but the second does? Strange, but it can be parsed using the modern java.time classes.
The Joda-Time project is now in maintenance mode, with the team advising migration to the java.time classes.
You were inappropriately trying to store a date-time value lacking any offset or time zone information into a data type wielding a time zone.Parse your input string as a LocalDateTime
since we have no indication of offset-from-UTC or time zone.
String input = "27/3/2015 2:47:08 AM";
DateTimeFormatter f = DateTimeFormatter.ofPattern( "d/M/uuuu h:m:s a" , Locale.US );
LocalDateTime ldt = LocalDateTime.parse( input , f );
Dump to console.
System.out.println( input + " = " + ldt );
27/3/2015 2:47:08 AM = 2015-03-27T02:47:08
Be aware that a LocalDateTime
is not a point on the timeline. It represents only a set of possible moments over a range of about 26-27 hours. It has no real meaning until you place the value in context, either applying an offset to get a OffsetDateTime
or a full time zone to get a ZonedDateTime
.
About java.time
The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date
, Calendar
, & SimpleDateFormat
.
The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.
To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.
Where to obtain the java.time classes?
The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval
, YearWeek
, YearQuarter
, and more.