tl;dr
LocalDateTime.parse(
"2013-10-28 04:10:43.000000222".replace( " " , "T" )
)
Using java.time
The Answer by rgettman is correct. But now both the Question and that Answer are outdated.
You are using troublesome old date-time classes that are now legacy, supplanted by the java.time classes.
Also, your input string has nanoseconds but those old classes are limited to milliseconds. Fortunately the java.time classes handle nanoseconds.
ISO 8601
Your input string nearly complies with the standard ISO 8601 formats. To comply fully, replace the SPACE in the middle with a T
.
String input = "2013-10-28 04:10:43.000000222".replace( " " , "T" ) ;
The java.time classes use the standard ISO 8601 formats by default when parsing/generating strings. So no need to specify a formatting pattern.
LocalDateTime
Your input string lacks any indication of an offset-from-UTC or a time zone. So we parse as a LocalDateTime
.
LocalDateTime ldt = LocalDateTime.parse( input ) ;
ldt.toString(): 2013-10-28T04:10:43.000000222
As a LocalDateTime
, this does not represent a moment on the timeline. Is that 4 AM in India, 4 AM in France, or 4 AM in Québec? Those would all be different points on the timeline with the 4 AM in India happening several hours earlier in history than the 4 AM times in France or Québec.
ZonedDateTime
If you know for certain the intended time zone, apply it.
ZoneId z = ZoneId.of( "America/Montreal" ) ;
ZonedDateTime zdt = ldt.atZone( z );
zdt.toString(): 2013-10-28T04:10:43.000000222-04:00[America/Montreal]
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.