0

There is rest-controller in spring applcation

    @GetMapping
    public String findByParams(@RequestParam("date") @DateTimeFormat(pattern = DATETIME_FORMAT) ZonedDateTime date) {...}

So, ZonedDateTime does't save my +03:00, +04:00 offsets and shift all times to Z

How to customize it to save my offset in repsonse?

for example, I saved date like this

2020-05-07T17:10:45.001+03:00

but @DateTimeFormat parsed it like

2020-05-07T14:10:45.001Z

But I need in +03:00 offset with time, how to fix it. The issue is save not only for request params, but for dto fields too..

Roberto
  • 1,288
  • 5
  • 23
  • 47
  • 1
    You need to specify a pattern that maps the offset like `@DateTimeFormat(pattern = "yyyy-MM-dd'T'HH:mm:ss.SSSXXX")` – Aluan Haddad May 11 '20 at 11:26

1 Answers1

0

for example, I saved date like this

2020-05-07T17:10:45.001+03:00

but @DateTimeFormat parsed it like

2020-05-07T14:10:45.001Z

And that is perfectly valid as it describes THE SAME INSTANT but represented in different timezones. Having this you are able to present the exact same instant of time to users in different timezones in their relative (local) time while having consistent timeline of events (eg in the datebase). I would leave it as is because most of integration with your API will work out of the box. 2020-05-07T14:10:45.001Z is equal to 2020-05-07T14:10:45.001+00:00 but it is a matter of convinience to have it shorter way :)

so as to

How to remember offset when save json-dto using @DateTimeFormat?

You don't. You are free to output data in any timezone (including the one required)

Here is how to do "hardcoded version' https://stackoverflow.com/a/46653797/1527544

Antoniossss
  • 31,590
  • 6
  • 57
  • 99