0

Is there way to initialize dynamic timezone while json output for all date formatted values, I have location id input by which I determine the timezone so each output has the location id so can I use this here as below.

@Configuration
public class TimeZoneConfig {
        
    @Value("${ovitag.timezone}")
    private String timezone;
    
    @Bean
    public Jackson2ObjectMapperBuilderCustomizer jacksonObjectMapperCustomization() {
        return jacksonObjectMapperBuilder -> jacksonObjectMapperBuilder.timeZone(TimeZone.getTimeZone("Asia/Kolkata"));
    }
}

The above code only change to a particular timezone is it possible here to call the json response here and get that locationId and transform the timezone values dynamically

Json response 

{
  "VisitDate": "1991-02-19 12:23:56",
  "firstName": "dharnisha",
  "lastName": "K",
  "mainIdentifier": "uhidd90011",
  "mobileNumber": "9300921111",
  "packageId": "2020"
  "locationId":"1234"
}

Timezone is dynamic based on loc id as obtained from below

String timZone = configFileDao.getConfigTimezoneByLocationId(locationId);
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • If I understand what you're saying, you have a POJO with a `String locationId` field that names a time zone, and you want all the date/time fields of that POJO (or nested POJOs) to output the date/time values in that given time zone when serializing to JSON. Did I understand that correctly? --- If so, show us an example of the POJO, and an example of what you want the JSON to be. What are the types of the date/time fields? `ZonedDateTime`? `OffsetDateTime`? `LocalDateTime`? `Instant`? – Andreas Dec 28 '20 at 14:06
  • is it possible to Customize this inbulid Jackson2ObjectMapperBuilderCustomizer interface – Ameenur Rahman Dec 28 '20 at 14:25
  • What is the type of `VisitDate` in your POJO? – Andreas Dec 28 '20 at 14:30
  • the above code not only change a particular response date time zone but every output from the entire project – Ameenur Rahman Dec 28 '20 at 14:32
  • Date visitDate; – Ameenur Rahman Dec 28 '20 at 14:33
  • Since your program will likely serialize many different types of POJOs, the global serializer should be general, with no specific knowledge of data structure, so it shouldn't have knowledge of a `locationId` field. Such behavior should be specified in custom serializers attached to the POJO with a [`@JsonSerialize`](https://fasterxml.github.io/jackson-databind/javadoc/2.9/com/fasterxml/jackson/databind/annotation/JsonSerialize.html) annotation. – Andreas Dec 28 '20 at 14:34
  • the idea is i get date values as utc from Database so i convert here with a condition which make this effective in all json response – Ameenur Rahman Dec 28 '20 at 14:34
  • But not ALL json responses will have a `locationId` field, and they may not all be in the root object of the response. – Andreas Dec 28 '20 at 14:36
  • no problem i make the timzone come from application properties if null – Ameenur Rahman Dec 28 '20 at 14:38
  • If you want to change the date according to `locationId`, I think you need to do the `JsonDeserialize` of the each object. See also for different solutions: https://stackoverflow.com/a/46653797/2039546 – İsmail Y. Dec 29 '20 at 13:15

0 Answers0