Quantcast
Viewing all articles
Browse latest Browse all 9

Is java.sql.Timestamp timezone specific?

I have to store UTC dateTime in DB.
I have converted the dateTime given in specific timezone to UTC. for that I followed the below code.
My input dateTime is "20121225 10:00:00 Z" timezone is "Asia/Calcutta"
My Server/DB(oracle) is running in the same timezone(IST) "Asia/Calcutta"

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.

I have confirmed from the below 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

  1. Date.getTime() is not in UTC
  2. Or Timestamp is has timezone impact while storing into DBWhat am I doing wrong here?

One more question:

Will timeStamp.toString() print in local timezone like java.util.date does? Not UTC?


Viewing all articles
Browse latest Browse all 9

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>