已解决我发现在午夜和凌晨1点之间,我的设备返回1小时后的时间(其他23个小时,它正确返回).更奇怪的是,如果我使用kk
而不是HH
(因为结果字符串对我没用),它会正确返回
我正在运行的代码:(在这个实例中strFormat
匹配硬编码的字符串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);
这些回报:
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"
凌晨1点后返回:
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"
我做错了什么/出了什么问题的任何帮助非常感谢.
UPDATE
将代码更改为:
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;
我得到了相同的结果:
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"
和
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"
所以问题似乎与TimeZone无关