- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我必须将 UTC 日期时间存储在数据库中。
我已将特定时区中给出的日期时间转换为 UTC。为此,我遵循了以下代码。
我输入的日期时间是“20121225 10:00:00 Z”时区是“亚洲/加尔各答”
我的服务器/数据库(oracle)在同一时区(IST)“亚洲/加尔各答”运行
获取该特定时区的日期对象
String date = "20121225 10:00:00 Z";
String timeZoneId = "Asia/Calcutta";
TimeZone timeZone = TimeZone.getTimeZone(timeZoneId);
DateFormat dateFormatLocal = new SimpleDateFormat("yyyyMMdd HH:mm:ss z");
//This date object is given time and given timezone
java.util.Date parsedDate = dateFormatLocal.parse(date + " "
+ timeZone.getDisplayName(false, TimeZone.SHORT));
if (timeZone.inDaylightTime(parsedDate)) {
// We need to re-parse because we don't know if the date
// is DST until it is parsed...
parsedDate = dateFormatLocal.parse(date + " "
+ timeZone.getDisplayName(true, TimeZone.SHORT));
}
//assigning to the java.sql.TimeStamp instace variable
obj.setTsSchedStartTime(new java.sql.Timestamp(parsedDate.getTime()));
存储到数据库
if (tsSchedStartTime != null) {
stmt.setTimestamp(11, tsSchedStartTime);
} else {
stmt.setNull(11, java.sql.Types.DATE);
}
输出
数据库(oracle)已存储相同的给定日期时间:“20121225 10:00:00
,而不是UTC。
我已经从下面的sql中确认了。
select to_char(sched_start_time, 'yyyy/mm/dd hh24:mi:ss') from myTable
我的数据库服务器也在同一时区“亚洲/加尔各答”上运行
它给了我以下外观
Date.getTime()
不是 UTC 还有一个问题:
timeStamp.toString()
会像 java.util.date
那样以本地时区打印吗?不是世界标准时间?
最佳答案
虽然没有明确指定 setTimestamp(int parameterIndex, Timestamp x)
驱动程序必须遵循 setTimestamp(int parameterIndex, Timestamp x, Calendar cal)
javadoc 建立的规则:
Sets the designated parameter to the given
java.sql.Timestamp
value, using the givenCalendar
object. The driver uses theCalendar
object to construct an SQLTIMESTAMP
value, which the driver then sends to the database. With aCalendar
object, the driver can calculate the timestamp taking into account a custom time zone. If noCalendar
object is specified, the driver uses the default time zone, which is that of the virtual machine running the application.
当您使用 setTimestamp(intparameterIndex, Timestamp x)
调用时,JDBC 驱动程序使用虚拟机的时区来计算该时区中时间戳的日期和时间。该日期和时间是存储在数据库中的内容,如果数据库列不存储时区信息,则有关该区域的任何信息都会丢失(这意味着使用该数据库的应用程序将使用该日期和时间)一致地使用相同的时区,或者提出另一种方案来识别时区(即存储在单独的列中)。
例如:您的本地时区是 GMT+2。您存储“2012-12-25 10:00:00 UTC”。数据库中存储的实际值为“2012-12-25 12:00:00”。您再次检索它:您再次检索它为“2012-12-25 10:00:00 UTC”(但前提是您使用 getTimestamp(..)
检索它),但当另一个应用程序时访问GMT+0时区的数据库,它将检索时间戳为“2012-12-25 12:00:00 UTC”。
如果您想将其存储在不同的时区,则需要将 setTimestamp(intparameterIndex, Timestamp x, Calendar cal)
与所需时区的 Calendar 实例结合使用。只需确保在检索值时也使用具有相同时区的等效 getter(如果您使用数据库中没有时区信息的 TIMESTAMP
)。
因此,假设您想存储实际的 GMT 时区,您需要使用:
Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
stmt.setTimestamp(11, tsSchedStartTime, cal);
对于 JDBC 4.2,兼容的驱动程序应支持 java.time.LocalDateTime
(和 java.time.LocalTime
)的 TIMESTAMP
(以及 TIME
) 通过 get/set/updateObject
。 java.time.Local*
类没有时区,因此不需要应用转换(尽管如果您的代码确实假设了特定时区,这可能会带来一系列新问题)。
即:
getDate(..)
替换为 getObject(.., LocalDate.class)
setDate(.., dateValue)
替换为 setObject(.., localDateValue)
getTime(..)
替换为 getObject(.., LocalTime.class)
setTime(.., timeValue)
替换为 setObject(.., localTimeValue)
getTimestamp(..)
替换为 getObject(.., LocalDateTime.class)
setTimestamp(.., timestampValue)
替换为 setObject(.., localDateTimeValue)
关于java - java.sql.Timestamp 时区是否特定?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62296586/
在我的 Rails (3.0) 应用程序中,我使用助手 time_zone_select() 来使用时区。它生成时区名称,如“(GMT+01:00) Paris”... 但这些名称与 MySQL 中的
我正在解析格式为的日期: SimpleDateFormat formatter = new SimpleDateFormat("EE MMM dd HH:mm:ss Z yyyy"); 当我这样做的时
我在 Android 上使用 ACTION_TIMEZONE_CHANGED Intent 过滤器来响应时区更改。 我注意到 Jodas 当前时区此时没有更新,使用:DateTimeZone.getD
我有一个旧版 Windows 应用程序,它从数据库读取数据。其中一列是“TimeZoneInfoId”。在传统世界中,它是由另一个 Windows 应用程序编写的,因此它存储 Windows 字符串:
我是数据库开发新手,目前从事 MySQL 工作。我有一列包含美国的所有时区。我希望能够获取每行中每个时区的当前时间(只有时间,没有日期)。 时区数据如下:+05:00、-03:00 等等 这就是我尝试
我正在尝试了解 session 时区在 PostgreSQL 中的工作原理。 当我将 session 时区设置为“01:00”时,返回日期为“-01:00”。符号总是颠倒的,我不理解这种行为。 这怎么
我写入我的 mongoDB 数据库,例如开始日期,开始日期始终是一周中的星期一(我使用并且我在欧洲中部时区)。 "startDate" : ISODate("2014-12-28T23:00:00Z"
是否有详尽的 MySQL 时区列表? 似乎 MySQL 设置中 time_zone 的有效值取决于主机操作系统,但我一直无法找到可能值的列表。 我需要时间显示卡尔加里本地时间。 最佳答案 默认情况下,
我已经在 config/app.php 中配置了时区至 Europe/Lisbon . 如果我做 return date_default_timezone_get(); , 返回 Europe/Lis
我在整个脚本中广泛使用 unix 时间,通常我将时区设置如下: date_default_timezone_set('Europe/London'); 这一直工作正常,直到上周末时钟回退一个小时,我怀
我有一个字符串,2013-10-07T23:59:51.205-07:00,想将其转换为 Java 日期对象。我收到解析错误。 date = new SimpleDateFormat("yyyy-MM
如何更改日志文件名中的 log4j 时区? 我的 log4j.xml 文件:
在JAVA中,如何确定所有日期都作为GMT日期返回? 例如,即使我尝试使用GMT语言环境强制使用DateFormat,它也会应用某种逻辑来检索BST日期。 public static void mai
好吧,这是我遇到过的最奇怪的错误之一! 首先:我不是 Python 程序员,该脚本是由 friend 编写的(我认为他的大部分内容来自示例)。 该脚本的用途:将日历信息(CET 时间)从 XML 文件
我们有一个网站,目前在某个时间(由用户选择)运行拍卖,这一切都工作正常,因为当服务器到达该时间时,拍卖就会开始。然后我们必须添加时区,具体取决于用户居住的地方。然后,根据所选的下拉菜单,这将增加或减少
我陷入了这个逻辑。我正在开发一个消息调度项目,我的客户来自不同的国家。让我们考虑一下亚洲(不遵守夏令时)和美国/纽约(遵守夏令时)。 现在,我正在编写以下查询来获取距当前时间 10 分钟间隔内的时间表
我尝试返回特定时区的值,但我遇到了一些奇怪的响应行为: SELECT created_at AT TIME ZONE 'US/Pacific' - created_at, NOW() A
我正在尝试将不带时区值(例如“31.05.2015”)的字符串转换为 NSDate。 我的 DateFormatter 有什么问题?它返回日期 2015-05-31 22:00:00 +0000当我尝
我已经成功地将我的本地 tzinfo 导入到 mysql 中,我几乎已经了解了关于如何存储和使用这些数据的所有细节,除了一个字段。有一个名为 Transition_type_id 的数字字段(在表 t
为什么以下行返回的 TimeZone 显示不正确的时间: TimeZone.getTimeZone("America/Ottawa") 现在显示晚上 10:26 [亚特兰大时间现在是 6:26,Ott
我是一名优秀的程序员,十分优秀!