首页 问答 如何设置系统时区作为 @JsonFormat timezone
问题详情

各位大佬:

从数据库查到的时间使用jackson的@JsonFormat进行格式化:
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
private Date beginTime;

由于GMT+8这种常量不利于国际化,所以我想使用系统时区作为timezone。

我的方法:
1 使用默认时区
@JsonFormat(shape = JsonFormat.Shape.STRING,pattern = "HH:mm",timezone = JsonFormat.DEFAULT_TIMEZONE)
private Date timeQuantumBegin;

2 配置objectMapper,设置系统时区
@Autowired
public void configureJackson(ObjectMapper objectMapper) {
objectMapper.setTimeZone(TimeZone.getDefault());
}

但是效果还是达不到,差了八个小时。请问是我哪里做的不对吗?

回答

已经解决,参考:
https://cloud.tencent.com/developer/article/1572118

根本原因是引用的jar包中配置了一个objectMapper.
导致我的代码失效。

版权:言论仅代表个人观点,不代表官方立场。转载请注明出处:https://www.stntk.com/question/224.html

发表评论
暂无评论

还没有评论呢,快来抢沙发~

点击联系客服

在线时间:8:00-16:00

客服QQ

70068002

客服电话

400-888-8888

客服邮箱

70068002@qq.com

扫描二维码

关注微信公众号

扫描二维码

手机访问本站