Your Code Works, As Of Java 8 Update 51
Your code is working now, as of Java 8 Update 51 on Mac OS X Mountain Lion. The Answer by Holger that there may have been a bug in earlier versions of Java. Understandable as the java.time framework is brand-new in Java 8.
Here is a modified copy of your code.
String dateStr = "2014-08-16T05:03:45-05:00";
TemporalAccessor creationAccessor = DateTimeFormatter.ISO_OFFSET_DATE_TIME.parse( dateStr );
Instant instant = Instant.from( creationAccessor );
long millisSinceEpoch = instant.toEpochMilli( );
ZonedDateTime zdt = ZonedDateTime.ofInstant( instant, ZoneOffset.of( "-05:00" ) );
Dump to console.
System.out.println( "dateStr: " + dateStr );
System.out.println( "instant: " + instant );
System.out.println( " millis: " + millisSinceEpoch );
System.out.println( " zdt: " + zdt );
When run.
dateStr: 2014-08-16T05:03:45-05:00
instant: 2014-08-16T10:03:45Z
millis: 1408183425000
zdt: 2014-08-16T05:03:45-05:00
Canonical Method:
parse(CharSequence text, TemporalQuery<T> query)
You may want to accomplish your parsing using an alternate method.
The class doc for DateTimeFormatter
mentions that the usual way to parse should be a call to DateTimeFormatter::parse(CharSequence text, TemporalQuery<T> query)
rather than DateTimeFormatter::parse(CharSequence text)
.
So instead of this:
String input = "2007-12-03T10:15:30+01:00[Europe/Paris]" ;
TemporalAccessor temporalAccessor = DateTimeFormatter.ISO_DATE_TIME.parse( input ) ;
…do this, where we add a second argument, the argument being a method reference in Java 8 syntax, to call the conversion from
method (in this example, ZonedDateTime :: from
):
String input = "2007-12-03T10:15:30+01:00[Europe/Paris]" ;
ZonedDateTime zdt = DateTimeFormatter.ISO_DATE_TIME.parse( input , ZonedDateTime :: from ) ;
Dump to console.
System.out.println("input: " + input );
System.out.println(" zdt: " + zdt );
When run.
input: 2007-12-03T10:15:30+01:00[Europe/Paris]
zdt: 2007-12-03T10:15:30+01:00[Europe/Paris]