java - Android SimpleDateFormat returning incorrect time between midnight and 1am -
solved have found between midnight , 1am device returns time 1 hour later (the other 23 hours day returns correctly). more weirdly returns correctly if use kk
instead of hh
(though resulting string no use me)
code running: (in instance strformat
matches hardcoded string in df3
)
simpledateformat df = new simpledateformat(strformat, locale.us); simpledateformat df2 = new simpledateformat("yyyy-mm-dd kk:mm", locale.us); simpledateformat df3 = new simpledateformat("yyyy-mm-dd hh:mm:ss.sss", locale.us); date d = c.gettime(); string s = d.tostring(); string ret = df.format(c.gettime()); string ret2 = df.format(new date(system.currenttimemillis())); string ret3 = df2.format(c.gettime()); string ret4 = df3.format(c.gettime()); string r1 = ""+c.get(calendar.hour);
these return:
s = "thu jan 07 00:39:32 gmt-11:00 2016" ret = "2016-01-07 01:39:32" ret2 = "2016-01-07 01:39:32" ret3 = "2016-01-07 24:39" ret4 = "2016-01-07 01:39:32.525" r1 = "0"
after 1am these return:
s = "thu jan 07 01:07:09 gmt-11:00 2016" ret = "2016-01-07 01:07:09" ret2 = "2016-01-07 01:07:09" ret3 = "2016-01-07 01:07" ret4 = "2016-01-07 01:07:09.606" r1 = "1"
any in have done wrong/what going wrong appreciated.
update
changing code to:
simpledateformat df = new simpledateformat(strformat, locale.us); df.settimezone(timezone.gettimezone("gmt")); simpledateformat df2 = new simpledateformat("yyyy-mm-dd kk:mm zzz", locale.us); df2.settimezone(timezone.gettimezone("gmt")); simpledateformat df3 = new simpledateformat("yyyy-mm-dd hh:mm:ss.sss zzz", locale.us); df3.settimezone(timezone.gettimezone("gmt")); date d = c.gettime(); string s = d.tostring(); string ret = df.format(c.gettime()); string ret2 = df.format(new date(system.currenttimemillis())); string ret3 = df2.format(c.gettime()); string ret4 = df3.format(c.gettime()); string r1 = ""+c.get(calendar.hour); return ret;
i same results:
s = "fri jan 08 00:52:05 gmt 2016" ret = "2016-01-08 01:52:05" ret2 = "2016-01-08 01:52:05" ret3 = "2016-01-08 24:52 gmt" ret4 = "2016-01-08 01:52:05.169 gmt" r1 = "0"
and
s = "fri jan 08 01:03:23 gmt 2016" ret = "2016-01-08 01:03:23" ret2 = "2016-01-08 01:03:23" ret3 = "2016-01-08 01:03 gmt" ret4 = "2016-01-08 01:03:23.547 gmt" r1 = "1"
so problem seems unrelated timezone
may have set timezone dateformat objects.
i tried :
calendar c = new gregoriancalendar(timezone.gettimezone("gmt-11:00"), locale.us); c.set(2016, 0, 7, 00, 39, 32); date date = c.gettime(); simpledateformat df = new simpledateformat("eee mmm dd hh:mm:ss zzz yyyy", locale.us); df.settimezone(timezone.gettimezone("gmt-11:00")); simpledateformat df2 = new simpledateformat("yyyy-mm-dd kk:mm", locale.us); df2.settimezone(timezone.gettimezone("gmt-11:00")); simpledateformat df3 = new simpledateformat("yyyy-mm-dd hh:mm:ss.sss", locale.us); df3.settimezone(timezone.gettimezone("gmt-11:00")); string ret = df.format(date.gettime()); string ret2 = df.format(date.gettime()); string ret3 = df2.format(date.gettime()); string ret4 = df3.format(date.gettime()); system.out.println(" ret : "+ret); system.out.println(" ret2 : "+ret2); system.out.println(" ret3 kk: "+ret3); system.out.println(" ret4 : "+ret4);
the result correct :
ret : thu jan 07 00:39:32 gmt-11:00 2016 ret2 : thu jan 07 00:39:32 gmt-11:00 2016 ret3 kk: 2016-01-07 24:39 ret4 : 2016-01-07 00:39:32.217
Comments
Post a Comment