gpt4 book ai didi

java - Android SimpleDateFormat 在午夜和凌晨 1 点之间返回不正确的时间

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:36:16 26 4
gpt4 key购买 nike

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

所以问题似乎与时区无关

最佳答案

可能您必须为 dateFormat 对象设置时区。

我试过这个:

    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

关于java - Android SimpleDateFormat 在午夜和凌晨 1 点之间返回不正确的时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34659639/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com