As Sun already said in another answer, you misunderstood: 2018-12-08T07:50:00+01:00
is the same point in time as 2018-12-08 06:50:00
in UTC (roughly the same as GMT), not 08:50
. +01:00
means that the time comes from a place where the clocks are 1 hour ahead of UTC, so to get the UTC time 1 hour should be subtracted, not added.
DateTimeFormatter desiredFormatter = DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm:ss");
String date = "2018-12-08T07:50:00+01:00";
OffsetDateTime dateTime = OffsetDateTime.parse(date);
OffsetDateTime dateTimeInUtc = dateTime.withOffsetSameInstant(ZoneOffset.UTC);
date = dateTimeInUtc.format(desiredFormatter);
System.out.println(date);
Output from this snippet is:
2018-12-08 06:50:00
Using your other example, 2018-12-08T07:50:00+04:00
, the output is
2018-12-08 03:50:00
I am taking advantage of the fact that your string is in ISO 8601 format. OffsetDateTime
parses this format as its default, that is, we don’t need to specify the format in a DateTimeFormatter
(as we do for your desired result format).
Link: Wikipedia article: ISO 8601