当前位置:  开发笔记 > 编程语言 > 正文

Android SimpleDateFormat在午夜和凌晨1点之间返回错误的时间

如何解决《AndroidSimpleDateFormat在午夜和凌晨1点之间返回错误的时间》经验,为你挑选了0个好方法。

已解决我发现在午夜和凌晨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无关

推荐阅读
ERIK又
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有