Java,添加几分钟到一个Date,奇怪的异常 [英] Java , adding minutes to a Date , weird anomaly

查看:107
本文介绍了Java,添加几分钟到一个Date,奇怪的异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Windows和Ubuntu Linux上使用Java版本1.5.0_06:

With Java Version 1.5.0_06 on both Windows and Ubuntu Linux :

每当我在2008/10/05 00:00:00中添加分钟时, ,似乎加了一个额外的小时。

Whenever I add minutes to the date "2008/10/05 00:00:00" , it seems that an extra hour is wrongly added.

ie:添加360分钟到2008/10/05 00:00:00在午夜应该到2008/10 / 05 06:00:00

ie: adding 360 minutes to 2008/10/05 00:00:00 at midnight should arrive at 2008/10/05 06:00:00

但是到2008/10/05 07:00:00

But it is arriving at 2008/10/05 07:00:00

完全令人困惑的是,这个 ONLY 发生在当天是2008/10/05,所有其他日子,我尝试执行分钟正确添加。

The totally perplexing thing is that this ONLY happens when the day is 2008/10/05, all other days that I try perform the minutes addition correctly.

我是疯了还是Java中的错误?

Am I going crazy or is this a bug in Java ?

    SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");

    try {
        String date = "2008/10/05 00:00:00";
        int minutesToAdd = 360;  // 6 hrs

        Calendar cal = Calendar.getInstance();
        cal.setTime(sdf.parse(date));
        cal.add(Calendar.MINUTE, minutesToAdd);
        System.out.println(cal.getTime());

    } catch (ParseException e) {}


推荐答案

那天有一个交叉的夏令时。

There's a crossover to daylight savings on that day.

你在新西兰吗?如果是这样,那意味着你的时区文件已经过时了。更好的去下载Java下载网站,下载新的;寻找JDK DST时区更新工具。

Are you in New Zealand? If so, that means your timezone files are out of date. Better go to the Java download site and download new ones; look for "JDK DST Timezone Update Tool".

这篇关于Java,添加几分钟到一个Date,奇怪的异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆