android / java se SimpleDateFormat TimeZone 问题
我在 android 2.1 上运行的程序中遇到了一个奇怪的问题,对我来说这似乎是 android 中的一个错误。请告诉我这里发生了什么,因为我迷路了。 :)
以下程序:
import java.util.*;
import java.text.*;
class TestMe {
public static void main(String[] args) {
String time = "2010-08-01T18:00:00+0000";
TimeZone tg = TimeZone.getDefault();
System.out.println(tg.getID());
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
try {
Date indate = formatter.parse(time);
String outdate = formatter.format(indate);
System.out.println(outdate);
formatter.setTimeZone( TimeZone.getTimeZone("UTC"));
String ooutdate = formatter.format(indate);
System.out.println(ooutdate);
formatter.setTimeZone( TimeZone.getDefault() );
Date ioutdate = formatter.parse(ooutdate);
System.out.println(formatter.format(ioutdate));
} catch( Exception e ) {
e.printStackTrace();
}
}
}
在 mac 上使用最新的 java se 时返回此内容:
(~) % javac -g TestMe.java && java TestMe
Europe/Berlin
2010-08-01T20:00:00+0200
2010-08-01T18:00:00+0000
2010-08-01T20:00:00+0200
但是当我在 2.1 上的 android 应用程序中的方法内运行它时返回以下内容:
I/System.out( 5379): ------------------------------------------------------------------
I/System.out( 5379): Europe/Berlin
I/System.out( 5379): 2010-08-01T20:00:00+0200
I/System.out( 5379): 2010-08-01T18:00:00+0100
I/System.out( 5379): 2010-08-01T18:00:00+0100
I/System.out( 5379): ------------------------------------------------------------------
有人知道这个特定的 android api 修订版与 TimeZone 和日期解析?这对我来说是一个非常奇怪的问题,因为 android 版本应该返回与 java se 版本完全相同的结果,但事实并非如此。
感谢您的任何提示,
卡斯滕
I'm encountering a strange issue in a program running on android 2.1 which seems like a bug in android to me. Please enlighten me as to what is going here, as I'm lost. :)
The following program:
import java.util.*;
import java.text.*;
class TestMe {
public static void main(String[] args) {
String time = "2010-08-01T18:00:00+0000";
TimeZone tg = TimeZone.getDefault();
System.out.println(tg.getID());
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
try {
Date indate = formatter.parse(time);
String outdate = formatter.format(indate);
System.out.println(outdate);
formatter.setTimeZone( TimeZone.getTimeZone("UTC"));
String ooutdate = formatter.format(indate);
System.out.println(ooutdate);
formatter.setTimeZone( TimeZone.getDefault() );
Date ioutdate = formatter.parse(ooutdate);
System.out.println(formatter.format(ioutdate));
} catch( Exception e ) {
e.printStackTrace();
}
}
}
returns this, when using latest java se on the mac:
(~) % javac -g TestMe.java && java TestMe
Europe/Berlin
2010-08-01T20:00:00+0200
2010-08-01T18:00:00+0000
2010-08-01T20:00:00+0200
but returns the following when I run it inside a method in my android app on 2.1 :
I/System.out( 5379): ------------------------------------------------------------------
I/System.out( 5379): Europe/Berlin
I/System.out( 5379): 2010-08-01T20:00:00+0200
I/System.out( 5379): 2010-08-01T18:00:00+0100
I/System.out( 5379): 2010-08-01T18:00:00+0100
I/System.out( 5379): ------------------------------------------------------------------
Is anybody aware of problems in this specific android api revision with TimeZone and Date parsing? This is a very strange issue to me, as it the android version should return exactly the same as the java se version but doesn't.
Thanks for any hints,
Karsten
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
尝试将
setTimeZone("UTC")
替换为setTimeZone("GMT")
它可能是不同的实现,因为现在 BST(英国夏令时)tz 中的 0 子午线是格林威治标准时间+1。Try to replace
setTimeZone("UTC")
withsetTimeZone("GMT")
it might be different implementations as now 0-meridian in BST(British summer time) tz which is GMT+1.是的,我尝试了一下,得到了和你一样的结果。我想那是一个错误。
http://code.google.com/p/android/issues/detail ?id=8258 可能会帮助你
Yes, I tried it and got the same result as you did. I guess its a bug then.
http://code.google.com/p/android/issues/detail?id=8258 might help you out
我已经在windows java和android2.3中进行了测试。我在这两种情况下都得到相同的结果。我认为这个bug在android 2.3中就不再存在了。反正
请检查 android 中的导入语句与 java 中的
导入语句 android
import statements in java
谢谢
迪帕克
I have tested in windows java and android2.3. I am getting the same result in both of the case. I think the bug is no more in android 2.3. Anyway
please check the import statements for android versus that of java
import statements in android
import statements in java
Thanks
Deepak
我设法使用 Calendar 对象完成预期的日期转换:
I managed to get the intended date conversion done using the Calendar object:
尝试在每次 setTimeZone() / format() 调用之前重新创建 SimpleDateFormat 对象。
这解决了我在使用通用 SimpleDateFormat 对象格式化一组跨越 DST 更改的时间时遇到的问题。如果集合中的第一个时间在 DST 阈值之后,则集合中的所有时间的格式均正确。如果集合中的第一次是在 DST 阈值之前,那么 DST 阈值之后的所有时间都会被错误地格式化(即超出一小时) - 即使我尝试在每次 format() 调用之前调用 setTimeZone() ,这种情况也会继续。
我从中推断出,SimpleDateFormat 对象使用的时区与第一个 format() 调用相关,并且对 setTimeZone() 的后续调用实际上被忽略或覆盖(在我看来,这听起来像是一个 Java 问题,而不是一个安卓问题)。
Try recreating your SimpleDateFormat object before each setTimeZone() / format() call.
This fixed an issue I was having when using a common SimpleDateFormat object to format a set of times that spanned a DST change. If the first time in the set was after the DST threshold, all times in the set were formatted correctly. If the first time in the set was before the DST threshold, then all times after the DST threshold were incorrectly formatted (i.e. were one hour out) - and this continued even when I tried calling setTimeZone() before every format() call.
What I inferred from this is that the time zone used by the SimpleDateFormat object is tied to the first format() call, and subsequent calls to setTimeZone() are effectively ignored or overwritten (which sounds to me like a Java issue, rather than an Android issue).