0

When I add or subtract hours it seems the Calendar object has some unexpected behaviour (at least I think so)

Can someone explain this, I first add 2 hours after that I subtract 3 hours, so my time should be 1 less then were i started at. What am I thinking wrong here?:

    Calendar calReference= new GregorianCalendar(2014,9,26);    
    sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
    System.out.println("test dummy "+sdf.format(calReference.getTime()));
    calReference.add(Calendar.HOUR_OF_DAY, 2);
    //calReference.set(Calendar.MINUTE, 0);
    System.out.println("test dummy "+sdf.format(calReference.getTime()));
    calReference.add(Calendar.HOUR_OF_DAY, -3);
    //calReference.set(Calendar.MINUTE, 0);
    System.out.println("test dummy "+sdf.format(calReference.getTime()));

I got output I never expected myself:

test dummy 2014/10/26 00:00:00

test dummy 2014/10/26 02:00:00

test dummy 2014/10/26 00:00:00 => this should be 2014/10/25 23:00:00.

or am I missing something here myself.

  • It sounds like your Locale has Daylight Savings Time a.k.a. Summer Time ending on October 26th. Here in California, USA, I get `test dummy 2014/10/25 23:00:00`, but DST didn't end here until November 2nd. Perhaps this is an edge case. – rgettman Nov 17 '14 at 18:21
  • What is your local time zone? As rgettman mentioned it may have to do with DST. In Europe it was in the night of Octobre 26th from 02:00 to 03:00 – wastl Nov 17 '14 at 18:26
  • Even if daylight savings is responsible, this should only happen if daylight savings ended with the clock being set back from 1am to midnight. Where I come from, we don't do this - our daylight savings always changes 3am to 2am or vice versa. But if this is what's happening, then "clock midnight" would have occurred twice. You could check if that's what's happening by printing the timezone along with the time. – Dawood ibn Kareem Nov 17 '14 at 18:48

1 Answers1

0

Try adding your desired locale into the SimpleDateFormat:

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

The Java documentation for Locale.getDefault() states:

The Java Virtual Machine sets the default locale during startup based on the host environment. It is used by many locale-sensitive methods if no locale is explicitly specified. It can be changed using the setDefault(Locale.Category, Locale) method.

So the default locale in your JVM may be incorrect.

This post delves a little deeper into how the default locale for your application is determined.

Community
  • 1
  • 1
Alexander Kohler
  • 1,907
  • 16
  • 23
  • Thx, to add the Locale, did the trick. At this moment I still don't know why. But it works. I have to look into this. – Marco Scholten Nov 17 '14 at 19:01
  • I've had similar problems with locales and dates, take a look at the post I linked, it seems like it's a matter of your JVM having the 'wrong' default locale. – Alexander Kohler Nov 17 '14 at 19:05
  • My Locale seem to be en_US. If I set it to any other as available, it seems to work..Just cannot explain why yet. I will look into your post thx – Marco Scholten Nov 17 '14 at 19:09
  • Hi add an subtract hours were fixed by this locale, but I have a new case. Maybe you have a suggestion for this too. When I add 2 hours from 00:00:00 at 2014-10-26 I get 2:00. So far so good. But when I add 3 hours to 00:00:00 I also get 2:00. Think it is indeed winter or summer time. – Marco Scholten Nov 17 '14 at 20:00