MySQL - 如何使用正确的时区存储时间? (来自Java) [英] MySQL - how to store time with correct timezone? (from Java)

查看:203
本文介绍了MySQL - 如何使用正确的时区存储时间? (来自Java)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用MySQL 5.0。我需要存储一列日期时间信息,这意味着使用 DATETIME TIMESTAMP 列类型。但是夏季冬天时间变化有问题。



我的时区是CET。夏天,我们使用夏季时间(CEST,GMT + 2),冬天我们使用冬季(CET,GMT + 1)。在夏季时间变成冬天的那一天(在今年的2012年是10月28日),我们有时间是凌晨2点两次。这一天的时间顺序是:

  ...  - > 1:00 CEST  - > 2:00 CEST  - > 2:00 CET(= 3:00 CEST) - > 3:00 CET  - > ... 

所以如果我有时间戳/ datetime'2012-10-28 02:00:00如果这个时间代表夏令时或冬季的2:00 AM,我无法正确说出来。



我有两个不同的java日期:

 日期d1 =新日期(1351382400000); // 2:00 CEST(夏令时)
日期d2 =新日期(1351386000000); // 2:00 CET(winter-time)

当我使用标准时间戳存储到数据库/ datetime格式(即'yyyy-MM-dd HH:mm:ss')他们都平等地存储为2012-10-28 02:00:00 '。但是当我从数据库中获取这些值到Date变量时,我得到两个相同的日期。所以输入日期不同但输出日期相同。

如果我使用FROM_UNIXTIME函数存储日期值,则同样会发生: FROM_UNIXTIME(1351382400) FROM_UNIXTIME(1351386000)值作为相同的值存储到数据库列(DATETIME或TIMESTAMP类型)中。所以当我将这些值转换成java的Date对象时,我再次得到两个相同的日期(在冬季)。



有没有办法,如何存储到MySQL时代的信息或者如何处理DATETIME / TIMESTAMP列中的时区信息?

当然我可以使用unix-timestamp存储到数据库BIGINT值中。但是我想知道是否有任何方式如何解决这个问题与任何



MySQL日期时间类型。



任何帮助或伎俩是赞赏...:)



非常感谢。



Honza(sporak )



编辑#1:

如果我尝试存储时间戳记值,然后将这个时间戳记值提取到java的日期,我再次收到错误的日期。假设我有简单的表与TIMESTAMP列。我以这种方式存储这个表中的数据:

  mysql> INSERT INTO`tab`(timestamp_column)VALUES 
(FROM_UNIXTIME(1351382400)),// 2:00 CEST(summer-time)
(FROM_UNIXTIME(1351386000)); // 2:00 CET(winter-time)
查询OK,2行受影响(0.00秒)
记录:2重复:0警告:0
/ pre>

当我将这些行提取到java Date对象时,我会得到两个相同的日期,显示2:00 CET。更重要的是 - 当我在MySQL中获取这些行时,我在MySQL中再次出现错误的值:

  mysql>从选项卡中选择UNIX_TIMESTAMP(timestamp_column) 
+ -------------------- +
| UNIX_TIMESTAMP(ts)|
+ -------------------- +
| 1351386000 |
| 1351386000 |
+ -------------------- +
2行集(0.00秒)

所以TIMESTAMP对我来说似乎没用。

解决方案

在我看来,最好的方法是告诉MySQL使用GMT并处理应用程序代码中的所有本地时间问题,而不是数据库。数据库中的值将始终为GMT,完全停止,这是明确的。正如你所说,夏令时(夏令时)调整后,您可以在数据库中获得与我们人类相同的两个不同时间的价值。



这也使数据库可移植。如果你转到北美,开始使用MySQL设置为(例如)中央时间,突然之间你的数据库中的值似乎已经移动了几个小时。当我把这个数据库从美国的东海岸移到西海岸时,我继承了一个使用服务器本地时间的数据库的问题,没有想过检查MySQL是否被奴隶到机器的区域...


I'm using MySQL 5.0. I need to store into one column date-time information, it means to use DATETIME or TIMESTAMP column type. But I have problem with summer-winter time change.

My timezone is CET. In summer we use summer-time (CEST, it is GMT+2) and in winter we use winter-time (CET, it is GMT+1). In the day when the summer time changes into winter time (in this year 2012 it was on 28th of October) we have time 2:00AM two-times. The sequence of time in this day is:

... -> 1:00 CEST -> 2:00 CEST -> 2:00 CET (= 3:00 CEST) -> 3:00 CET -> ...

So if I have timestamp/datetime '2012-10-28 02:00:00' I'm not able to say correctly if this time represents 2:00AM in summer time or in winter time.

I have two different java dates:

Date d1 = new Date(1351382400000); // 2:00 CEST (summer-time)
Date d2 = new Date(1351386000000); // 2:00 CET (winter-time)

and when I store them into database using standard timestamp/datetime format (ie. 'yyyy-MM-dd HH:mm:ss') both of them are store equally as '2012-10-28 02:00:00'. But when I get these values from database back into Date variables I get two same dates. So input dates were different but output dates are equal.
The same occures if I use FROM_UNIXTIME function to store date value: FROM_UNIXTIME(1351382400) and FROM_UNIXTIME(1351386000) values are stored into database column (DATETIME or TIMESTAMP type) as same values. So when I get these values into java's Date object I get two same dates again (in winter time).

Is there any way, how to store into MySQL information about timezone or how to handle with timezone information within DATETIME/TIMESTAMP columns?
Of course I can store into database BIGINT value with unix-timestamp. But I'm wondering if there's any way how to solve this problem with any

MySQL date-time type.

Any help or trick is appreciated ... :)

Thanks a lot.

Honza (sporak)

EDIT #1:
If I tried to store timestamp values and then fetch this timestamp values into java's Date, I get faulty Date again. Let's say I have simple table with TIMESTAMP column. I store data in this table this way:

mysql> INSERT INTO `tab` (timestamp_column) VALUES 
          (FROM_UNIXTIME(1351382400)),  // 2:00 CEST (summer-time)
          (FROM_UNIXTIME(1351386000));  // 2:00 CET (winter-time)
Query OK, 2 rows affected (0.00 sec)
Records: 2  Duplicates: 0  Warnings: 0

When I fetch these rows into java Date object, I'm getting two same dates that shows 2:00 CET. And what's more - when I fetch these rows in MySQL I get wrong values again in MySQL:

mysql> SELECT UNIX_TIMESTAMP(timestamp_column) from tab;
+--------------------+
| UNIX_TIMESTAMP(ts) |
+--------------------+
|         1351386000 | 
|         1351386000 | 
+--------------------+
2 rows in set (0.00 sec)

So TIMESTAMP seems to me to be little useless.

解决方案

Your best bet, in my view, is to tell MySQL to use GMT and handle all local time issues in your application code, not your database. The values in the database would always be GMT, full stop, which is unambiguous. As you say, with daylight savings time (summer time) adjustments, you can end up with the same value in your database for what is, to us humans, two different times.

This also makes the database portable. If you move to North America and start using MySQL set to (say) Central time, all of a sudden the values in your database seem to have moved several hours. I had that issue with a database I inherited which was using the server's local time, when I moved it from the east coast of the U.S. to the west coast, not having thought to check whether MySQL was slaved to the machine's zone...

这篇关于MySQL - 如何使用正确的时区存储时间? (来自Java)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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