10
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "dd-MM-yyyy", timezone = "Asia/Kolkata")
private Date activationDate;

From the above java code, I want to set timezone value as Current System timezone using below: TimeZone.getDefault().getID() - it returns value as "Asia/Kolkata"

But if i set this code to json format

@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "dd-MM-yyyy", timezone = TimeZone.getDefault().getID())

I am getting error like "The value for annotation attribute JsonFormat.timezone must be a constant expression"

Pls help me to solve this issue.

Thanks in advance, Vishnu

peterh
  • 11,875
  • 18
  • 85
  • 108
  • I think you must define a deserializer for this task, but haven't tried yet. I am in such situation, too. If you have something, please attach your solution below, thanks. – WesternGun Nov 07 '18 at 13:37
  • Did you get any solution for this? If yes then please update – Vikas Jan 05 '19 at 13:10

3 Answers3

15

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:

@Configuration
public 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
Community
  • 1
  • 1
xonya
  • 2,146
  • 29
  • 37
  • 4
    Not entirely correct. See https://stackoverflow.com/a/55225726/10513675 – 8t12c7081 Mar 18 '19 at 16:45
  • 1
    Looks like javadoc a little bit misleading https://github.com/FasterXML/jackson-databind/issues/1266 – simar Oct 16 '19 at 11:16
  • Hi guys, thanks for pointing out the inaccuracies, I updated the answer. Jackson updated their Javadocs too. – xonya Jan 21 '20 at 15:46
7

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);
Raju Sharma
  • 2,496
  • 3
  • 23
  • 41
-1

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 )