Closed languanghao closed 2 years ago
"Local" in there indicates that there is no timezone at all -- these are abstract values that are not tied to any timezone. Values like "ZonedDateTime" (and "OffsetDateTime") are specifically designed to be used when timezone is relevant.
So I don't quite understand what is being asked here.
Code above adds a work-around to strip erroneous timezone indicator for compatibility reasons but in general values really should not pretend there are timezones if dealing with non-timezone (local) date/time values.
@cowtowncoder My time zone is +8, JSON.stringify()
always change the time to UTC. When user pick a date 2021-03-26 08:00:00
then server receive a date 2021-03-26T00:00:00Z
. In this case, if the java type is LocalDateTime
, it lost 8 hours.
I think the LocalDateTime
name misleads me, LocalDateTime.now()
return a time in my timezone instead of UTC
Finally I use ZonedDateTime
instead of LocalDateTime
, then everything goes fine.
Can we provide an option to set special the timezone when deserialize the LocalDateTime
? I know the Local
in there indicates that there is no timezone at all, but JSON.stringify()
and LocalDateTime
really make some troubles.
I think you will need to write a custom deserializer for this use case: I don't think it makes sense to add support for handling invalid data like this unfortunately: if String ends in Z
, that is +0 timezone, it cannot be anything else.
Or put another way: LocalDateTimeDeserializer
does not use any timezone. Code to remove Z
does this:
return LocalDateTime.parse(string.substring(0, string.length()-1),
_formatter);
and so you can non-zone date/time value. It is not in UTC or local time zone; it is abstract.
But you can associate it with any timezone you need to, to create ZonedDateTime or OffsetDateTime.
Same question
var date = new Date() // Wed Dec 08 2021 12:39:00 GMT+0800
date.toISOString() // '2021-12-08T04:39:00.312Z'
in java
LocalDateTime date; // deserialized
date.toString(); // 2021-12-08T04:39:00.312
It would be great if the timezone is customizable, so the deserializer could work with the given zone
I don't think the request makes sense, closing.
When deserializer a json date to LocalDateTime, we always want to get a date with default time zone or custom timezone. Is there a way to special the timezone?