如何显示Java中的夏令时开始后的时间差异? [英] How to show the difference in time after the daylight saving started in Java?
问题描述
我想检查夏令时开始前后的时间差。我正在使用JVM,其中时区与DST WRT巴西时间存在一些问题。我看到的输出显示10月21日属于夏令时,这是错误的,因为夏令时从11月4日开始。我希望看到10月20日至10月21日之间的时差为1小时,由于JRE时区问题,我可以在我的应用程序中看到该时差。有什么办法可以通过程序来实现。
I wanted to check the difference in time before and after the daylight saving time started. I am using a JVM where timezone has some issue with DST WRT Brazilian time. I could see the output where it says that 21st October falls under DST, which is wrong as the daylight saving started on 4th of November. I would like to see the timing difference which is 1 Hour in between 20th of October and 21st of October, which I could see in my application because of JRE Timezone issue. Is there any way I can achieve this by a program. Here below the code I have used to see if the date is under DST or not.
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;
public class Timezone
{
public static void main(String[] args) throws ParseException {
TimeZone TIMEZONE = TimeZone.getTimeZone("Brazil/East");
System.out.println("timeZone : " + TIMEZONE);
Calendar c = Calendar.getInstance(TIMEZONE);
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
dateFormat.setTimeZone(TIMEZONE);
Date date = dateFormat.parse("2018-10-20T00:00:00");
c.setTime(date);
System.out.println("In dst " + c.getTimeZone().inDaylightTime(c.getTime()));
date = dateFormat.parse("2018-10-21T00:00:00");
c.setTime(date);
System.out.println("In dst " + c.getTimeZone().inDaylightTime(c.getTime()));
date = dateFormat.parse("2018-11-04T00:00:00");
c.setTime(date);
System.out.println("In dst " + c.getTimeZone().inDaylightTime(c.getTime()));
}
}
除了我的问题,我还能够找出夏令时是通过使用我的上述程序进行的。我想告诉我什么:10月20日时间是上午9点,而由于夏令时造成的同一时间偏移了1小时,即上午10点。有什么方法可以实现?
Adding further to my question I was able to figure out that daylight saving is applied by using my above program. I would like something where it should show me: On 20th October time is 9 am whereas same time due to DST shifted by 1 hr, i.e., 10 am. Is there any way to achieve this?
预期输出:
timeZone : sun.util.calendar.ZoneInfo[id="Brazil/East"]
Time is: 21-10-2018 07:52:16
实际输出错误的JVM:
timeZone : timeZone : sun.util.calendar.ZoneInfo[id="Brazil/East"]
Time is: 21-10-2018 08:52:30
推荐答案
我将添加一个简短的答案。它表明1天不等于24小时,即DST已更改。我还使用Java 8,因为我对Java 8更加熟悉。
I'll add a very brief answer. It shows that 1 day is not equal to 24 hours, i.e. DST changed. I also stepped over to Java 8 because I'm more familiar with it.
ZoneId timezone = ZoneId.of("Brazil/East");
ZonedDateTime t = ZonedDateTime.of(2018, 10, 20, 7, 52, 16, 0, timezone);
System.out.println(t);
System.out.println(t.plus(1, ChronoUnit.DAYS));
System.out.println(t.plus(24, ChronoUnit.HOURS));
输出:
2018-10- 20T07:52:16-03:00 [巴西/东部]
2018-10-21T 07 :52:16-02:00 [巴西/东部]
2018-10-21T 08 :52:16-02:00 [巴西/东部]
2018-10-20T07:52:16-03:00[Brazil/East]
2018-10-21T07:52:16-02:00[Brazil/East]
2018-10-21T08:52:16-02:00[Brazil/East]
这篇关于如何显示Java中的夏令时开始后的时间差异?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!