'TestHour'.
You call a bunch of methods from inside the constructor, ergo the object
hasn't finished construction when you call methods like
'getCurrentHour12HourClock()'. It is better (usually) to let an object finish
construction before it does anything useful.
Okay, I see your point now. (Well, actually it was Eric's point and
you are clarifying it.) But I'm not clear on what I have to do
differently. How do I know the object has finished construction so
that I know it is safe to use it? In other words, how should I code it
differently.
For what it's worth, your point is well taken that I should be using
DateFormat and SimpleDateFormat. I'm actually rewriting my methods to
use that approach as I wait for replies to my problem. I think I
originally wrote my DateTimeUtils class before I knew about DateFormat
and SimpleDateFormat so my code is way overdue to be brought up to
date anyway.
(TAB characters removed for readability.)
And please, please, please, please, please use narrower indentation for Usenet
code posts - a maximum of four spaces per indent level. It makes the code so
hard to read when you use such wide indentation. Stop using TAB characters
for indentation.
Sorry about that! The tabs just look like spaces and its hard to find
them all.
The actual output still has a marginal chance of being useful to those of us
not there who attempt to help from here. Every time you redact information
you risk losing the piece that solves the puzzle.
Okay, I'll include that as well.
Also for what it's worth, when I wrote the code using
SimpleDateFormat, the hour is still one hour later than it should be.
Now for some new output.
I've modified TestHour slightly to display the values you've
requested. I modified the three methods along these lines and added a
single instance variable, DEBUG which is a boolean set to true.
public int getCurrentHour12HourClock() {
/* Get the current date, then the current hour (12 hour clock). */
GregorianCalendar now = new GregorianCalendar();
if (DEBUG) {
System.out.println("now.toString(): " + now.toString());
System.out.println("Calendar.getInstance().toString(): " +
Calendar.getInstance().toString());
}
return now.get(Calendar.HOUR);
}
Each of the three methods were modified in exactly the same way with
the exact same if (DEBUG) and the exact same printlin() statements in
the exact same position within the method.
This is the output from running TestHour, INCLUDING all the output
from the new println() statements.
--------------------------------------------
now.toString():
java.util.GregorianCalendar[time=1269135963921,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=2010,MONTH=2,WEEK_OF_YEAR=12,WEEK_OF_MONTH=3,DAY_OF_MONTH=20,DAY_OF_YEAR=79,DAY_OF_WEEK=7,DAY_OF_WEEK_IN_MONTH=3,AM_PM=1,HOUR=9,HOUR_OF_DAY=21,MINUTE=46,SECOND=3,MILLISECOND=921,ZONE_OFFSET=-18000000,DST_OFFSET=3600000]
Calendar.getInstance().toString():
java.util.GregorianCalendar[time=1269135963937,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=2010,MONTH=2,WEEK_OF_YEAR=12,WEEK_OF_MONTH=3,DAY_OF_MONTH=20,DAY_OF_YEAR=79,DAY_OF_WEEK=7,DAY_OF_WEEK_IN_MONTH=3,AM_PM=1,HOUR=9,HOUR_OF_DAY=21,MINUTE=46,SECOND=3,MILLISECOND=937,ZONE_OFFSET=-18000000,DST_OFFSET=3600000]
The current hour on the 12 hour clock is: 9.
now.toString():
java.util.GregorianCalendar[time=1269135963937,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=2010,MONTH=2,WEEK_OF_YEAR=12,WEEK_OF_MONTH=3,DAY_OF_MONTH=20,DAY_OF_YEAR=79,DAY_OF_WEEK=7,DAY_OF_WEEK_IN_MONTH=3,AM_PM=1,HOUR=9,HOUR_OF_DAY=21,MINUTE=46,SECOND=3,MILLISECOND=937,ZONE_OFFSET=-18000000,DST_OFFSET=3600000]
Calendar.getInstance().toString():
java.util.GregorianCalendar[time=1269135963937,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=2010,MONTH=2,WEEK_OF_YEAR=12,WEEK_OF_MONTH=3,DAY_OF_MONTH=20,DAY_OF_YEAR=79,DAY_OF_WEEK=7,DAY_OF_WEEK_IN_MONTH=3,AM_PM=1,HOUR=9,HOUR_OF_DAY=21,MINUTE=46,SECOND=3,MILLISECOND=937,ZONE_OFFSET=-18000000,DST_OFFSET=3600000]
The current hour on the 24 hour clock is: 21.
Calendar:
java.util.GregorianCalendar[time=1269135963937,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=2010,MONTH=2,WEEK_OF_YEAR=12,WEEK_OF_MONTH=3,DAY_OF_MONTH=20,DAY_OF_YEAR=79,DAY_OF_WEEK=7,DAY_OF_WEEK_IN_MONTH=3,AM_PM=1,HOUR=9,HOUR_OF_DAY=21,MINUTE=46,SECOND=3,MILLISECOND=937,ZONE_OFFSET=-18000000,DST_OFFSET=3600000]
now.toString():
java.util.GregorianCalendar[time=1269135963937,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=2010,MONTH=2,WEEK_OF_YEAR=12,WEEK_OF_MONTH=3,DAY_OF_MONTH=20,DAY_OF_YEAR=79,DAY_OF_WEEK=7,DAY_OF_WEEK_IN_MONTH=3,AM_PM=1,HOUR=9,HOUR_OF_DAY=21,MINUTE=46,SECOND=3,MILLISECOND=937,ZONE_OFFSET=-18000000,DST_OFFSET=3600000]
Calendar.getInstance().toString():
java.util.GregorianCalendar[time=1269135963937,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=2010,MONTH=2,WEEK_OF_YEAR=12,WEEK_OF_MONTH=3,DAY_OF_MONTH=20,DAY_OF_YEAR=79,DAY_OF_WEEK=7,DAY_OF_WEEK_IN_MONTH=3,AM_PM=1,HOUR=9,HOUR_OF_DAY=21,MINUTE=46,SECOND=3,MILLISECOND=937,ZONE_OFFSET=-18000000,DST_OFFSET=3600000]
The current time is: 21:46:03.
--------------------------------------------
As you see, the hour is now indicated as 9/21 but it is still not
quite 9 PM here so the hour is still one hour later than the actual
time.
I've also rewritten the routines along the lines you suggested with
DateFormat and SimpleDateFormat and now present TestHour2:
-------------------------------------------
import java.text.SimpleDateFormat;
import java.util.Date;
public class TestHour2 {
final static boolean DEBUG = true;
public static void main(String[] args) {
new TestHour2();
}
public TestHour2() {
System.out.println("The current hour on the 12 hour clock is: " +
getCurrentHour12HourClock() + ".\n");
System.out.println("The current hour on the 24 hour clock is: " +
getCurrentHour24HourClock() + ".\n");
System.out.println("The current time is: " + getCurrentTime() + ".
\n");
}
public int getCurrentHour12HourClock() {
Date now = new Date();
SimpleDateFormat hourFormat = new SimpleDateFormat("h");
String strCurrentHour = hourFormat.format(now).toString();
int currentHour = 0;
try {
currentHour = Integer.parseInt(strCurrentHour.trim());
}
catch (NumberFormatException nf_excp) {
throw new NumberFormatException("The current hour is not an
integer.");
}
return(currentHour);
}
public int getCurrentHour24HourClock() {
Date now = new Date();
SimpleDateFormat hourFormat = new SimpleDateFormat("H");
String strCurrentHour = hourFormat.format(now).toString();
int currentHour = 0;
try {
currentHour = Integer.parseInt(strCurrentHour.trim());
}
catch (NumberFormatException nf_excp) {
throw new NumberFormatException("The current hour is not an
integer.");
}
return(currentHour);
}
public String getCurrentTime() {
Date now = new Date();
SimpleDateFormat timeFormat = new SimpleDateFormat("hh:mm:ss");
String strCurrentTime = timeFormat.format(now).toString();
return(strCurrentTime);
}
}
-------------------------------------------
The output from this version of the date routines is, as you might
have guessed:
-------------------------------------------
The current hour on the 12 hour clock is: 9.
The current hour on the 24 hour clock is: 21.
The current time is: 09:58:06.
-------------------------------------------
In other words, rewriting the methods to use better techniques still
causes the hour to be one hour later than it should be.
Have we proven now that the problem is with the OS or is there still a
chance that this is a Java issue?