英国夏令时间 - BST不是SimpleDateFormat的时区的认可 [英] British Summer Time - BST not recognised by SimpleDateFormat timezone
问题描述
的SimpleDateFormat
解析在JUnit测试(Robolectric测试运行)的作品,但如果设备运行失败。
SimpleDateFormat
parse works in a JUnit test (Robolectric test runner), but fails if run on device.
@Test
public void testDateParse() throws ParseException {
String datetime = "Wed Sep 03 12:59:27 BST 2014";
new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy", Locale.ENGLISH)
.parse(datetime);
}
当它失败设备上的异常表示:
When it fails on the device the exception says:
java.text.ParseException: Unparseable date: "Wed Sep 03 12:59:27 BST 2014" (at offset 20)
20偏移量为 B的
BST
,所以它的时区的关系。
Offset 20 is the B
of BST
, so it's timezone related.
确实GMT
和字符串日期时间=周三9月3日12点59分27秒GMT 2012;
推荐答案
我想这里的有趣的问题是,为什么这样做是否对Robolectric确定,但未能在Android试运行。
I guess the interesting question here is why does this run ok on Robolectric but fails to run on an Android test.
机器人在JVM上运行,并有对时区Android和良好的醇'Java之间的解析行为差异。
Robo runs on the JVM and there is a difference in behaviour for Timezone parsing between Android and good ol' Java.
在 Android文档:
除了特殊情况UTC和GMT(这是同义在这种情况下,既对应于UTC),机器人不支持德precated用Java 1.1使用三个字母的时区的ID。
Other than the special cases "UTC" and "GMT" (which are synonymous in this context, both corresponding to UTC), Android does not support the deprecated three-letter time zone IDs used in Java 1.1.
在<一个href=\"http://docs.oracle.com/javase/7/docs/api/java/util/TimeZone.html#getTimeZone(java.lang.String)\"相对=nofollow> Java文档:
的ID为一个TimeZone,要么是缩写,如PST,全名,如美国/洛杉矶,或自定义ID,如GMT-8:00。需要注意的是缩写的支持仅供JDK 1.1.x版本的兼容性,应该使用全名。
the ID for a TimeZone, either an abbreviation such as "PST", a full name such as "America/Los_Angeles", or a custom ID such as "GMT-8:00". Note that the support of abbreviations is for JDK 1.1.x compatibility only and full names should be used.
因此,答案是这3个字母的缩写不应使用(因为它们是模棱两可),但是的可以的可以在JVM上出于兼容性考虑使用,但不能在Android上。
So the answer is that the 3-letter abbreviations should not be used (because they are ambiguous), but can be used on the JVM for compatibility reasons, but not on Android.
这篇关于英国夏令时间 - BST不是SimpleDateFormat的时区的认可的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!