将当前TimeZone设置为@JsonFormat时区值 [英] Set Current TimeZone to @JsonFormat timezone value

查看:2903
本文介绍了将当前TimeZone设置为@JsonFormat时区值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

伙计们,

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

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"

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

But if i set this code to json format

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

我遇到类似的错误注释属性JsonFormat.timezone的值必须为常量表达式"

请帮我解决这个问题.

预先感谢, 毗湿奴

推荐答案

您可以使用JsonFormat.DEFAULT_TIMEZONE:

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

从文档中

DEFAULT_TIMEZONE的特殊值可用于表示仅使用默认值",其中默认值由序列化上下文指定,而该序列化上下文又默认为系统默认值(TimeZone.getDefault()),除非明确设置为其他语言环境. /p>

Special value of DEFAULT_TIMEZONE can be used to mean "just use the default", where default is specified by the serialization context, which in turn defaults to system defaults (TimeZone.getDefault()) unless explicitly set to another locale.

这篇关于将当前TimeZone设置为@JsonFormat时区值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆