This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Java in General and the fly likes Calendar.HOUR for another timezone Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Calendar.HOUR for another timezone" Watch "Calendar.HOUR for another timezone" New topic

Calendar.HOUR for another timezone

ganesh gowthaman

Joined: Nov 25, 2009
Posts: 3
Hi there,




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?
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074

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

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
ganesh gowthaman

Joined: Nov 25, 2009
Posts: 3
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!!!

Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074

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

If I run this, I get:


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.
I agree. Here's the link:
subject: Calendar.HOUR for another timezone
Similar Threads
Problem while starting websphere portalserver 6.0 (log file attached)
Timezone Issues
Calendar returns wrong date?
how to get the date from the string representation of date
Daylight Saving Time in Java5