1

We have api: call_summary/

{
  "id": 2,
  "number: "xyz",
  "call_time": "2021-10-11T03:50:23Z"
}

We have multiple users with various timezones like ADT, EDT, IST, etc. When users access this API the call_time should change according to user timezone. I tried to use @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "IST"), but this won't allow us to change the call_time dynamically. Is there any way to do it using annotations or filters?

  • 3
    There is no automatic way. You need a mechanism to get the user's timezone (e.g. using some API) and then convert the date-time to that timezone. Check [How to convert date time from one time zone to another time zone](https://stackoverflow.com/q/8238661/10819573) for the second part. – Arvind Kumar Avinash Oct 11 '21 at 12:42
  • 1
    https://www.logicbig.com/tutorials/misc/jackson/json-serialize-deserialize-converter.html may this help you – Akash Shah Oct 11 '21 at 12:46
  • 1
    I do not know the best way to do this, but, assuming you have a way to know the desired TZ for the current response, you need to register a custom serializer (Jackson's `ZonedDateTimeSerializer` might be a good start to copy or extend, see also Jackson's modules) that reads the desired TZ and uses an appropriate formatter. However, I would argue that **presenting the date to the user's own TZ is a responsibility of the presentation layer!** Good luck anyway! – Nikos Paraskevopoulos Oct 11 '21 at 13:03
  • 1
    I suggest to store the time in UTC timezone and let client change to its desired timezone. If that is not possible you’ll have to write custom serialiser to change the timezone as present in the request – Ravik Oct 11 '21 at 13:32

3 Answers3

0

I would recommend storing call_time in two columns, UTC and users local time-zone.

By doing so, it will eliminate complexity and confusion at both ends (server and client)

Umais Gillani
  • 589
  • 3
  • 9
  • This is not possible bcoz users can change their timezone anytime. And Second thing is there are some other objects with the same requirements, we can't add one additional column in all other tables and do another set of handling for it. – Jyoti Singh Oct 11 '21 at 13:21
0

Check the following link, it may help you: Pass browser timezone to the backend springboot application to generate reports with dates as per the browser timezone. According to the latter, you can use TimeZone as input to your controller. You could do something like the following:

@RestController
public class TestController {
  DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");

  @GetMapping(value = "/test")
  public String generate(TimeZone timezone) throws IOException {
    return LocalDateTime.now().atZone(timezone.toZoneId()).format(formatter);
  }
}

Alternatively, you could get the timezone from HttpServletRequest.

dchar
  • 1,575
  • 1
  • 16
  • 28
0

JSON Serialize is the best fit for this custom data conversion as per user or user role. I created the converter class and called by @JsonSerialize(converter = LocalDateTimeToStringConverter.class) and @JsonDeserialize(converter = StringToLocalDatetimeConverter.class). It worked as per my expection.

For reference, I attached the sample link below.

Custom conversion using JSON-Serialize-Deserialize