問題:
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?