JsonFormat 日期少了8个小时?还我

2023-10-19 02:38
文章标签 日期 小时 jsonformat

本文主要是介绍JsonFormat 日期少了8个小时?还我,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

JsonFormat 后日期少了8个小时什么鬼?

前言

今天测试的时候发现时间对不上,比数据库里的时间少了8个小时?测试小姐姐一顿狂轰乱炸,一点都不温柔。

什么鬼?哪里出了问题?
数据库显示的是下面👇
在这里插入图片描述
画面显示如下
在这里插入图片描述

我的数据里明明显示的是对的时间,怎么到画面显示你就少了8个小时?

快,还我8个小时。

扯远了,赶紧撸代码,找问题。

数据库里显示的是
2020-03-17 11:40:27

然而到了画面前端显示的是
2020-03-17 03:40:27

分析

    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")private Date createTime;

应该就是上面的代码出了问题,没关系,出问题慢慢解决。

看看源码


@Target({ElementType.ANNOTATION_TYPE, ElementType.FIELD, ElementType.METHOD, ElementType.PARAMETER, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@JacksonAnnotation
public @interface JsonFormat {String DEFAULT_LOCALE = "##default";String DEFAULT_TIMEZONE = "##default";String pattern() default "";JsonFormat.Shape shape() default JsonFormat.Shape.ANY;String locale() default "##default";String timezone() default "##default";JsonFormat.Feature[] with() default {};JsonFormat.Feature[] without() default {};
}

JsonFormat 是一个注解,上面的createTime,我们配置的pattern时间的格式,其他的都是默认的。

少了8个小时?是不是会是时区的问题,接着往下面看,眼前一亮呀。

    //java.util.TimeZone to use for serialization (if needed)public String timezone() default DEFAULT_TIMEZONE;

不设置时区的话,会有个默认的时区。

网上找找代码看看时区的数据

 public static void main(String[] args) {System.out.println(TimeZone.getDefault().getID());String[] ids = TimeZone.getAvailableIDs();for (String id : ids) {System.out.println(displayTimeZone(TimeZone.getTimeZone(id)));}System.out.println("\nTotal TimeZone ID " + ids.length);}private static String displayTimeZone(TimeZone tz) {long hours = TimeUnit.MILLISECONDS.toHours(tz.getRawOffset());long minutes = TimeUnit.MILLISECONDS.toMinutes(tz.getRawOffset())- TimeUnit.HOURS.toMinutes(hours);// avoid -4:-30 issueminutes = Math.abs(minutes);String result = "";if (hours > 0) {result = String.format("(GMT+%d:%02d) %s", hours, minutes, tz.getID());} else {result = String.format("(GMT%d:%02d) %s", hours, minutes, tz.getID());}return result;}

输出结果

Asia/Shanghai
(GMT-12:00) Etc/GMT+12
(GMT-11:00) Etc/GMT+11
(GMT-11:00) Pacific/Midway
(GMT-11:00) Pacific/Niue
(GMT-11:00) Pacific/Pago_Pago
(GMT-11:00) Pacific/Samoa
(GMT-11:00) US/Samoa
(GMT-10:00) America/Adak
(GMT-10:00) America/Atka
(GMT-10:00) Etc/GMT+10
(GMT-10:00) HST
(GMT-10:00) Pacific/Honolulu
(GMT-10:00) Pacific/Johnston
(GMT-10:00) Pacific/Rarotonga
(GMT-10:00) Pacific/Tahiti
(GMT-10:00) SystemV/HST10
(GMT-10:00) US/Aleutian
(GMT-10:00) US/Hawaii
(GMT-9:30) Pacific/Marquesas
省略……
Total TimeZone ID 620

我们在看看

 public TimeZone getTimeZone() {TimeZone tz = this._timezone;if (tz == null) {if (this._timezoneStr == null) {return null;}tz = TimeZone.getTimeZone(this._timezoneStr);this._timezone = tz;}return tz;}/*** Gets the <code>TimeZone</code> for the given ID.** @param ID the ID for a <code>TimeZone</code>, either an abbreviation* such as "PST", a full name such as "America/Los_Angeles", or a custom* ID such as "GMT-8:00". Note that the support of abbreviations is* for JDK 1.1.x compatibility only and full names should be used.** @return the specified <code>TimeZone</code>, or the GMT zone if the given ID* cannot be understood.*/public static synchronized TimeZone getTimeZone(String ID) {return getTimeZone(ID, true);}private static TimeZone getTimeZone(String ID, boolean fallback) {TimeZone tz = ZoneInfo.getTimeZone(ID);if (tz == null) {tz = parseCustomTimeZone(ID);if (tz == null && fallback) {tz = new ZoneInfo(GMT_ID, 0);}}return tz;}

timezone

1.1 什么是时区?
timezone,即由于世界各国家与地区经度不同,地方时也有所不同,按照经度将全球划分为24个时区。

由于世界各国家与地区经度不同,地方时也有所不同,因此会划分为不同的时区。

时区有相应的英文字母缩写,例如GMT,UTC,CST等,常见的时区。

很多细节要注意呀。

这篇关于JsonFormat 日期少了8个小时?还我的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/236724

相关文章

springboot日期格式化全局LocalDateTime详解

《springboot日期格式化全局LocalDateTime详解》文章主要分析了SpringBoot中ObjectMapper对象的序列化和反序列化过程,并具体探讨了日期格式化问题,通过分析Spri... 目录分析ObjectMapper与jsonSerializer结论自定义日期格式(全局)扩展利用配置

对postgresql日期和时间的比较

《对postgresql日期和时间的比较》文章介绍了在数据库中处理日期和时间类型时的一些注意事项,包括如何将字符串转换为日期或时间类型,以及在比较时自动转换的情况,作者建议在使用数据库时,根据具体情况... 目录PostgreSQL日期和时间比较DB里保存到时分秒,需要和年月日比较db里存储date或者ti

AI Toolkit + H100 GPU,一小时内微调最新热门文生图模型 FLUX

上个月,FLUX 席卷了互联网,这并非没有原因。他们声称优于 DALLE 3、Ideogram 和 Stable Diffusion 3 等模型,而这一点已被证明是有依据的。随着越来越多的流行图像生成工具(如 Stable Diffusion Web UI Forge 和 ComyUI)开始支持这些模型,FLUX 在 Stable Diffusion 领域的扩展将会持续下去。 自 FLU

javaScript日期相加减例子

当前时间加上2天 var d = new Date(“2015-7-31”); d.setDate(d.getDate()+2); var addTwo=d.getFullYear()+”年”+(d.getMonth()+1)+”月”+d.getDate()+”日”; “控制台输出===============”+”当前日期加2天:”+addTwo; 使用这种方法,月份也会给你计算.

linux下修改系统日期与时间

cp /usr/share/zoneinfo/Asia/Shanghai  /etc/localtime

时间日期与时间戳转换(Linux C)

本文主要学习三个知识点,第一是UTC时间、GMT时间的概念;第二是在Unix环境下UTC时间与时间戳的转换;第三是在C语言中如何修改时区。 本文参考了《UNP》以及 http://blog.csdn.net/foxir/article/details/43916601 http://blog.csdn.net/ljafl9988/article/details/16847935 一、

Java中计算两个日期间隔多少天

String dbtime1 = "2017-02-23";  //第二个日期 String dbtime2 = "2017-02-22";  //第一个日期 //算两个日期间隔多少天 SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); Date date1 = format.parse(dbtime1); Date dat

实现日期往前或往后或跳转到指定月份或天数

//月份跳转 //初始日期 String yearMonth = "201702"; String yearMonthStr = ""; //往前(负数)或往后(正数) int add = -2; SimpleDateFormat sdf = new SimpleDateFormat("yyyyMM"); Date source = sdf.parse(yearMonth); Cal

Java:日期类2

SimpleDateFormat日期格式化类 构造 SimpleDateFormat(String pattern); pattern是我们自己制定的日期格式,字母不能改变,但连接符可以改变 yyyy--MM--dd--HH 时间单位字母表示 Y年M月d日H时m分s秒 方法 String format(Date date)将Date对象按照对应格式转成String Date par

【JavaScript】版本号和日期时间的比较

JS使用 ‘>’ 运算符比较两个字符串大小时,会把字符串转换为ASCII码依次比较。 比较标准时间格式可以直接使用 ’ > ’ 比较; (2018-07-20格式)