Java 时区 - IST 的奇怪行为?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11264697/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java timezone - strange behavior with IST?
提问by Vasan
I have the below code:
我有以下代码:
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"));
Assuming I now set my PC's timezone to Pacific Time (UTC-7 for PDT), this prints
假设我现在将 PC 的时区设置为太平洋时间(PDT 为 UTC-7),这会打印
Fri Jun 29 08:15:00 PDT 2012
2012 年 6 月 29 日星期五 08:15:00 PDT
Isn't PDT 12.5 hours behind IST (Indian Standard Time)? This problem does not occur for any other timezone - I tried UTC, PKT, MMT etc instead of IST in the date string. Are there two ISTs in Java by any chance?
PDT 不是比 IST(印度标准时间)晚 12.5 小时吗?任何其他时区都不会出现此问题 - 我在日期字符串中尝试了 UTC、PKT、MMT 等而不是 IST。Java 中是否有两个 IST?
P.S: The date string in the actual code comes from an external source, so I cannot use GMT offset or any other timezone format.
PS:实际代码中的日期字符串来自外部源,因此我不能使用 GMT 偏移量或任何其他时区格式。
回答by rnk
The abberviated names of timezone are ambiguous and have been deprecated for Olson names for timezones. The following works consistently as there may be be differences in the way parse() and getTimezone() behaves.
时区的缩写名称不明确,并且已被弃用时区的 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);
回答by biziclop
Sorry, I have to write an answer for this, but try this code:
对不起,我必须为此写一个答案,但试试这个代码:
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);
}
}
}
You'll find that IST appears several times in the list and the first one is indeed Israel Standard Time.
您会发现 IST 在列表中出现了多次,而第一个确实是以色列标准时间。
回答by assylias
Not an answer but see the output + code below - it does seem that parse
treats IST differently from TimeZone.getTimeZone("IST")
...
不是答案,但请参阅下面的输出 + 代码 - 似乎parse
对 IST 的处理与TimeZone.getTimeZone("IST")
...不同。
Fri Jun 29 16:15:00 BST 2012
Fri Jun 29 12:45:00 BST 2012
Fri Jun 29 12:45:00 BST 2012
*BST = London
2012 年
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"));
}
回答by Sumanth
It is because of IST will have multiple meanings like Irish Standard Time, Isreal Standrad Time, Indian Standard Time.
这是因为 IST 将具有多种含义,如爱尔兰标准时间、Isreal Standrad 时间、印度标准时间。
Ref: https://www.timeanddate.com/time/zones/
参考:https: //www.timeanddate.com/time/zones/
Use setTimezone() method specifically to set the timezone.
专门使用 setTimezone() 方法来设置时区。
Ex: parser.setTimeZone(TimeZone.getTimeZone("specify timezone in detail here"));
例如: parser.setTimeZone(TimeZone.getTimeZone("请在此处详细指定时区"));