FasterXML / jackson-modules-java8

Set of support modules for Java 8 datatypes (Optionals, date/time) and features (parameter names)
Apache License 2.0
401 stars 117 forks source link

LocalDateTimeDeserializer should use SystemDefault timezone #209

Closed languanghao closed 2 years ago

languanghao commented 3 years ago
 if (_formatter == DEFAULT_FORMATTER) {
                    // JavaScript by default includes time and zone in JSON serialized Dates (UTC/ISO instant format).
                    if (string.length() > 10 && string.charAt(10) == 'T') {
                       if (string.endsWith("Z")) {
                           return LocalDateTime.ofInstant(Instant.parse(string), ZoneOffset.UTC);
                       } ...

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?

cowtowncoder commented 3 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.

languanghao commented 3 years ago

@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.

cowtowncoder commented 3 years ago

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.

cowtowncoder commented 3 years ago

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.

eatgrass commented 2 years ago

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

cowtowncoder commented 2 years ago

I don't think the request makes sense, closing.