解决
我发现在午夜和凌晨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"
对我做错了什么/出了什么问题的任何帮助都非常感激。
更新
将代码更改为:
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
无关答案 0 :(得分:4)
您可能需要为dateFormat对象设置timeZone。
我试过了:
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);
结果是正确的:
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
答案 1 :(得分:1)
Idk ......老实说。我尝试重新创建你的问题,但是在PC上的Eclipse中,而不是在Android中。这是我用过的:
Calendar c = new GregorianCalendar(TimeZone.getTimeZone(ZoneId.ofOffset("", ZoneOffset.ofHours(-11))), Locale.US);
c.set(2016, 0, 7, 0, 7);
String strFormat = "yyyy-MM-dd HH:mm:ss.SSS";
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);
System.out.printf("s = %s%nret = %s%nret2 = %s%nret3 = %s%nret4 = %s%nr1 = %s%n%n", s, ret, ret2, ret3, ret4, r1);
df.setTimeZone(TimeZone.getTimeZone(ZoneId.ofOffset("", ZoneOffset.ofHours(-11))));
df2.setTimeZone(TimeZone.getTimeZone(ZoneId.ofOffset("", ZoneOffset.ofHours(-11))));
df3.setTimeZone(TimeZone.getTimeZone(ZoneId.ofOffset("", ZoneOffset.ofHours(-11))));
d = c.getTime();
s = d.toString();
ret = df.format(c.getTime());
ret2 = df.format(new Date(System.currentTimeMillis()));
ret3 = df2.format(c.getTime());
ret4 = df3.format(c.getTime());
r1 = ""+c.get(Calendar.HOUR);
System.out.printf("s = %s%nret = %s%nret2 = %s%nret3 = %s%nret4 = %s%nr1 = %s%n%n", s, ret, ret2, ret3, ret4, r1);
c.set(2016, 0, 7, 1, 7);
d = c.getTime();
s = d.toString();
ret = df.format(c.getTime());
ret2 = df.format(new Date(System.currentTimeMillis()));
ret3 = df2.format(c.getTime());
ret4 = df3.format(c.getTime());
r1 = ""+c.get(Calendar.HOUR);
System.out.printf("s = %s%nret = %s%nret2 = %s%nret3 = %s%nret4 = %s%nr1 = %s%n", s, ret, ret2, ret3, ret4, r1);
这就是我得到的:
s = Thu Jan 07 12:07:58 CET 2016
ret = 2016-01-07 12:07:58.662
ret2 = 2016-01-07 17:55:58.697
ret3 = 2016-01-07 12:07
ret4 = 2016-01-07 12:07:58.662
r1 = 0
s = Thu Jan 07 12:07:58 CET 2016
ret = 2016-01-07 00:07:58.662
ret2 = 2016-01-07 05:55:58.718
ret3 = 2016-01-07 24:07
ret4 = 2016-01-07 00:07:58.662
r1 = 0
s = Thu Jan 07 13:07:58 CET 2016
ret = 2016-01-07 01:07:58.662
ret2 = 2016-01-07 05:55:58.722
ret3 = 2016-01-07 01:07
ret4 = 2016-01-07 01:07:58.662
r1 = 1
也许你的SimpleDateFormat存在问题,它使用不同的时区......这真的很有趣。
答案 2 :(得分:0)
可能: c.getTime()返回当前时间,SimpleDateFormat将其格式化为Locale.US。美国是一个拥有不同时区的大国,您的设备时区可能与Locale.US不同! 删除Locale.US并让我们发布。
答案 3 :(得分:0)
问题在于平板电脑本身 - 它们内置了额外的传感器,一旦被访问弄乱了内部时钟。 谢谢大家的帮助