pdhinoja
pdhinoja

Reputation: 95

problems with java.util.Calendar and timeZones

I have following method which convert my custom DMY (date,month,year) object to Date.

public static Date serverCreateDateFromDMY(DMY pDMY, TimeZone pTimeZone)
    {
        Calendar vCalendar = Calendar.getInstance(pTimeZone);
        vCalendar.set(Calendar.YEAR, pDMY.getYear());
            // Below line is because DMY month counts are 1-indexed
            // and Date month counts are 0-indexed
        vCalendar.set(Calendar.MONTH, pDMY.getMonthOfYear() - 1);
        vCalendar.set(Calendar.DAY_OF_MONTH, pDMY.getDayOfMonth());
        System.out.println(vCalendar.getTime());
        TimeUtilsServer.zeroCalendarHoursAndBelow(vCalendar);
        System.out.println(vCalendar.getTime());
        return vCalendar.getTime();
    }


public static void zeroCalendarHoursAndBelow(Calendar pToZero)
    {
        pToZero.set(Calendar.HOUR_OF_DAY, 0);
        pToZero.set(Calendar.MINUTE, 0);
        pToZero.set(Calendar.SECOND, 0);
        pToZero.set(Calendar.MILLISECOND, 0);
    }

to serverCreateDateFromDMY() method, I am passing these arguments : DMY=20120424, and TimeZone is : America/New_York. Application is running locally in my timezone which is IST.

based in above inputs, following output is printed.

Tue Apr 24 14:43:07 IST 2012

Tue Apr 24 09:30:00 IST 2012

so as you see that in last output time is not zeroed out. any suggestions please?

@Marko, yes I come to know about DateFormat and I tried following example. but still date is printed with time and not zeroing out.

TimeZone tz = TimeZone.getTimeZone("America/New_York");
        Calendar vCalendar = Calendar.getInstance(tz);
        vCalendar.set(Calendar.YEAR, 2012);

        vCalendar.set(Calendar.MONTH, 4 - 1);
        vCalendar.set(Calendar.DAY_OF_MONTH, 24);
        DateFormat df = DateFormat.getDateTimeInstance();
        df.setTimeZone(tz); 
        System.out.println(df.format(vCalendar.getTime()));

         vCalendar.set(Calendar.HOUR_OF_DAY,     vCalendar.getActualMinimum(Calendar.HOUR_OF_DAY));
        vCalendar.set(Calendar.MINUTE, vCalendar.getActualMinimum(Calendar.MINUTE));
        vCalendar.set(Calendar.SECOND, vCalendar.getActualMinimum(Calendar.SECOND));
        vCalendar.set(Calendar.MILLISECOND,   vCalendar.getActualMinimum(Calendar.MILLISECOND));
        System.out.println(df.format(vCalendar.getTime()));

Upvotes: 2

Views: 1338

Answers (3)

Rens Verhage
Rens Verhage

Reputation: 5857

Internally, Date and Calendar objects are stored in UTC. When you set the fields to 0, the Calendar is updated in UTC.

When you ask the Calendar for the time, it then converts the Date to your desired Timezone, hence the difference.

Upvotes: 2

Marko Topolnik
Marko Topolnik

Reputation: 200138

... and you are 9:30h ahead of NY time. You set the time to midnight NY time and read it out as time in your zone. Note that getTime returns a Date, which is not timezone-configurable. You'll need DateFormat if you want to specify the timezone for which you print the result.

Upvotes: 1

Bax
Bax

Reputation: 4466

java Date / Time API have a bad design from the time of its creation. Maybe you should take a look at some library - for example this which hides JDK API deficiencies - http://joda-time.sourceforge.net/

Upvotes: 2

Related Questions