Java Date Timestamp conversion issue -
i doing simple string date conversion in java, 1 thing have take care conversion should in gmt. able convert string in gmt. when tried convert date timestamp got different value (seems value based on local timezone).
public static void timefun(string str) throws parseexception { simpledateformat formatter = new simpledateformat("yyyy-mm-dd't'hh:mm:ss.sssxxx"); string dateinstring = "2015-07-10t09:54:31.000-04:00"; date date = formatter.parse(dateinstring); simpledateformat sdfamerica = new simpledateformat("yyyy-mm-dd't'hh:mm:ss.sssxxx"); timezone tz = timezone.gettimezone("gmt"); sdfamerica.settimezone(tz); string converteddate = sdfamerica.format(date); // convert string // first date dateingmt = sdfamerica.parse(converteddate); system.out.println(converteddate); // output = 2015-07-10t13:54:31.000z (right) timestamp timestamp = new java.sql.timestamp(dateingmt.gettime()); system.out.println(timestamp); // output = 2015-07-10 19:24:31.0 (wrong) }
timestamp.tostring() internally uses calendar instance default system timezone. can see in source code java.util.date.normalize() magic. so, yes ... last system output based on local timezone.
Comments
Post a Comment