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 issue
minutes = 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(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
private Date createTime;

千万别忘记加时区timezone = "GMT+8"

真的是开发的bug无处不在,看来开发的时候还得多小心。不小心就被测试妹子搔扰了。

JsonFormat 日期少了8个小时?还我的更多相关文章

  1. 解决JsonFormat日期少一天问题

    使用Jackson的@JsonFormat注解时出现少一天 比如数据库存的日期是2015-01-05,转成json则变成了2015-01-04 解决办法: @JsonFormat(pattern=&q ...

  2. js字符串转日期,js字符串解析成日期,js日期解析, Date.parse小时是8点,Date.parse时间多了8小时

    js字符串转日期,js字符串解析成日期,js日期解析, Date.parse小时是8点,Date.parse时间多了8小时 >>>>>>>>>&g ...

  3. SimpleDateFormat 格式化时间少了12个小时

    SimpleDateFormat 格式化时间少了12个小时 标签(空格分隔): java SimpleDateFormat H:一天中的小时数:0-23 h: am/pm 小时数: 1-12 publ ...

  4. Java获取系统时间少了八个小时

    Java获取系统时间少了八个小时 今天忽然遇到需要获取当前时间的问题,我向来谨慎,先测试获取到的系统时间是否正确,结果竟然发现少了八个小时,晕死了,记得之前在页面用javascript获取过当前时间, ...

  5. 使用Jackson的@JsonFormat注解时出现少了 8 个小时

    比如数据库存的日期是2018-01-05,转成json则变成了2018-01-04 解决办法: @JsonFormat(pattern="yyyy-MM-dd") public D ...

  6. springMvc 注解@JsonFormat 日期格式化

    1:一定要加入依赖,否则不生效: <!--日期格式化依赖--> <dependency> <groupId>com.fasterxml.jackson.core&l ...

  7. java web系统中时间比sql server中的日期少2天的解决办法

    系统环境 jdk:1.7 数据库:sql server 2008 问题描述 升级1.7之后查询出来的日期就比数据库中的少2天,降回1.6版本的jdk就正常了. 问题原因及解决办法 国内网站有很多不靠谱 ...

  8. @JsonFormat 日期格式自动格式化

    通常日期格式都是以时间戳的形式存放在数据库里,当前端页面通过接口查询时,我们会将一个对象的某些属性查出来返回给页面. 例如,某个类里面有个属性 Timestamp create_time 给这个对象实 ...

  9. mysql 中两个日期相减获得 天 小时 分钟 或者 小时:分钟的格式

    /**有一个需求,要求获得两个日期想减的天数,小时数,分钟数.通过查找资料,于是乎我写出了如下代码,来获得两个字段.*/ IFNULL(CONCAT( ,'-',''), ),),'天')), ),) ...

随机推荐

  1. 网页title滚动

    );        var leftstar=title.substring (1,title.length );        document.title =leftstar +firstch ; ...

  2. hadoop-2.5.2 源码学习1

  3. 多版本firefox共存

    https://blog.csdn.net/gavinxlf/article/details/53173034 https://blog.csdn.net/weixin_33979745/articl ...

  4. 新特DEV1光速发布背后:原来“开公司”也能风驰电掣

    去年12月16日,总融资额达到200亿元的蔚来汽车正式推出电动SUV车型ES8:去年12月22日,威马汽车宣布最新一轮融资,累计获得120亿元的融资额:今年1月29日,小鹏汽车宣布启动总额为22亿元人 ...

  5. JAVA的堆栈和内存、垃圾回收解说

    1.有关java健壮性特点的真相 很多书上都说java健壮性的特点是因为java使用数组代替了c++的指针:c++最令人头痛的问题就是内存问题,java的健壮性使编程人员不用再考虑内存的问题:这种观点 ...

  6. CF-1102E-Monotonic Renumeration

    比较可惜昨天比赛的时候时间不够了,在比赛结束之后五分钟找出了bug提交通过了.然并软: 首先这题说b数组的后一项要么等于前一项,要么等于前一项加一,而且如果a[i] == a[j] ,那么b[i] = ...

  7. 最长递增子序列-dp问题

    Longest Increasing Subsequence The longest increasing subsequence problem is to find a subsequence o ...

  8. 吴裕雄--天生自然python编程:turtle模块绘图(1)

    Turtle库是Python语言中一个很流行的绘制图像的函数库,想象一个小乌龟,在一个横轴为x.纵轴为y的坐标系原点,(0,0)位置开始,它根据一组函数指令的控制,在这个平面坐标系中移动,从而在它爬行 ...

  9. R的基础数据结构

  10. Mybatis--映射器注解

    因为最初设计时,MyBatis 是一个 XML 驱动的框架.配置信息是基于 XML 的,而且 映射语句也是定义在 XML 中的.而到了 MyBatis 3,有新的可用的选择了.MyBatis 3 构建 ...