JDBC时间戳和日期格林尼治标准时间(GMT)问题 [英] JDBC Timestamp & Date GMT issues

查看:193
本文介绍了JDBC时间戳和日期格林尼治标准时间(GMT)问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个JDBC Date列,如果我使用getDate仅在 2009年10月2日上获得了' date '部分,但是如果我使用getTimestamp则获得了完整的' date " 2009年10月2日13:56:78:890 .这正是我想要的.

I have a JDBC Date column, which if a i use getDate is get the 'date' part only 02 Oct 2009 but if i use getTimestamp i get the full 'date' 02 Oct 2009 13:56:78:890. This is excatly what i want.

但是,getTimestamp返回的日期"会忽略" GMT值(假设日期); 2009年10月2日13:56:78:890 ,我最终得到了 2009年10月2日15:56:78:890

However the 'date' returned by getTimestamp 'ignores' the GMT values, suppose date; 02 Oct 2009 13:56:78:890, i end up getting 02 Oct 2009 15:56:78:890

我的日期在数据库中另存为+ 2GMT日期,但应用程序服务器位于GMT上,即晚2小时

My date was saved as a +2GMT date on the database but the application server is on GMT i.e 2hrs behind

如何仍然可以按时获得我的日期, 2009年10月2日13:56:78:890

How can still get my date as is, 02 Oct 2009 13:56:78:890

编辑

我在格林尼治标准时间+2的客户端获得日期+2

I get the date +2 on the client side that is on GMT +2

推荐答案

这是Timestamp与MySQL中其他时间类型之间的区别.时间戳记以UTC格式保存为Unix time_t,但其他类型则存储日期/时间,而实际上没有区域信息.

That's the difference between Timestamp and other temporal types in MySQL. Timestamp is saved as Unix time_t in UTC but other types store date/time literally without zone information.

调用getTimestamp()时,如果类型为timestamp,MySQL JDBC驱动程序会将时间从GMT转换为默认时区.对于其他类型,它不执行这种转换.

When you call getTimestamp(), MySQL JDBC driver converts the time from GMT into default timezone if the type is timestamp. It performs no such conversion for other types.

您可以更改列类型,也可以自己进行转换.我建议采用前一种方法.

You can either change the column type or do the conversion yourself. I recommend former approach.

这篇关于JDBC时间戳和日期格林尼治标准时间(GMT)问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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