Java 将当前时区设置为 @JsonFormat 时区值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/46011703/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 02:14:55  来源:igfitidea点击:

Set Current TimeZone to @JsonFormat timezone value

javajsontimezonespring-data-jpadto

提问by Vishnu K

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

Hi Guys,

嗨,大家好,

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"

从上面的 java 代码中,我想使用下面的TimeZone.getDefault().getID()将时区值设置为当前系统时区 - 它返回值作为“亚洲/加尔各答”

But if i set this code to json format

但是如果我将此代码设置为 json 格式

@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"

我收到类似“注释属性 JsonFormat.timezone 的值必须是常量表达式”之类的错误

Pls help me to solve this issue.

请帮我解决这个问题。

Thanks in advance, Vishnu

提前致谢,毗湿奴

回答by Raju Sharma

You cannot assign timezonevalue a dynamic or a runtime value. It should be constant or a compile time value and enumstoo accepted.

您不能为时值分配动态值或运行时值。它应该是常量或编译时值,并且枚举也被接受。

So you should assign a constant to timezone. like below.

所以你应该为timezone分配一个常量。像下面。

private static final String MY_TIME_ZONE="Asia/Kolkata";
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "dd-MM-yyyy", timezone = MY_TIME_ZONE);

回答by 0__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 )

回答by xonya

You can use JsonFormat.DEFAULT_TIMEZONE, after properly configuring the ObjectMapper:

您可以使用JsonFormat.DEFAULT_TIMEZONE经过正确配置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 Hymanson databindings default, usually UTC, but may be changed on ObjectMapper.

指示应使用默认 TimeZone(来自反序列化或序列化上下文)的值:注释未定义要使用的值。

注意:这里的默认值并不意味着 JVM 默认值,而是 Hymanson 数据绑定默认值,通常是 UTC,但可能会在 ObjectMapper 上更改。

In order to configure the ObjectMapper:

为了配置ObjectMapper

@Configuration
public class MyApp {

    @Autowired
    public void configureHymanson(ObjectMapper objectMapper) {
        objectMapper.setTimeZone(TimeZone.getDefault());
    }
}

To set the default TimeZone on your application use this JVM property:

要在您的应用程序上设置默认时区,请使用此 JVM 属性:

-Duser.timezone=Asia/Kolkata