tl;dr
OffsetDateTime.parse( "2017-02-19T12:23:37.000000-00:00" )
Use java.time
You are using troublesome old date-time classes that are now legacy, supplanted by the java.time classes.
Milliseconds vs nanoseconds
The legacy classes support only milliseconds resolution, limited to three decimal places of fractional second. The java.time classes use nanoseconds resolution, for up to nine decimal places of fractional second. Your example data of six decimal places is for microseconds.
ISO 8601
Your input string happens to be in standard ISO 8601 format.
Well, almost standard: Your example has a negative zero offset-from-UTC. A negative zero offset is explicitly forbidden by the ISO 8601 standard. The standard requires a zero offset be marked with as a positive number with the +
sign rather than -
sign. However, RFC 3339 which claims to be a profile of ISO 8601 violates this rule with the unfortunate and unwise choice as a different connotation.
The java.time classes use ISO 8601 formats by default when parsing/generating strings. So no need to specify a formatting pattern at all.
OffsetDateTime
Fortunately for you, the OffsetDateTime
class tolerates the negative zero offset when parsing your input.
String input = "2017-02-19T12:23:37.123456-00:00" ;
OffsetDateTime odt = OffsetDateTime.parse( input );
See this code run live at IdeOne.com.
odt.toString(): 2017-02-19T12:23:37.123456Z
Notice the use of ISO 8601 formatting by OffsetDateTime::toString
when generating a string from our OffsetDateTime
. The Z
on the end is short for Zulu
, and means UTC.
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.