如何在Java中使用Timezone解决夏令时

编程入门 行业动态 更新时间:2024-10-11 05:30:05
本文介绍了如何在Java中使用Timezone解决夏令时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我必须在我的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解决夏令时

本文发布于:2023-11-23 20:17:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1622758.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:夏令时   如何在   Java   Timezone

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!