如何在Java中使用Timezone解决夏令时 [英] How to tackle daylight savings using Timezone in java
问题描述
我必须在我的java应用程序中打印EST时间。我已使用
I have to print the EST time in my java application. I had set the timezone to EST using
Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("EST"));
但是当在这个时区遵循夏令时,我的代码不打印正确的时间它打印少1小时)。如何使代码工作,总是读取正确的时间,无论是否遵守夏令时。
But when the daylight savings is being followed in this timezone, my code does not print the correct time(It prints 1 hour less). How to make the code work to read the correct time always irrespective of whether the daylight savings are being observed or not.
PS:我尝试将时区设置为EDT,但它不能解决问题
PS: I tried setting the timezone to EDT, but it does'nt solve the problem
推荐答案
这是开始的问题:
Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("EST"));
应全心避免使用3个字母的缩写,以利于TZDB区域ID。 EST是东部标准时间 - 和标准时间从未观察到DST;它并不是一个完整的时区名称。它是用于时区的部分的名称。 (不幸的是,我没有遇到这个半时区概念的好词。)
The 3-letter abbreviations should be wholeheartedly avoided in favour of TZDB zone IDs. EST is Eastern Standard Time - and Standard time never observes DST; it's not really a full time zone name. It's the name used for part of a time zone. (Unfortunately I haven't come across a good term for this "half time zone" concept.)
你想要一个 。例如, America / New_York
位于东部时区:
You want a full time zone name. For example, America/New_York
is in the Eastern time zone:
TimeZone zone = TimeZone.getTimeZone("America/New_York");
DateFormat format = DateFormat.getDateTimeInstance();
format.setTimeZone(zone);
System.out.println(format.format(new Date()));
这篇关于如何在Java中使用Timezone解决夏令时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!