# Calendar.HOUR for another timezone

ganesh gowthaman

Greenhorn

Posts: 3

posted 6 years ago

Hi there,

System.out.println(Calendar.getInstance(TimeZone.getTimeZone("EST")));

gives

java.util.GregorianCalendar[time=1271917105506,areFieldsSet=true,areAllFieldsSet=true,lenient=true,zone=sun.util.calendar.ZoneInfo[id="EST",offset=-18000000,dstSavings=0,useDaylight=false,transitions=0,lastRule=null],firstDayOfWeek=1,minimalDaysInFirstWeek=1,ERA=1,YEAR=2010,MONTH=3,WEEK_OF_YEAR=17,WEEK_OF_MONTH=4,DAY_OF_MONTH=22,DAY_OF_YEAR=112,DAY_OF_WEEK=5,DAY_OF_WEEK_IN_MONTH=4,AM_PM=0,HOUR=1,HOUR_OF_DAY=1,MINUTE=18,SECOND=25,MILLISECOND=506,ZONE_OFFSET=-18000000,DST_OFFSET=0]

Current time is Thu Apr 22 16:18:25 EST 2010

But Calendar.HOUR_OF_DAY return 1

Looks like something to do with OFFSET, GMT...

I want to schedule a task when the clock strikes 10AM next.

I know i should use add or set of Calendar. But how to get the correct current hours?

Any help please?

System.out.println(Calendar.getInstance(TimeZone.getTimeZone("EST")));

gives

java.util.GregorianCalendar[time=1271917105506,areFieldsSet=true,areAllFieldsSet=true,lenient=true,zone=sun.util.calendar.ZoneInfo[id="EST",offset=-18000000,dstSavings=0,useDaylight=false,transitions=0,lastRule=null],firstDayOfWeek=1,minimalDaysInFirstWeek=1,ERA=1,YEAR=2010,MONTH=3,WEEK_OF_YEAR=17,WEEK_OF_MONTH=4,DAY_OF_MONTH=22,DAY_OF_YEAR=112,DAY_OF_WEEK=5,DAY_OF_WEEK_IN_MONTH=4,AM_PM=0,HOUR=1,HOUR_OF_DAY=1,MINUTE=18,SECOND=25,MILLISECOND=506,ZONE_OFFSET=-18000000,DST_OFFSET=0]

Current time is Thu Apr 22 16:18:25 EST 2010

But Calendar.HOUR_OF_DAY return 1

Looks like something to do with OFFSET, GMT...

I want to schedule a task when the clock strikes 10AM next.

I know i should use add or set of Calendar. But how to get the correct current hours?

Any help please?

posted 6 years ago

You're not saying it explicitly, but I suspect that your code looks like this:

Note that that's not the correct way to use Calendar.HOUR_OF_DAY. In fact, HOUR_OF_DAY is a constant in class Calendar that you should pass to the get() method, for example:

So, HOUR_OF_DAY is not the actual hour, it's just a "label" that you can use with the get() method (and other methods).

ganesh gowthaman wrote:But Calendar.HOUR_OF_DAY return 1

You're not saying it explicitly, but I suspect that your code looks like this:

Note that that's not the correct way to use Calendar.HOUR_OF_DAY. In fact, HOUR_OF_DAY is a constant in class Calendar that you should pass to the get() method, for example:

So, HOUR_OF_DAY is not the actual hour, it's just a "label" that you can use with the get() method (and other methods).

ganesh gowthaman

Greenhorn

Posts: 3

posted 6 years ago

I am sorry..

I meant

Calendar c = Calendar.getInstance(TimeZone.getTimeZone("EST"));

c.get(Calendar.HOUR_OF_DAY) returns 1 for the above scenario where the actual hour (as per the current timezone which is EST) is 16 and it returned 0 when the time was 15hours.

My question is what math will help me to get 16 from 1 or 15 from 0?

I hope i am clear!!!

I meant

Calendar c = Calendar.getInstance(TimeZone.getTimeZone("EST"));

c.get(Calendar.HOUR_OF_DAY) returns 1 for the above scenario where the actual hour (as per the current timezone which is EST) is 16 and it returned 0 when the time was 15hours.

My question is what math will help me to get 16 from 1 or 15 from 0?

I hope i am clear!!!

posted 6 years ago

Can you post the exact source code that you use to test this? I wrote this small program:

If I run this, I get:

java.util.GregorianCalendar[time=1271936988190,areFieldsSet=true,areAllFieldsSet=true,lenient=true,zone=sun.util.calendar.ZoneInfo[id="EST",offset=-18000000,dstSavings=0,useDaylight=false,transitions=0,lastRule=null],

firstDayOfWeek=2,minimalDaysInFirstWeek=4,ERA=1,YEAR=2010,MONTH=3,WEEK_OF_YEAR=16,WEEK_OF_MONTH=4,DAY_OF_MONTH=22,DAY_OF_YEAR=112,DAY_OF_WEEK=5,DAY_OF_WEEK_IN_MONTH=4,AM_PM=0,HOUR=6,HOUR_OF_DAY=6,MINUTE=49,SECOND=48,

MILLISECOND=190,ZONE_OFFSET=-18000000,DST_OFFSET=0]

Hour of day: 6

Note that in the long string I also see HOUR_OF_DAY=6, so that's consistent with what I get from cal.get(Calendar.HOUR_OF_DAY). (Note, the local time here is 13:52, so 6:52 in EST should be right).

If I look at your output, in the long line I also see HOUR_OF_DAY=1 so it looks right that get(Calendar.HOUR_OF_DAY) returns 1 for your case.

If I run this, I get:

java.util.GregorianCalendar[time=1271936988190,areFieldsSet=true,areAllFieldsSet=true,lenient=true,zone=sun.util.calendar.ZoneInfo[id="EST",offset=-18000000,dstSavings=0,useDaylight=false,transitions=0,lastRule=null],

firstDayOfWeek=2,minimalDaysInFirstWeek=4,ERA=1,YEAR=2010,MONTH=3,WEEK_OF_YEAR=16,WEEK_OF_MONTH=4,DAY_OF_MONTH=22,DAY_OF_YEAR=112,DAY_OF_WEEK=5,DAY_OF_WEEK_IN_MONTH=4,AM_PM=0,HOUR=6,HOUR_OF_DAY=6,MINUTE=49,SECOND=48,

MILLISECOND=190,ZONE_OFFSET=-18000000,DST_OFFSET=0]

Hour of day: 6

Note that in the long string I also see HOUR_OF_DAY=6, so that's consistent with what I get from cal.get(Calendar.HOUR_OF_DAY). (Note, the local time here is 13:52, so 6:52 in EST should be right).

If I look at your output, in the long line I also see HOUR_OF_DAY=1 so it looks right that get(Calendar.HOUR_OF_DAY) returns 1 for your case.