Java SQL日期关闭1天 [英] Java SQL date is off by 1 day

查看:96
本文介绍了Java SQL日期关闭1天的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用与我位于同一时区的MySQL服务器。我正在尝试使用以下代码在类型为 DATE 的列上将java.util.Date插入数据库:

I'm using a MySQL server which is located in the same timezone as me. Im trying to insert a java.util.Date into the database on a column of type DATE with the following code:

SimpleDateFormat dateFormat = new SimpleDateFormat( yyyy-MM-dd);
dateFormat.setTimeZone(TimeZone.getTimeZone( GMT + 2));
日期date = dateFormat.parse(map.value( date));

插入日期的位置。问题在于,在数据库中查找时,日期已过了一天。示例:我插入了2020-05-02,数据库显示为2020-05-01。这就产生了一个问题,当我想获取日期为2020-05-02的比赛时,我必须提前1天进行搜索,而这是我不想做的。
这也发生在一天中的特定时间之间,所以我假设它是一个时区问题,但我不知道如何解决!

Where "date" is inserted. The problem is that the date is off by one day when looking in the database. Example: I inserted 2020-05-02, the database shows 2020-05-01. This gives the problem that when I want to get the matches with date 2020-05-02, I have to search for 1 day earlier which I dont want to do. Also this happens between specific times on the day so im assuming its a timezone problem but I dont know how to fix it!

提示?

推荐答案

此问题来自Java中的MySQL连接器。 8.0.19版使用JVM时区转换日期,这会导致1天休假问题。 8.0.20补丁中已解决此问题。在此处阅读 https://dev.mysql.com/doc/relnotes /connector-j/8.0/en/

This issue is coming from the MySQL Connector in java. Version 8.0.19 converts the date using the JVMs timezone which results in the 1 day off problem. This is resolved in the 8.0.20 patch. Read here https://dev.mysql.com/doc/relnotes/connector-j/8.0/en/

这篇关于Java SQL日期关闭1天的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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