我在使用日期和日历类处理指数分布以模拟到达商店的时间(学术工作)时发现了这种有趣的行为。代码非常简单,如下所示。假设“this.currentDate”是“Feb 15 08:00:00 BRST 2014”。 如果我将时间向前移动 24 小时(参数 iSeconds=86.400),应该返回什么?预期的字符串将是“2014-02-16 08:00:00”,但时间缩短了 1 小时,结果是“2014-02-16 07:00:00”,我想知道是否有人可以解释为什么我的一小时被“偷”了。没什么大不了的,但由于我的下一次到达时间取决于较早的到达时间,这使我的时间基线变得一团糟,它们也全部移动了一小时。
我以为可能是一些 TZ 问题,但哎呀,我刚刚在 2 月中旬搬了 24 小时。
public String shiftTimeStamp( int iSeconds)
{
Calendar cal = Calendar.getInstance();
cal.setTime(this.currentDate);
cal.add(Calendar.SECOND, iSeconds);
this.currentDate = cal.getTime();
String sTS = new SimpleDateFormat(SCSimLabels.DATE_TS_FORMAT).format(this.currentDate);
return sTS;
}
注意:夏令时问题 :) BRT <--> BRST tz。
我的解决方法:我只是想要一个信标来引导由于到达时间间隔而导致的时间跳跃,并且我对此类日历细节不感兴趣,因此当我需要移动到第二天的第一个工作时间时,我只是强制1 天类后时间为 08:00:00。它就像一个魅力:)
Calendar cal = Calendar.getInstance();
cal.setTime(this.currentDate);
cal.add(Calendar.DATE, 1);
String sDate = (new SimpleDateFormat("yyyy-MM-dd 08:00:00")).format(cal.getTime());
Date newDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(sDate);
this.currentDate = newDate;
最佳答案
将格式调用更改为:
return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss z").format(this.currentDate);
查看格式调用使用的时区。我打赌对 .add() 的调用正在修改 Calendar 对象的时区,因为它跨越了标准时间/夏令时边界。
如果是这种情况,您可以尝试添加 Calendar.DAY,1 或简单地添加 Calendar 对象的 .setTimeZone(...) 。 .add 调用后返回原始时区。
关于java - Java日历日期,2014-02-16跳过一小时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20387263/