2

I've faced the problem of receiving of date from front-end. I send date from front-end in the format "2020-01-03T00:00:00+02:00" to ZonedDateTime object in back-end. But instead of 2020-01-03T00:00Z[UTC], I receive 2020-01-02T22:00Z[UTC] (actualy minus 2 hours).

Question:

Is there any way to make ZonedDateTime lib not to convert to UTC, or not to minus timezone? Maybe any annotation?

Controller and object simple code example

Object that came from front-end:

@DIfferentsLombokAnnotations
public class Filters {
  private ZonedDateTime startDate;

  //Other fields
}

Controller:

@PostMapping("/ggg")
public List<ResponceObject> method(@RequestBody Filters filters) {
  //any code
 }
Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
Andrew
  • 39
  • 9
  • You have to format the date in ISO. take a look here : https://stackoverflow.com/questions/948532/how-do-you-convert-a-javascript-date-to-utc – Tiago Pereira Mar 20 '20 at 19:59
  • Thanks, but actually problem with back-end – Andrew Mar 20 '20 at 20:19
  • You are getting the correct point in time (only expressed differently, converted to UTC). No two hours have been subtracted. Maybe you want to pass a `LocalDate` instead. It’s a date without time zone or offset and without time of day, for example `2020-01-03`, so there is no way that the time could be wrong. – Ole V.V. Mar 20 '20 at 20:33
  • 1
    Is there a way to pass LocalDate using Moment .js lib? – Andrew Mar 20 '20 at 20:40
  • And how I can convert It to ZDT to this value: 2020-01-03T00:00Z[UTC]? The main problem that I need only ZDT objecct – Andrew Mar 20 '20 at 21:03
  • Actually I can't deserialize from "2020-01-03T00:00:00+02:00" this format on the frontend to LDT. – Andrew Mar 20 '20 at 21:11
  • If you get a `LocalDate` through, you may convert using `yourLocalDate.atStartOfDay(ZoneOffset.UTC)`. – Ole V.V. Mar 21 '20 at 05:56

1 Answers1

0

ZonedDateTime only support UTC or UTC+2 which would explain why you're getting the result you have. One option would be to instead of sending a ZonedDateTime send a LocalDate, and LocalTime, and a ZoneId, and a ZoneOffset, then parsing to a ZonedDateTime on the backend. At the end of the day, a ZonedDateTime is already composed of these components.

Jason
  • 5,154
  • 2
  • 12
  • 22
  • Understand, but, unfortunately I can't change a front-end request because of a specific architecture. But thanks for answer. – Andrew Mar 20 '20 at 20:14
  • Darn, okay, well if you always determine the offset you can just offset the ZonedDateTime by some offset. That might be a valid solution (sort of). – Jason Mar 20 '20 at 20:15
  • Of course, I have already realized this in this way... I took the timezone, convert it to sec, and used plusSeconds method to compensate lost hours. I just thought that there are the better way to do this, using spring or other fitches – Andrew Mar 20 '20 at 20:26