R
Rhino
I'm just retesting some date/time methods I wrote a while back and
noticed something odd. It's 8 PM Eastern time as I write this and the
date routines I have just retested tell me that it's actually 9 PM. Why
would that be?
I'm guessing it has something to do with Java date routines not correctly
handling the earlier changeover to daylight savings time that started a
few years back. If that's right, how should I be calculating the hour or
the time?
Or have I got something wrong in my computer somewhere? I'm running XP
and the system clock says it's 8 PM Eastern time and it is set to
recognize Daylight Saving Time. But I may not have installed the update
that handles the earlier changeover to Daylight Time. Perhaps that needs
to be installed??
Here are the methods I'm using to get the current hour:
public int getCurrentHour12HourClock() {
/* Get the current date, then the current hour (12 hour clock). */
GregorianCalendar now = new GregorianCalendar();
return now.get(Calendar.HOUR);
}
public int getCurrentHour24HourClock() {
/* Get the current date, then the current hour (24 hour clock). */
GregorianCalendar now = new GregorianCalendar();
return now.get(Calendar.HOUR_OF_DAY);
}
Here is the method I'm using to get the current time:
public String getCurrentTime() {
/* Get the current time. */
GregorianCalendar now = new GregorianCalendar();
int intCurrentHour = now.get(Calendar.HOUR_OF_DAY);
int intCurrentMinute = now.get(Calendar.MINUTE);
int intCurrentSecond = now.get(Calendar.SECOND);
StringUtils stringUtils = StringUtils.getInstance();
String strCurrentHour = stringUtils.pad(intCurrentHour, '0', 'L',
2);
String strCurrentMinute = stringUtils.pad(intCurrentMinute, '0',
'L', 2);
String strCurrentSecond = stringUtils.pad(intCurrentSecond, '0',
'L', 2);
/* Construct the string representing the current time. */
return strCurrentHour + ":" + strCurrentMinute + ":" +
strCurrentSecond; //$NON-NLS-1$ //$NON-NLS-2$
}
All of these methods are displaying one hour later than it actually is.
noticed something odd. It's 8 PM Eastern time as I write this and the
date routines I have just retested tell me that it's actually 9 PM. Why
would that be?
I'm guessing it has something to do with Java date routines not correctly
handling the earlier changeover to daylight savings time that started a
few years back. If that's right, how should I be calculating the hour or
the time?
Or have I got something wrong in my computer somewhere? I'm running XP
and the system clock says it's 8 PM Eastern time and it is set to
recognize Daylight Saving Time. But I may not have installed the update
that handles the earlier changeover to Daylight Time. Perhaps that needs
to be installed??
Here are the methods I'm using to get the current hour:
public int getCurrentHour12HourClock() {
/* Get the current date, then the current hour (12 hour clock). */
GregorianCalendar now = new GregorianCalendar();
return now.get(Calendar.HOUR);
}
public int getCurrentHour24HourClock() {
/* Get the current date, then the current hour (24 hour clock). */
GregorianCalendar now = new GregorianCalendar();
return now.get(Calendar.HOUR_OF_DAY);
}
Here is the method I'm using to get the current time:
public String getCurrentTime() {
/* Get the current time. */
GregorianCalendar now = new GregorianCalendar();
int intCurrentHour = now.get(Calendar.HOUR_OF_DAY);
int intCurrentMinute = now.get(Calendar.MINUTE);
int intCurrentSecond = now.get(Calendar.SECOND);
StringUtils stringUtils = StringUtils.getInstance();
String strCurrentHour = stringUtils.pad(intCurrentHour, '0', 'L',
2);
String strCurrentMinute = stringUtils.pad(intCurrentMinute, '0',
'L', 2);
String strCurrentSecond = stringUtils.pad(intCurrentSecond, '0',
'L', 2);
/* Construct the string representing the current time. */
return strCurrentHour + ":" + strCurrentMinute + ":" +
strCurrentSecond; //$NON-NLS-1$ //$NON-NLS-2$
}
All of these methods are displaying one hour later than it actually is.