Annoying Calendar question

T

Tobi

My apologies in advance...

I was doing what I thought should be fairly straightforward, creating
a Calendar like below:

protected boolean sendTime(){
boolean sendTime = false;

Calendar currentHoursCal = new GregorianCalendar();
int hour = currentHoursCal.HOUR_OF_DAY;
int hour2 = currentHoursCal.HOUR;

System.out.println("Calendar value: " + currentHoursCal);

System.out.println("currentHoursCal.HOUR_OF_DAY: " +
currentHoursCal.HOUR_OF_DAY);
System.out.println("currentHoursCal.HOUR: " +
currentHoursCal.HOUR);
if(hour > 23) {
sendTime = true;
}
return sendTime;
}

When I run this I see:

Calendar value:
java.util.GregorianCalendar[time=1213210518817,areFieldsSet=true,areAllFieldsSet=true,lenient=true,zone=sun.util.calendar.ZoneInfo[id="America/
New_York",offset=-18000000,dstSavings=3600000,useDaylight=true,transitions=235,lastRule=java.util.SimpleTimeZone[id=America/
New_York,offset=-18000000,dstSavings=3600000,useDaylight=true,startYear=0,startMode=3,startMonth=2,startDay=8,startDayOfWeek=1,startTime=7200000,startTimeMode=0,endMode=3,endMonth=10,endDay=1,endDayOfWeek=1,endTime=7200000,endTimeMode=0]],firstDayOfWeek=1,minimalDaysInFirstWeek=1,ERA=1,YEAR=2008,MONTH=5,WEEK_OF_YEAR=24,WEEK_OF_MONTH=2,DAY_OF_MONTH=11,DAY_OF_YEAR=163,DAY_OF_WEEK=4,DAY_OF_WEEK_IN_MONTH=2,AM_PM=1,HOUR=2,HOUR_OF_DAY=14,MINUTE=55,SECOND=18,MILLISECOND=817,ZONE_OFFSET=-18000000,DST_OFFSET=3600000]
currentHoursCal.HOUR_OF_DAY: 11
currentHoursCal.HOUR: 10

Which seems strange because the HOUR_OF_DAY in the Calendar object is
set 14, not 11. Likewise, the HOUR property is 2, not 10, as shown
above.

Am I not seeing something obvious?

Thanks
 
J

Joshua Cranmer

Tobi said:
int hour = currentHoursCal.HOUR_OF_DAY;
int hour2 = currentHoursCal.HOUR;

From the Javadocs:
HOUR_OF_DAY

public static final int HOUR_OF_DAY

Field number for get and set indicating the hour of the day.
HOUR_OF_DAY is used for the 24-hour clock. E.g., at 10:04:15.250 PM the
HOUR_OF_DAY is 22.

See Also:
HOUR, Constant Field Values


I'm sure you don't want to be getting the static field, but instead you
want to be doing:

int hour = currentHoursCal.get(Calendar.HOUR_OF_DAY);
HOUR_OF_DAY=14
currentHoursCal.HOUR_OF_DAY: 11
currentHoursCal.HOUR: 10
Which seems strange because the HOUR_OF_DAY in the Calendar object is
set 14, not 11. Likewise, the HOUR property is 2, not 10, as shown
above.

The toString of Calendar (also isn't reliable) is obviously using the
HOUR_OF_DAY to represent the internal hour of day variable. When you
manually ask for the HOUR_OF_DAY, you're asking for the constant static
value defined, not the instance's hour of day.
Am I not seeing something obvious?

Capital letters almost always indicate some sort of compile-time
constant; it would be extremely bad practice to name a variable that,
and it is almost as bad to have such a field be public even.

You can also read the Javadocs to figure out your conundrum:
<http://java.sun.com/javase/6/docs/api/java/util/Calendar.html>
 
R

Roland de Ruiter

My apologies in advance...

I was doing what I thought should be fairly straightforward, creating
a Calendar like below:
[snip]
currentHoursCal.HOUR_OF_DAY: 11
currentHoursCal.HOUR: 10

Which seems strange because the HOUR_OF_DAY in the Calendar object is
set 14, not 11. Likewise, the HOUR property is 2, not 10, as shown
above.

Am I not seeing something obvious?

Thanks

You're just printing the values of the field constants
Use

currentHoursCal.set(Calendar.HOUR_OF_DAY, 14)
currentHoursCal.get(Calendar.MINUTE)
etc.
 
T

Tobi

My apologies in advance...
I was doing what I thought should be fairly straightforward, creating
a Calendar like below:
[snip]
currentHoursCal.HOUR_OF_DAY: 11
currentHoursCal.HOUR: 10
Which seems strange because the HOUR_OF_DAY in the Calendar object is
set 14, not 11. Likewise, the HOUR property is 2, not 10, as shown
above.
Am I not seeing something obvious?

You're just printing the values of the field constants
Use

currentHoursCal.set(Calendar.HOUR_OF_DAY, 14)
currentHoursCal.get(Calendar.MINUTE)
etc.

Hi-

Thanks for the response. Basically, I'm just trying to get the
current hour.

When I fixed to say:

int hour = currentHoursCal.get(Calendar.HOUR_OF_DAY);

the value of hour is still 11-strange because my system time is 15:27.

Tobi
 
L

Lew

Tobi said:
When I fixed to say:

int hour = currentHoursCal.get(Calendar.HOUR_OF_DAY);

the value of hour is still 11-strange because my system time is 15:27.

Calendar is time-zone aware.
SimpleTimeZone [id=America/New_York,offset=-18000000,dstSavings=3600000,useDaylight=true[/QUOTE]

You are in the Eastern Daylight Time zone. That's four hours back
from UTC, so the Calendar is taking 15:27 as Zulu time and factoring
it back to "New York" time, 11:27.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,060
Latest member
BuyKetozenseACV

Latest Threads

Top