LocalDateTime
class doesn't have any timezone/offset information. So I suspect that the problem is (although I haven't tested in a Spring environment): Spring is parsing the string as an OffsetDateTime
(as the format contains an offset, like -03:00
) and getting the local date time part of it (getting rid of the offset). Or doing some other similar thing but ignoring the offset.
When doing this, no time conversion is done. So I think the best solution is to change the field to OffsetDateTime
or Instant
(see more details below). If not possible, you can convert this OffsetDateTime
to a LocalDateTime
, using the following code:
// convert OffsetDateTime to LocalDateTime (converting the time to UTC)
LocalDateTime localDateTime = OffsetDateTime.parse("2017-05-31T12:00-03:00")
// change to UTC ("sameInstant" converts the time)
.withOffsetSameInstant(ZoneOffset.UTC)
// get only localdatetime part (without offset)
.toLocalDateTime();
System.out.println(localDateTime);
withOffsetSameInstant(ZoneOffset.UTC)
converts the time to UTC. So the output for the code above is:
2017-05-31T15:00
You can also parse the UTC string with the same code:
localDateTime = OffsetDateTime.parse("2017-05-31T12:00Z")
.withOffsetSameInstant(ZoneOffset.UTC)
.toLocalDateTime();
System.out.println(localDateTime);
Note that in this case, withOffsetSameInstant(ZoneOffset.UTC)
is redundant because the string is already in UTC (it ends with "Z"). But you can leave it without any problem.
The output will be:
2017-05-31T12:00
What might be the error cause
Note that if you don't use withOffsetSameInstant
, you get an incorrect result:
localDateTime = OffsetDateTime.parse("2017-05-31T12:00-03:00").toLocalDateTime();
System.out.println(localDateTime); // 2017-05-31T12:00 (12h instead of 15h)
That's what I suspect that Spring is doing. Or maybe it's parsing the LocalDateTime
using a parser that ignores the offset - something very similar to this:
System.out.println(LocalDateTime.parse("2017-05-31T12:00-03:00",
DateTimeFormatter.ISO_DATE_TIME));
// output is 2017-05-31T12:00
Anyway, Spring is ignoring the offset. You can try to write a converter (using the code described above) or use the approach described below.
A better approach when dealing with timezones
IMO, using a LocalDateTime
is not the best approach when dealing with date/times that can handle multiple timezones and offsets. That's because a LocalDateTime
doesn't have any timezone/offset information and can't handle it properly.
I think the best approach in this case is to use OffsetDateTime
or Instant
. I believe that Instant
is best for the reasons below.
If you choose to change your field type to OffsetDateTime
, you can convert it to UTC with withOffsetSameInstant(ZoneOffset.UTC)
:
// 2017-05-31T15:00Z
System.out.println(OffsetDateTime.parse("2017-05-31T12:00-03:00").withOffsetSameInstant(ZoneOffset.UTC));
// 2017-05-31T12:00Z
System.out.println(OffsetDateTime.parse("2017-05-31T12:00Z").withOffsetSameInstant(ZoneOffset.UTC));
To convert this OffsetDateTime
to another timezone and back to UTC, you can do:
OffsetDateTime utcOffset = OffsetDateTime.parse("2017-05-31T12:00-03:00").withOffsetSameInstant(ZoneOffset.UTC);
System.out.println(utcOffset); // 2017-05-31T15:00Z
// convert to London timezone
ZonedDateTime z = utcOffset.atZoneSameInstant(ZoneId.of("Europe/London"));
System.out.println(z); // 2017-05-31T16:00+01:00[Europe/London]
// convert back to UTC
System.out.println(z.withZoneSameInstant(ZoneOffset.UTC)); // 2017-05-31T15:00Z
Note that 15h in UTC is 16h in London, because in May it's British's Summer Time, and the API handles it automatically.
If you choose to use an Instant
(which is a UTC instant, independent of timezone/offset), you can parse it this way:
// 2017-05-31T15:00:00Z
System.out.println(OffsetDateTime.parse("2017-05-31T12:00-03:00").toInstant());
// 2017-05-31T12:00:00Z
System.out.println(OffsetDateTime.parse("2017-05-31T12:00Z").toInstant());
If you want to make it explicit that the date/time is in UTC, I believe that Instant
is the best choice. If you use LocalDateTime
, it's not clear in what timezone it is (actually, because this class has no such information, technically it's not in any timezone), and you must remember (or store this information anywhere else). With Instant
, the usage of UTC is explicit.
The conversion from and to another timezones is very easy with Instant
:
// UTC instant (2017-05-31T15:00:00Z)
Instant instant = OffsetDateTime.parse("2017-05-31T12:00-03:00").toInstant();
// converts to London timezone
ZonedDateTime london = instant.atZone(ZoneId.of("Europe/London"));
System.out.println(london); // 2017-05-31T16:00+01:00[Europe/London]
// ** note that 15h in UTC is 16h in London, because in May it's British's Summer Time
// converts back to UTC instant
System.out.println(london.toInstant()); // 2017-05-31T15:00:00Z
Note that London's Summer Time was also applied.
Converts the Instant
to/from OffsetDateTime
is also straightforward:
// converts to offset +05:00
OffsetDateTime odt = instant.atOffset(ZoneOffset.ofHours(5));
System.out.println(odt); // 2017-05-31T20:00+05:00
// converts back to UTC instant
System.out.println(odt.toInstant()); // 2017-05-31T15:00:00Z
But if you use LocalDateTime
, is not so obvious to handle some cases:
// LocalDateTime (2017-05-31T15:00)
LocalDateTime dt = OffsetDateTime.parse("2017-05-31T12:00-03:00")
.withOffsetSameInstant(ZoneOffset.UTC)
.toLocalDateTime();
// converts to London timezone (wrong way)
ZonedDateTime wrongLondon = dt.atZone(ZoneId.of("Europe/London"));
System.out.println(wrongLondon); // 2017-05-31T15:00+01:00[Europe/London] (hour is 15 instead of 16)
// converts to London timezone (right way: first convert to UTC, then to London)
ZonedDateTime correctLondon = dt.atZone(ZoneOffset.UTC).withZoneSameInstant(ZoneId.of("Europe/London"));
System.out.println(correctLondon); // 2017-05-31T16:00+01:00[Europe/London]
PS: when I say "wrong way", I mean "wrong for your case". If my local time is 10h and I want to create an object that means "10h in London timezone", the localDateTime.atZone()
does the job. But in your case, 10h is a UTC time. But as the object is local, you need to convert it to UTC before converting to another timezone. That's why a LocalDateTime
is not ideal for your case.
And when converting back to LocalDateTime
, you must take care to convert to UTC before getting the local part:
// wrong: 2017-05-31T16:00
System.out.println(correctLondon.toLocalDateTime());
// correct: 2017-05-31T15:00
System.out.println(correctLondon.withZoneSameInstant(ZoneOffset.UTC).toLocalDateTime());
So, IMO, using Instant
is more straightforward as the usage of UTC is explicit and conversion to/from another timezones are easy. But if you can't change the type of your field, you can convert the LocalDateTime
to/from another type (OffsetDateTime
seems to be the best choice), taking care of timezone/offset details as explained above.