Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

specifying only timezone in @JsonFormat doesn't serialize ZonedDateTime in specified timezone. #180

Closed
shankuljain opened this issue Jul 17, 2020 · 5 comments
Labels
json-format Missing handling of `@JsonFormat` property annotation

Comments

@shankuljain
Copy link

For a ZonedDateTime field with timezone(Say ET), annotating this field with @JsonFormat(timezone="UTC") doesn't serialize it in the specified format. If I include the pattern in JsonFormat along with timezone it serializes correctly.

While just specifying timezone while using old Date (from java.util.Date), it serializes in specified timezone. (without specifying pattern)

Jackson versions -
jackson-datatype-jsr : 2.11.0
Jackson Core : 2.11.0
Jackson databind: 2.11.0

Reproducer:

import java.time.ZonedDateTime;

package com.example.demo;

import com.fasterxml.jackson.annotation.JsonFormat;
import java.util.Date;

import java.time.ZonedDateTime;

public class POJO {


    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss.SSSZ", timezone = "UTC")
    private ZonedDateTime zonedDateTimeWithPatternAndTimeZoneInJsonFormat;

    @JsonFormat(timezone = "UTC")
    private ZonedDateTime zonedDateTimeWithTimeZoneInJsonFormat;

    private ZonedDateTime zonedDateTime;


    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss.SSSZ", timezone = "UTC")
    private Date oldDateWithTimeZoneAndPatternInJsonFormat;

    @JsonFormat(timezone = "UTC")
    private Date oldDateWithTimeZoneInJsonFormat;

    private Date oldDate;

    //  getters and setters below


}
package com.example.demo;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;

import java.time.*;
import java.util.TimeZone;

package com.example.demo;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

public class DemoApplication {

    public static void main(String[] args) throws JsonProcessingException {

        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.registerModule(new JavaTimeModule());
        objectMapper.setTimeZone(TimeZone.getDefault());
        objectMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);

        LocalDateTime ldt = LocalDateTime.of(2020, Month.JUNE.getValue(), 12, 15, 0 , 0);
        ZonedDateTime zdt = ZonedDateTime.of(ldt, ZoneId.of("America/New_York"));
        Date oldDate = Date.from(zdt.toInstant());

        System.out.println("old Date with System's time zone" + oldDate);

        POJO pojo = new POJO();
        pojo.setOldDate(oldDate);
        pojo.setOldDateWithTimeZoneAndPatternInJsonFormat(oldDate);
        pojo.setOldDateWithTimeZoneInJsonFormat(oldDate);

        pojo.setZonedDateTimeWithPatternAndTimeZoneInJsonFormat(zdt);
        pojo.setZonedDateTimeWithTimeZoneInJsonFormat(zdt);
        pojo.setZonedDateTime(zdt);

        System.out.println(objectMapper.writeValueAsString(pojo));

    }


}

Output:

old Date with System's time zone Sat Jun 13 00:30:00 IST 2020

{
    "zonedDateTimeWithPatternAndTimeZoneInJsonFormat": "2020-06-12 19:00:00.000+0000",
    "zonedDateTimeWithTimeZoneInJsonFormat": "2020-06-12T15:00:00-04:00",
    "zonedDateTime": "2020-06-12T15:00:00-04:00",
    "oldDateWithTimeZoneAndPatternInJsonFormat": "2020-06-12 19:00:00.000+0000",
    "oldDateWithTimeZoneInJsonFormat": "2020-06-12T19:00:00.000+00:00",
    "oldDate": "2020-06-13T00:30:00.000+05:30"
}

As you can see in output, for oldDate only specifying timezone serializes in UTC, but not for ZonedDateTime.

@cowtowncoder
Copy link
Member

Needs to go to Java8 modules repo where support for Java 8 date/time is added, will transfer.

@cowtowncoder cowtowncoder transferred this issue from FasterXML/jackson-databind Jul 18, 2020
@cowtowncoder
Copy link
Member

Thank you for reporting this; sounds like incomplete handling of @JsonFormat information.

@kupci
Copy link
Member

kupci commented Jul 18, 2020

Looks like the same as #175, which was originally for OffsetDateTime, but appeared to impact other date types as well.

@cowtowncoder
Copy link
Member

@kupci Thanks, that makes sense.

@cowtowncoder cowtowncoder added the good first issue Issue that seems easy to resolve and is likely a good candidate for contributors new to project label Sep 19, 2020
@cowtowncoder cowtowncoder added hacktoberfest Issue related to Hactoberfest2020 activities, eligible for additional rewards json-format Missing handling of `@JsonFormat` property annotation labels Sep 30, 2020
@cowtowncoder cowtowncoder removed good first issue Issue that seems easy to resolve and is likely a good candidate for contributors new to project hacktoberfest Issue related to Hactoberfest2020 activities, eligible for additional rewards labels Oct 16, 2020
@cowtowncoder
Copy link
Member

I think this is effectively duplicate of #175, closing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
json-format Missing handling of `@JsonFormat` property annotation
Projects
None yet
Development

No branches or pull requests

3 participants