Java日历/时间戳添加问题

时间:2017-01-31 19:38:51

标签: java calendar timezone

我试图在java.sql.Timestamp上连续添加4个小时。 代码如下:

Timestamp period = Timestamp.valueOf("2011-10-01 00:00:00");
Timestamp finish = Timestamp.valueOf("2011-12-31 00:00:00");

Calendar calendar = Calendar.getInstance();

while (period.before(finish)) {
    calendar.setTimeInMillis(period.getTime());
    calendar.add(Calendar.HOUR_OF_DAY, 4);
    period.setTime(calendar.getTimeInMillis());
    System.out.println(period);
}

输出应该是一个时间戳列表,如:

2011-10-02 00:00:00.0
2011-10-02 04:00:00.0
2011-10-02 08:00:00.0
2011-10-02 12:00:00.0
2011-10-02 16:00:00.0
2011-10-02 20:00:00.0
2011-10-03 00:00:00.0
2011-10-03 04:00:00.0
2011-10-03 08:00:00.0
2011-10-03 12:00:00.0
2011-10-03 16:00:00.0
2011-10-03 20:00:00.0
2011-10-04 00:00:00.0

一直工作到2011-10-30,其中3小时而不是4:

2011-10-29 08:00:00.0
2011-10-29 12:00:00.0
2011-10-29 16:00:00.0
2011-10-29 20:00:00.0
2011-10-30 00:00:00.0
2011-10-30 03:00:00.0
2011-10-30 07:00:00.0
2011-10-30 11:00:00.0
2011-10-30 15:00:00.0
2011-10-30 19:00:00.0

由于

0 个答案:

没有答案