Why does Spring ignore time offset of the input string when binding LocalDateTime annotated with @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME)?

Viewed 1880

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.

1 Answers
Related