From: Rhino on
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.

--
Rhino

--- news://freenews.netfront.net/ - complaints: news(a)netfront.net ---
From: Arne Vajhøj on
On 18-03-2010 20:34, Rhino wrote:
> 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.

What Java version ? Including the update !

Arne
From: Joshua Cranmer on
On 03/18/2010 08:34 PM, Rhino wrote:
> 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?

You probably don't have the up-to-date timezone data files for Java.

See <http://java.sun.com/javase/tzupdater_README.html>.

Yet another reason to tell politicians to stop messing with time: it
makes the already complicated time system much more complicated in the
programming world. For approximately no change in energy consumption.

Actually, I wonder what the total cost is to adapt for a change in DST
rules in terms of computer systems.

--
Beware of bugs in the above code; I have only proved it correct, not
tried it. -- Donald E. Knuth
From: Eric Sosman on
On 3/18/2010 8:34 PM, Rhino wrote:
> [...] But I may not have installed the update
> that handles the earlier changeover to Daylight Time. Perhaps that needs
> to be installed??

That'd be my first guess, my very first guess, and quite
likely my second as well. Third -- well, no, I'm not sure
I'd go quite *that* far; perhaps there's something more subtle
going on. But until you can assure us your Java is of this
millennium, I'm not going to speculate.

--
Eric Sosman
esosman(a)ieee-dot-org.invalid
From: Rhino on
Arne Vajh�j <arne(a)vajhoej.dk> wrote in
news:4ba2c897$0$283$14726298(a)news.sunsite.dk:

> On 18-03-2010 20:34, Rhino wrote:
>> 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.
>
> What Java version ? Including the update !
>

I'm running Java 1.6.18, which is pretty recent if I'm not mistaken ;-)

--
Rhino

--- news://freenews.netfront.net/ - complaints: news(a)netfront.net ---