Java时区 – IST的奇怪行为?

我有以下代码:

DateFormat df = new SimpleDateFormat("M/d/yy h:mm az"); df.setLenient(false); System.out.println(df.parse("6/29/2012 5:15 PM IST")); 

假设我现在将PC的时区设置为太平洋时间(PDT为UTC-7),则会打印出来

2012年4月29日星期五08:15:00

不是PDT比IST(印度标准时间)落后12.5小时? 任何其他时区都不会出现此问题 – 我在日期字符串中尝试使用UTC,PKT,MMT等代替IST。 Java中是否有两个IST?

PS:实际代码中的日期字符串来自外部源,因此我不能使用GMT偏移或任何其他时区格式。

对不起,我必须为此写一个答案,但请尝试以下代码:

 public class Test { public static void main(String[] args) throws ParseException { DF df = new DF("M/d/yy h:mm az"); 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); } } } 

你会发现IST在列表中出现了好几次,第一次确实是以色列标准时间。

时区的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); 

不是答案,但请看下面的输出+代码 – 似乎parse对待IST与TimeZone.getTimeZone("IST") ……

星期五6月29日16:15:00 BST 2012
星期五6月29日12:45:00 BST 2012
星期五6月29日12:45:00 BST 2012
* BST =伦敦

 public static void main(String[] args) throws InterruptedException, ParseException { DateFormat fmt1 = new SimpleDateFormat("M/d/yy h:mm a Z"); Date date = fmt1.parse("6/29/2012 5:15 PM IST"); System.out.println(date); DateFormat fmt2 = new SimpleDateFormat("M/d/yy h:mm a"); fmt2.setTimeZone(TimeZone.getTimeZone("IST")); System.out.println(fmt2.parse("6/29/2012 5:15 PM")); DateFormat fmt3 = new SimpleDateFormat("M/d/yy h:mm a"); fmt3.setTimeZone(TimeZone.getTimeZone("Asia/Kolkata")); System.out.println(fmt3.parse("6/29/2012 5:15 PM")); } 

因为IST会有多种含义,如爱尔兰标准时间,Isreal Standrad时间,印度标准时间。

参考: https : //www.timeanddate.com/time/zones/

使用setTimezone()方法专门设置时区。

例如:parser.setTimeZone(TimeZone.getTimeZone(“在这里详细说明时区”));