• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Calendar.HOUR for another timezone

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic