日期在UTC在mysql [英] Date in UTC in mysql

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

问题描述

我有一个表的数据类型为DATETIME的字段。我使用java持续记录在这个表中,我计算当前日期如下。

 日期日期= Calendar.getInstance(TimeZone .getTimeZone(UTC))。getTime(); 

但是当我查看我的表格时,它再次将此时间转换为本地时区。



有些人可以向我解释,如何在数据库中的UTC时区中存储日期。



@Code



我有一个db实体Referral对应于表,日期成员数据类型为java.util.Date,并使用hibernate保存。

  Referral ref = new Referral(); 
ref.setCreationDate(Calendar.getInstance(TimeZone.getTimeZone(UTC))。getTime());
referralDAO.save(ref);

谢谢



Jitendra

解决方案

MySql DATETIME以时区不可知的方式存储。它只是存储年/月/日/小时/分钟/秒



您如何解释这些信息取决于您和您的应用程序。
如果您将其存储为UTC,那么当您检索它时,您必须确保将其解释为UTC。



您可能对此感兴趣SO问题:如何在Java或GMT中获取当前的日期和时间?



正如该问题的答案所述:


java.util.Date始终为,以UTC为单位。
什么让你觉得在本地的
时间?我怀疑问题是你通过
的日历显示
,它使用本地
时区,或者可能使用
Date.toString(),它也使用
本地时区。



I have a table which has field with data type as DATETIME. I persist a record in this table using java and I calculate current date as as following.

Date date = Calendar.getInstance(TimeZone.getTimeZone("UTC")).getTime();

But When I check my table, it again converts this time into local timezone.

Can some one please explain to me, how to store date in UTC timezone in database.

@Code

I have a db entity 'Referral' corresponding to table with date member data type as java.util.Date and using hibernate to save it.

Referral ref = new Referral();
ref.setCreationDate(Calendar.getInstance(TimeZone.getTimeZone("UTC")).getTime());
referralDAO.save(ref);

Thanks

Jitendra

解决方案

MySql DATETIME is stored in a time zone agnostic manner. It just stores year/month/day/hour/minute/second

How you interpret that information is up to you and your application. If you are storing it as UTC, then when you retrieve it you must make sure to interpret it as UTC as well.

You might be interested in this SO question: How can I get the current date and time in UTC or GMT in Java?

As mentioned in the answer to that question:

java.util.Date is always in UTC. What makes you think it's in local time? I suspect the problem is that you're displaying it via an instance of Calendar which uses the local timezone, or possibly using Date.toString() which also uses the local timezone.

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

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