2

I have an object Activity with the field:

@DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME)
private LocalDateTime start;

I'm binding this object to controller method by sending the form:

@RequestMapping(value = "update", method = RequestMethod.POST)
public String submitUpdateActivityForm(Activity activity) {
      activityRepository.save(activity);
      return "successPage;
}

I'm using Spring Boot 1.5.1, Spring MVC 4.3.6 and in my web application I want to receive time from clients in any time zone but keep LocalDateTime always in UTC. But when Spring binds object from the form to request parameter in controller it completely ignores time offset of the input string for property of type LocalDateTime.

I thought according to documentation of @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) Spring will find the offset in string and convert the input date-time from the given time zone to the time zone of my server (which is UTC) and then bind.

For example: I want string 2017-05-31T12:00-03:00 to be converted to LocalDateTime '2017-05-31T15:00' and 2017-05-31T12:00Z to be interpreted without changes as LocalDateTime '2017-05-31T12:00'. Unfortunately I always get LocalDateTime regardless of the time offset.

Is it right behaviour for @DateTimeFormat or am I doing something wrong? Should I implement Spring Converter or extend PropertyEditorSupport?

And is it right approach to dealing with time? I want to accept any kind of dates, but keep them in UTC in LocalDateTime because then I will be able to easily send them to client side where I can convert them from UTC to client's local time zone and display.

0

1 Answer 1

5

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.