I have a Date String in UTC format with Zone. I have to convert it into SQL Timestamp with +/- Zone into time using Java 8.0 E.g.
2016-10-14T10:12:18.000+02:00
needs to be converted to 2016-10-14T08:12:18.000+00:00
or 2016-10-14T8:12:18.000Z
String utcDate = "2016-10-14T10:12:18.100+02:00";
ZonedDateTime result = ZonedDateTime.parse(utcDate, DateTimeFormatter.ISO_DATE_TIME);
System.out.println("Parsed: " + result);
Output:
Parsed: 2016-10-14T10:12:18.100+02:00
It doesn't add the zone +02:00 to Date.
Well currently you're not doing anything to convert the value - you're using a ZonedDateTime
, so it's preserving the time zone (at least, the offset) that was provided. (I'd use OffsetDateTime
for this rather than ZonedDateTime
, as you don't actually have a time zone.)
You could either use withZoneSameInstant
:
ZonedDateTime utc = result.withZoneSameInstant(ZoneOffset.UTC);
Or just convert to an Instant
:
Instant instant = result.toInstant();
(and then format appropriately, of course).
See more on this question at Stackoverflow