我有以下代码:
DateFormat df = new SimpleDateFormat("M/d/yy h:mm a z");
df.setLenient(false);
System.out.println(df.parse("6/29/2012 5:15 PM IST"));
Run Code Online (Sandbox Code Playgroud)
假设我现在将PC的时区设置为太平洋时间(PDT为UTC-7),则会打印出来
2012年4月29日星期五08:15:00
不是PDT比IST(印度标准时间)落后12.5小时?任何其他时区都不会出现此问题 - 我在日期字符串中尝试使用UTC,PKT,MMT等代替IST.Java中有两个IST是否有机会?
PS:实际代码中的日期字符串来自外部源,因此我不能使用GMT偏移或任何其他时区格式.
时区的abberviated名称含糊不清,并因Olson时区的名字而被弃用.以下工作始终如一,因为parse()和getTimezone()的行为方式可能存在差异.
SimpleDateFormat sdf = new SimpleDateFormat("M/d/yy h:mm a Z");
TimeZone istTimeZone = TimeZone.getTimeZone("Asia/Kolkata");
Date d = new Date();
sdf.setTimeZone(istTimeZone);
String strtime = sdf.format(d);
Run Code Online (Sandbox Code Playgroud)
对不起,我必须为此写一个答案,但请尝试以下代码:
public class Test {
public static void main(String[] args) throws ParseException {
DF df = new DF("M/d/yy h:mm a z");
String [][] zs = df.getDateFormatSymbols().getZoneStrings();
for( String [] z : zs ) {
System.out.println( Arrays.toString( z ) );
}
}
private static class DF extends SimpleDateFormat {
@Override
public DateFormatSymbols getDateFormatSymbols() {
return super.getDateFormatSymbols();
}
public DF(String pattern) {
super(pattern);
}
}
}
Run Code Online (Sandbox Code Playgroud)
您会发现IST在列表中出现过几次,第一次确实是以色列标准时间.
| 归档时间: |
|
| 查看次数: |
15135 次 |
| 最近记录: |