【发布时间】:2016-09-26 06:33:47
【问题描述】:
我必须将格式为“2016-09-25 17:26:12”的 UTC 日期转换为 Android 的当前时区。我这样做了:
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
simpleDateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
Date myDate = simpleDateFormat.parse(temp.getString("date"));
System.out.println(mydate.toString());
这可行,但我不知道为什么它也会打印“GMT+02:00”。 这是输出:“Sun Sep 25 19:26:12 GMT+02:00 2016”,我不想显示“GMT+02:00”。
编辑,解决方案代码:
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
simpleDateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
SimpleDateFormat outputSdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date myDate = simpleDateFormat.parse(temp.getString("date"));
System.out.println(outputSdf.format(myDate));
【问题讨论】:
-
您没有显示任何可以打印出任何内容的内容...如果您要打印出
myDate.toString(),那正是Date.toString()所做的。基本上你应该使用另一个SimpleDateFormat来实现你想要的输出。 -
是的,我正在打印 myDate.toString()
-
所以不要那样做。使用 SimlleDateFormat 对其进行格式化。