tags:

views:

7105

answers:

4

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

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

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

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

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.

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) {}
+10  A: 

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

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".

Chris Jester-Young
Aha - great minds think alike - +1 :-)
toolkit
Hehehe, yep! Anyway, my comment about New Zealand is this, as of last year daylight savings was moved from first Sunday of October to last Sunday of September. Since Java 5 has been around a while, the zone files may indeed need updating.
Chris Jester-Young
+3  A: 

Could this be daylight savings kicking in?

toolkit
A: 

thanks, yep it is a daylight savings quirk....i didn't recognise the daylight savings change over dates until i ran the JDK DST Timezone Update Tool

A: 

Take a look at Joda-Time.

From the Documentation:

"Joda-Time has been created to radically change date and time handling in Java. The JDK classes Date and Calendar are very badly designed, have had numerous bugs and have odd performance effects."

Willi aus Rohr