TL; DR
LocalDateTime.parse( "2011-08-19 06:11:03.0".replace( " " , "T" ) )
More details
Your input string does not match your formatting pattern. Your template has oblique characters in which you have hyphens.
java.time
In addition, you use the terrible old time classes, which are now obsolete, being superseded by java.time classes.
Your input string is nearly ISO 8601 standard for date and time formats. Replace SPACE in the middle with T
String input = "2011-08-19 06:11:03.0".replace( " " , "T" ) ;
There is no time zone indicator or off-UTC in your input. Therefore, we analyze as LocalDateTime for an object that does not have the concept of zone / offset.
LocalDateTime ldt = LocalDateTime.parse( input ) ;
To generate a string in a standard format, call toString .
String output = ldt.toString() ;
If this entry was intended for a specific time zone, assign it.
ZoneId z = ZoneId.of( "Africa/Tunis" ) ; ZonedDateTime zdt = ldt.atZone( z ) ;
About java.time
The java.time framework is built into Java 8 and later. These classes supersede the nasty old legacy time classes such as java.util.Date , Calendar and SimpleDateFormat .
The Joda-Time project, now in maintenance mode , we recommend switching to the java.time classes.
To learn more, see the Oracle Tutorial . And search for qaru for many examples and explanations. JSR 310 specification .
With a JDBC driver corresponding to JDBC 4.2 or later, you can exchange java.time objects directly with your database. No need for strings or java.sql. * Classes.
Where to get java.time classes?
- Java SE 8 , Java SE 9 , and then
- Built in.
- Part of the standard Java API with integrated implementation.
- Java 9 adds some minor features and fixes.
- Java SE 6 and Java SE 7
- Most of the functionality of java.time is ported back to Java 6 and 7 in ThreeTen-Backport .
- Android
- Later versions of the Android package implementations of the java.time classes.
- For earlier Android, the ThreeTenABP project adapts ThreeTen-Backport (mentioned above). See How to use ThreeTenABP ....
The ThreeTen-Extra project extends java.time with additional classes. This project is a proof of possible future additions to java.time. Here you can find useful classes such as Interval , YearWeek , YearQuarter and more .