使用Java和Spring在Oracle数据库中插入UTC/GMT日期 [英] Insert UTC/GMT date in Oracle database with Java and Spring

查看:295
本文介绍了使用Java和Spring在Oracle数据库中插入UTC/GMT日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我使用jdbcTemplate插入new Date()对象到Oracle数据库时,我可以看到jdbc驱动程序或Spring jdbcTemplate使用本地JVM偏移量插入了Date.

When I insert new Date() object using jdbcTemplate to Oracle database, I can see that jdbc driver or Spring jdbcTemplate insert Date using local JVM offset.

SimpleDateFormat sdf = new SimpleDateFormat("dd-MMM-yyyy");
sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
Date timeZoneDate = sdf.parse("09-SEP-1987");

例如,当我插入在GMT中创建的Date对象时,如果JVM时区为USA,则此结果将在Oracle数据库中插入08-SEP-1987.

For example when I insert Date object created in GMT this result to inserting 08-SEP-1987 in Oracle database if JVM timezone is USA.

推荐答案

java.util.Date和Oracle Date都不存储时区信息.在您的情况下,Jdbc驱动程序使用JVM时区转换日期.您可以使用以下选项之一:

Neither java.util.Date nor Oracle Date stores timezone information. In your case Jdbc driver converts your date using the JVM timezone. You can use one of the following options:

  • 如果使用的是PreparedStatement,则可以使用setDate(int parameterIndex, Date x, Calendar cal)方法指定Calendar 在UTC时区.
  • 对于Spring jdbcTemplate,而不是插入Date对象,请在UTC时区插入Calendar
  • TimeZone.setDefault(TimeZone.getTimeZone("GMT"))可以在JVM lvl上设置
  • 在JVM启动时使用-Duser.timezone=GMT
  • If you are using PreparedStatement, you can use setDate(int parameterIndex, Date x, Calendar cal) method to specify Calendar in UTC timezone.
  • For Spring jdbcTemplate instead of inserting Date object, insert Calendar with UTC timezone
  • TimeZone.setDefault(TimeZone.getTimeZone("GMT")) could be set on JVM lvl
  • Use -Duser.timezone=GMT on JVM startup

这篇关于使用Java和Spring在Oracle数据库中插入UTC/GMT日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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