Set Current TimeZone to @JsonFormat timezone value
You can use JsonFormat.DEFAULT_TIMEZONE
, after properly configuring the ObjectMapper:
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "dd-MM-yyyy", timezone = JsonFormat.DEFAULT_TIMEZONE)
From the docs:
Value that indicates that default TimeZone (from deserialization or serialization context) should be used: annotation does not define value to use.
NOTE: default here does NOT mean JVM defaults but Jackson databindings default, usually UTC, but may be changed on ObjectMapper.
In order to configure the ObjectMapper
:
@Configurationpublic class MyApp { @Autowired public void configureJackson(ObjectMapper objectMapper) { objectMapper.setTimeZone(TimeZone.getDefault()); }}
To set the default TimeZone on your application use this JVM property:
-Duser.timezone=Asia/Kolkata
You cannot assign timezone value a dynamic or a runtime value. It should be constant or a compile time value and enums too accepted.
So you should assign a constant to timezone. like below.
private static final String MY_TIME_ZONE="Asia/Kolkata";@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "dd-MM-yyyy", timezone = MY_TIME_ZONE);
You can use enumeration in order to possibly enrich you time zones that you would use. A solution using enumeration is the following enumeration class implementation.
package <your package goes here>; import java.util.TimeZone; public enum TimeZoneEnum { DEFAULT(TimeZone.getDefault()), ASIA_KOLKATA = (TimeZone.getTimeZone("Africa/Abidjan")), //other timezones you maybe need ... private final TimeZone tz; private TimeZoneEnum(final TimeZone tz) { this.tz = tz; } public final TimeZone getTimeZone() { return tz; } }
Then you can utilize you enumeration like below:
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "dd-MM-yyyy", timezone = TimeZoneEnum.ASIA_KOLKATA )