问题:
I have to store UTC dateTime in DB.我必须将 UTC dateTime 存储在 DB 中。
I have converted the dateTime given in specific timezone to UTC.我已将特定时区中给出的日期时间转换为 UTC。 for that I followed the below code.为此,我遵循了以下代码。
My input dateTime is "20121225 10:00:00 Z" timezone is "Asia/Calcutta"我输入的日期时间是“20121225 10:00:00 Z”时区是“亚洲/加尔各答”
My Server/DB(oracle) is running in the same timezone(IST) "Asia/Calcutta"我的服务器/数据库(oracle)运行在同一时区(IST)“亚洲/加尔各答”
Get the Date object in this specific Timezone获取此特定时区中的日期对象
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()));
Store into DB存入数据库
if (tsSchedStartTime != null) {
stmt.setTimestamp(11, tsSchedStartTime);
} else {
stmt.setNull(11, java.sql.Types.DATE);
}
OUTPUT输出
DB (oracle) has stored the same given dateTime: "20121225 10:00:00
not in UTC. DB(oracle)存储了相同的给定dateTime: "20121225 10:00:00
not in UTC。
I have confirmed from the below sql.我已经从下面的 sql 中确认了。
select to_char(sched_start_time, 'yyyy/mm/dd hh24:mi:ss') from myTable
My DB server also running on the same timezone "Asia/Calcutta"我的数据库服务器也在同一时区“亚洲/加尔各答”上运行
It gives me the below appearances它给了我以下外观
Date.getTime()
is not in UTCDate.getTime()
不是 UTC- Or Timestamp is has timezone impact while storing into DB What am I doing wrong here?或者时间戳在存储到数据库时有时区影响我在这里做错了什么?
One more question:还有一个问题:
Will timeStamp.toString()
print in local timezone like java.util.date
does? timeStamp.toString()
会像java.util.date
那样在本地时区打印吗? Not UTC?不是UTC?