Try this (Java 8):
String input = "2016-10-14T14:19:40-04:00";
String output = ZonedDateTime.parse(input).toInstant().toString();
System.out.println(output); // prints 2016-10-14T18:19:40Z
This works because the primary format of ZonedDateTime
used by parse()
is ISO_ZONED_DATE_TIME
, so it will parse input in the format you described.
The output format you requested happens to be the default format of Instant
, as returned by its toString()
method, i.e. the ISO_INSTANT
format.
Converting between the two (ZonedDateTime
to Instant
using the toInstant()
method) will perform the timezone adjustment necessary.