使用Java日历时忽略DST [英] Ignoring DST when using Java Calendars
问题描述
我有一个绘制时间序列图的GUI。用户通过在文本框中输入值来输入他们想要绘制的日期。例如,如果他们输入 25/07/13 22:00 和 26/07/13 00:00 ,图表应输入来自的数据25日22:00:00至次日早晨00:00:59。用户输入的时间被解析为日历
对象。
I have a GUI that plots time-series graphs. The user enters the dates they want to plot from and to by entering values in text boxes. For example, if they enter 25/07/13 22:00 and 26/07/13 00:00 the graph should plot data from 22:00:00 on the 25th through to 00:00:59 the following morning. The times the user enters are parsed into a Calendar
object.
我的问题来自DST。
用户不关心DST,所以他们想要看到的只是这两次之间的图表。但是,日历
对象确实关心DST,因此我的到日期当前不是00:00,而是01:00。 (我在伦敦,我们目前是格林尼治标准时间+ 1小时的夏令时)。
The user doesn't care about DST, so all they want to see is a graph between those two times. However, the Calendar
objects do care about DST and so my "to" date is currently not 00:00, but 01:00. (I am in London and we are currently GMT + 1 hour for DST).
我想有效地忽略DST,并且当我想要时,好像一切都是GMT + 0绘制图表。
I want to effectively ignore DST and act as though everything is GMT + 0 when I want to plot graphs.
我该怎么做?
推荐答案
谢谢你们的答案,他们帮我解决了问题。这有点归结为我使用 Calendar
对象,用于表示和存储数据,以及 epoch
用于时间计算。
Thanks for the answers guys, they helped me get my head around the problem. It sort of comes down to the fact that I use both the Calendar
object, for presentation and storage of data, and the epoch
for temporal calculations.
事实证明日历
set()
方法将考虑DST当然。因此,当我在用户输入的文本框中解析时间值时,为每个日历
set() >字段,日历
对象将知道,基于历史数据,您刚刚设置的日期是否已应用DST。因此,它理解你的意思,例如,GMT + 1,即使你没有意识到它(因为,坦率地说,谁呢?!)。
It turns out that the Calendar
set()
methods will take into account DST as a matter of course. So when I parse the time values in the text boxes that the user enters, and use set()
for each individual Calendar
field, the Calendar
object will know, based-off historical data, whether the date you've just set will have DST applied. Because of this, it understands that you meant, for example, GMT+1, even if you didn't realise it (because, frankly, who does?!).
但是,当你执行 getTimeInMillis()
时,返回的 epoch
没有概念时区或DST,以便与当前时区匹配,您必须手动将DST应用于返回的纪元
(如果适用)。相反,当您在日历
对象上使用 setTimeInMillis()
时,假设您输入的时间是GMT + 0,但如果纪元
是当前已应用DST的日期,日历
对象会为你添加它,这意味着你距离你想象的地方+1小时。要解决此问题,您需要在纪元
中再次扣除DST,然后再在日历中设置。
However, when you do getTimeInMillis()
, the epoch
returned has no concept of time zones or DST, so to match with the current time zone you have to apply DST manually to the returned epoch
, if it applies. Conversely, when you use setTimeInMillis()
on a Calendar
object, it is assumed that the time you entered is GMT+0, but if the epoch
is a date that currently has DST applied, the Calendar
object will add it on for you, meaning you're +1 hour from where you thought you were. To solve this problem, you need to subtract DST, again if necessary, from the epoch
before setting it in the calendar.
所有这些混乱在日间界限上尤其重要,特别是如果你像我一样使用日期分辨率。
All of this confusion is particularly important on day boundaries, especially if you're using day resolution for anything, like me.
这篇关于使用Java日历时忽略DST的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!