Day Light Saving Issue with Java Calendar

This is regarding Java Calendar and the effects we encountered after today day light saving change in Toronto.

Below is the code

    Date date = new Date(); //Sun Mar 11 00:00:00 EST 2018
    Integer time = 349;

    Calendar scheduleDateCal = Calendar.getInstance();

    scheduleDateCal.set(Calendar.MINUTE, 0);
    scheduleDateCal.set(Calendar.HOUR_OF_DAY, 0);
    String strSchAdminTime = String.valueOf(time);
    Integer schAdminMinute = time;

    if (strSchAdminTime.length() >= 2) {
        schAdminMinute = Integer.valueOf(strSchAdminTime.substring(strSchAdminTime.length()-2));
        Integer schAdminHour = Integer.valueOf(strSchAdminTime.substring(0,strSchAdminTime.length()-2));
        scheduleDateCal.add(Calendar.HOUR_OF_DAY, schAdminHour);
        scheduleDateCal.add(Calendar.HOUR_OF_DAY, 0);
    scheduleDateCal.add(Calendar.MINUTE, schAdminMinute);


I know this code hasn't done with the best practises however I need to maintain it for the current release. In here it uses integer to represent the time portion and later there is a logic to extract the hours and minutes from it.

When I followed the logic, the hour portion is 3. Then there is a logic to add this time to Calendar object with value 'Sun Mar 11 00:00:00 EST 2018' with below statement

scheduleDateCal.add(Calendar.HOUR_OF_DAY, schAdminHour);

Theoretically after this calculation, the calendar object should have value "Sun Mar 11 03:00:00 EDT 2018". However it returns "Sun Mar 11 04:00:00 EDT 2018" I know starting from today the time will go one hour ahead with the daylight saving. Can any one please help me to understand this

Appreciate the help.

2 answers

  • answered 2018-03-11 12:58 alexq

    EST is -5, EDT is -4, so you get 1 hour when you are calling add(). You can use scheduleDateCal.set(Calendar.HOUR_OF_DAY, schAdminHour) and scheduleDateCal.set(Calendar.MINUTE, schAdminMinute), if you need result in different timezone.

  • answered 2018-03-11 12:58 Ole V.V.

    At 00:00:00 today (Sunday March 11, 2018) summer time (DST) was not yet in effect, so that time was correctly rendered as Sun Mar 11 00:00:00 EST 2018 (EST for Eastern Standard Time). Date.toString chooses between EST and EDT based on the time contained in the Date object (not based on the time the toString method is called). When you add 3 hours to that time, you cross the time at 2 when the clock was turned forward to 3. So 3 hours after your start time the time is 04:00:00 EDT (EDT for Eastern Daylight Time).

    PS Modern code

    PS In case you or someone else is interested, here is the modern — both simpler and shorter — version of your code. To set the time to 03:49:

        int time = 349;
        ZoneId zone = ZoneId.of("America/Toronto");
        ZonedDateTime scheduledDateTime =
                .atTime(time / 100, time % 100)

    Today this printed


    Still better, of course, if you can get completely rid of representing 03:49 as the integer value 349. To use your JVM’s time zone setting you may set zone to ZoneId.systemDefault(). This is fragile because the setting may be changed at any time by other parts of your program or other programs running in the same JVM.

    To set the time to 3 hours 49 minutes after midnight (which with DST transition isn’t the same thing, as you have seen):

        ZonedDateTime scheduledDateTime =
                .plusHours(time / 100)
                .plusMinutes(time % 100);

    This time I got