java的SimpleDateFormat
parse
方法支持时区短名称、长名称和偏移量。为什么不支持时区Id??
例如。
SimpleDateFormat sdf = new SimpleDateFormat("z");
sdf.parse("IST"); //works fine
SimpleDateFormat sdf = new SimpleDateFormat("z");
sdf.parse("Indian Standard Time"); //Also works fine
为什么java不支持这个:
SimpleDateFormat sdf = new SimpleDateFormat("z");
sdf.parse("Asia/Kolkata"); //does not work
发布于 2013-02-01 11:40:28
我们应该问问JDK开发人员为什么他们决定SimpleDateFormat不应该支持时区Id。此外,SimpleDateFormat应用程序接口也不清楚它希望'z‘采用什么时区格式。但我知道它支持什么。它根据DateFormatSymbols.getZoneStrings()返回的数据检查时区。它是一个时区数组,每个时区是一个字符串数组
•[0] - time zone ID
•[1] - long name of zone in standard time
•[2] - short name of zone in standard time
•[3] - long name of zone in daylight saving time
•[4] - short name of zone in daylight saving time
区域ID不是本地化的;其他是本地化的名称。详情请参见API。
我们可以获取所有可用的时区
DateFormatSymbols dfs = DateFormatSymbols.getInstance();
for(String[] s : dfs.getZoneStrings()) {
System.out.println(Arrays.toString(s));
}
结果(取决于区域设置)
...
[Asia/Calcutta, India Standard Time, IST, India Daylight Time, IDT]
...
因此,SimpleDateFormat (在我的区域设置中)允许'z‘的印度标准时间、印度标准时间、印度夏令时或IDT,但它不允许亚洲/加尔各答(时区ID)
发布于 2015-06-23 08:04:05
如果你使用Java8,你可以使用新的日期和时间API。"VV“表示时区ID。
请参阅https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html
示例:
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MM/dd/yyyy HH:mm VV");
ZonedDateTime zonedDateTime = ZonedDateTime.parse("06/23/2015 21:00 US/Pacific", formatter);
https://stackoverflow.com/questions/14644614
复制相似问题