tl;dr
How can I fix this problem?
Match your formatting pattern to your input.
LocalDateTime.parse(
"01/06/2015 8:20" ,
DateTimeFormatter.ofPattern( "dd/MM/uuuu H:m" )
)
.toString()
2015-06-01T08:20
java.time
As others stated, your formatting pattern failed to match your string input.
Also, you are using troublesome old date-time classes supplanted years ago by the java.time classes.
Your time-of-day hour lacks a padding zero. So you should have been using one h
rather than two hh
. Furthermore, a lowercase h
means 12-hour clock, where your input seems to be 24-hour clock given the lack of an AM/PM indicator. So an uppercase H
is in order.
String input = "01/06/2015 8:20" ; // Notice the lack of a padding zero on the hour.
DateTimeFormatter f = DateTimeFormatter.ofPattern( "dd/MM/uuuu H:m" ) ;
LocalDateTime ldt = LocalDateTime.parse( input , f ) ;
ldt.toString(): 2015-06-01T08:20
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.
You may exchange java.time objects directly with your database. Use a JDBC driver compliant with JDBC 4.2 or later. No need for strings, no need for java.sql.*
classes.
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.