我必须在我的 Java 应用程序中打印 EST 时间.我已使用以下方法将时区设置为 EST:
I have to print the EST time in my Java application. I had set the time zone 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 doesn't 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 处理夏令时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!