Discrepancy in current hour

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.
 
J

Joshua Cranmer

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.
 
A

Arne Vajhøj

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
 
E

Eric Sosman

[...] 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.
 
R

Rhino

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

See <http://java.sun.com/javase/tzupdater_README.html>.
This is the first I've heard of them. Downloading now....
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.
Point well taken. I share your concerns and doubts about the value of
making these DST changes. I have to wonder if the energy saved by some
businesses is not more than outweighed by the grief this causes our
industry.

But then politics is pretty much always the practice of robbing Peter to
pay Paul....
 
R

Roedy Green

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??

try SetClock to be sure your timezone and time are correct.
see http://mindprod.com/webstart/setclock.html

--
Roedy Green Canadian Mind Products
http://mindprod.com

Responsible Development is the style of development I aspire to now. It can be summarized by answering the question, “How would I develop if it were my money?” I’m amazed how many theoretical arguments evaporate when faced with this question.
~ Kent Beck (born: 1961 age: 49) , evangelist for extreme programming.
 
R

Rhino

try SetClock to be sure your timezone and time are correct.
see http://mindprod.com/webstart/setclock.html

I'm very confused, Roedy.

I clicked on your link and launched your program without any
difficulties. However, I don't understand how it is behaving.

Before I launched SetClock, the clock on my PC (I'm running XP with SP2)
shows a time of 15:26 Eastern time. The time.gov website confirms this
is the correct time for the Eastern time zone.

When I clicked on SetClock, the GUI showed:
Your Time Zone: Eastern Daylight Time (GMT-4:00)
Your PC's Clock: Friday 2010/03/19 04:29:00 PM
Correction: 59 minutes and 59 seconds
Accurate clock: Friday: 201003/19 03:29:56 PM

When I clicked on the SetClock button, the time on my computer changed
to 2:31 and the SetClock GUI now shows 3:31 in both the "Your PC's
Clock" and "Accurate Clock" fields and Correction now says "none".

However, my Java date routines now show the correct hour, 3 P.M.

To paraphrase from Battlestar Galactica, "what the frak??"

How do I get the Java routines to get the right time WHILE STILL HAVING
WINDOWS ITSELF DISPLAY THE CORRECT TIME?
 
A

Arne Vajhøj

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

It is latest, so it should have the correct timezone definitions.

Something is confusing Java on your PC.

Arne
 
L

Lew

It is latest, so it should have the correct timezone definitions.

Something is confusing Java on your PC.

Or something is confusing Windows or the PC itself.

What does the OS claim the time zone is, independently of Java?

Do the OS time and BIOS time match?

Would the OP provide an SSCCE and copy-paste actual output, and a comparison
with what was expected?

The problem has to be in a detail that hasn't reached Usenet yet.

For example, what is this 'StringUtils'? If 'String' formatted
representations are needed of a 'Calendar', what's wrong with using 'DateFormat'?

I really, really don't think that "it has something to do with Java date
routines not correctly handling the earlier changeover to daylight savings
time" or anything else to do with the standard API, nor do I think it's a
problem with out-of-date tzdata.

I'm deeply suspicious of code that uses a 'Calendar', then extracts fields
from it, then pads the fields back into 'String's. That's an awful lot of
custom conversion, with lots of room for things to be done wrong, for one to
go around calumnizing the java.* packages.

Look to thine own house first.
 
R

Roedy Green

How do I get the Java routines to get the right time WHILE STILL HAVING
WINDOWS ITSELF DISPLAY THE CORRECT TIME?

Click the clock time in the bottom right.
click change date and time settings.
Click change time zone.
Make sure you have the correct timezone.
Click "adjust clock for daylight saving".

Rerun Setclock.
--
Roedy Green Canadian Mind Products
http://mindprod.com

Responsible Development is the style of development I aspire to now. It can be summarized by answering the question, “How would I develop if it were my money?” I’m amazed how many theoretical arguments evaporate when faced with this question.
~ Kent Beck (born: 1961 age: 49) , evangelist for extreme programming.
 
A

Arved Sandstrom

Lew said:
Or something is confusing Windows or the PC itself.

What does the OS claim the time zone is, independently of Java?

Do the OS time and BIOS time match?

Would the OP provide an SSCCE and copy-paste actual output, and a
comparison with what was expected?

The problem has to be in a detail that hasn't reached Usenet yet.

For example, what is this 'StringUtils'? If 'String' formatted
representations are needed of a 'Calendar', what's wrong with using
'DateFormat'?
[ SNIP ]

Rhino mentioned a StringUtils class of his own - it was the subject of
the "Design question" thread dating from 15 March. Although there are
dozens of StringUtils classes put out by various projects, the most
notable probably being Apache Commons Lang, I'm guessing the one here is
likely Rhino's.

The first thing I'm usually suspicious of when a test reports a test
failure is the test itself. As part of one maintenance project back in
2008 I spent the best part of a month fixing defects in a JUnit test
suite; nearly a hundred JUnit tests were themselves broken. They hadn't
necessarily always been - most of them were OK when written - but they
failed to track code changes. But some had clearly always been wrong.

I think it's a bit crazy to unit test your unit tests, under normal
conditions; nevertheless it's worth keeping in mind that if you can't
keep your tested code error-free then neither can you keep your testing
code error-free, and interpret the output of tests accordingly.

In that project I mentioned, the tests that had the highest problem
percentage involved date/time code. No surprise there. Both the
date/time code under test, and the date/time code *in* the tests, were
riddled with defects.

Under the circumstances, in this hypothetical SSCCE, I'd be checking the
tests first.

AHS
 
L

Lew

Arved said:
Rhino mentioned a StringUtils class of his own - it was the subject of
the "Design question" thread dating from 15 March. Although there are
dozens of StringUtils classes put out by various projects, the most
notable probably being Apache Commons Lang, I'm guessing the one here is
likely Rhino's.

Same here, but that doesn't answer my question. I mean "what is?" as in "show
us the code", as it is at the time of the bug under discussion. That has not
been shown.
The first thing I'm usually suspicious of when a test reports a test
failure is the test itself. As part of one maintenance project back in
2008 I spent the best part of a month fixing defects in a JUnit test
suite; nearly a hundred JUnit tests were themselves broken. They hadn't
necessarily always been - most of them were OK when written - but they
failed to track code changes. But some had clearly always been wrong.

I've seen this, too, coincidentally in one case where it was calendar
functions under test and the bug in the unit test permitted wrong code to go
into production.
I think it's a bit crazy to unit test your unit tests, under normal
conditions; nevertheless it's worth keeping in mind that if you can't
keep your tested code error-free then neither can you keep your testing
code error-free, and interpret the output of tests accordingly.

In that project I mentioned, the tests that had the highest problem
percentage involved date/time code. No surprise there. Both the
date/time code under test, and the date/time code *in* the tests, were
riddled with defects.

Under the circumstances, in this hypothetical SSCCE, I'd be checking the
tests first.

The problem is that the SSCCE is hypothetical.

Rhino, we need an SSCCE to proceed further.
 
R

Rhino

Click the clock time in the bottom right.
click change date and time settings.
Click change time zone.
Make sure you have the correct timezone.
Click "adjust clock for daylight saving".

I clicked on the Windows clock icon and found that the time is already set
to 1:37 PM; the date is already correct. The timezone is already set to GMT
-5:00 Eastern Time, Canada and US. The observe daylight saving time box is
checked. In other words, Windows clock is already exactly right.
Rerun Setclock.

When I launched SetClock, it said the timezone was "EDT : Eastern Daylight
Time (GMT-04:00)" [NOT GMT-5:00] and claimed that my PC clock was set to
2:37 [NOT 1:37]. I clicked on the GetTime button and the correction
appeared as 59 minutes, 59 seconds. I clicked on the SetClock button and
the SetClock GUI now says the PC Clock is 1:37 and the Accurate Clock is
1:37. The Windows Clock now says 12:37, which is an hour early of the
correct time.

Am I not understanding your instructions or is there something wrong here?

If there IS something wrong, where is the problem? In XP? Or could there be
a bug in your SetClock program?
 
L

Lew

Rhino said:
I clicked on the Windows clock icon and found that the time is already set
to 1:37 PM; the date is already correct. The timezone is already set to GMT

So are we to conclude, as you have not stated it, that the time you checked
was actually 1:37 p.m. Eastern Daylight, and that you are in the Eastern time
zone?
-5:00 Eastern Time, Canada and US. The observe daylight saving time box is

That is not correct, if you are in the Eastern Time Zone and DST is in effect.
If you are in the Eastern Time Zone and DST is in effect, you are at GMT-04:00.
checked. In other words, Windows clock is already exactly right.

Your statements leave room for doubt about that.
Rerun Setclock.

When I launched SetClock, it said the timezone was "EDT : Eastern Daylight
Time (GMT-04:00)" [NOT GMT-5:00] and claimed that my PC clock was set to

That is correct. The Eastern Time Zone is currently in DST and is GMT-04:00.

Why do you not think that is correct?
2:37 [NOT 1:37]. I clicked on the GetTime button and the correction
appeared as 59 minutes, 59 seconds. I clicked on the SetClock button and
the SetClock GUI now says the PC Clock is 1:37 and the Accurate Clock is
1:37. The Windows Clock now says 12:37, which is an hour early of the
correct time.

Your Windows clock must be set wrong.
Am I not understanding your instructions or is there something wrong here?

If there IS something wrong, where is the problem? In XP? Or could there be
a bug in your SetClock program?

The problem is in your environment, not the software you keep trying to blame.
 
R

Rhino

Lew said:
Rhino said :

Provide an SSCCE!

You're kidding, right? If not, how do I provide an SSCCE of what the clock
on my PC is doing?

I could do some screen shots, assuming I can think of some place to put
them where you can see it but short of that, I don't know how to honour
your request.....
 
L

Lew

Rhino said :
You're kidding, right? If not, how do I provide an SSCCE of what the clock
on my PC is doing?

No, I mean of the Java code, duh.
I could do some screen shots, assuming I can think of some place to put
them where you can see it but short of that, I don't know how to honour
your request.....

By honoring the actual request.
 
R

Rhino

Lew said:
So are we to conclude, as you have not stated it, that the time you
checked was actually 1:37 p.m. Eastern Daylight, and that you are in
the Eastern time zone?


That is not correct, if you are in the Eastern Time Zone and DST is in
effect.
If you are in the Eastern Time Zone and DST is in effect, you are at
GMT-04:00.
I am indeed in the Eastern Time Zone and DST went into effect last
weekend. However, Windows XP isn't going to let me set the timezone to
Eastern Time GMT-4:00. Windows gives me a drop down menu showing me
umpteen timezone and Eastern GMT-4:00 isn't one of them. It also provides
a checkbox that says "automatically adjust clock for DST", which I have
checked.
checked. In other words, Windows clock is already exactly right.

Your statements leave room for doubt about that.
Rerun Setclock.

When I launched SetClock, it said the timezone was "EDT : Eastern
Daylight Time (GMT-04:00)" [NOT GMT-5:00] and claimed that my PC
clock was set to

That is correct. The Eastern Time Zone is currently in DST and is
GMT-04:00.

Why do you not think that is correct?
I'm not saying it isn't correct. I'm saying that it is not consistent
with what Windows is telling me. I'm trying to find out if the behaviour
I'm seeing in SetClock is wrong or if Windows is behaving badly.
2:37 [NOT 1:37]. I clicked on the GetTime button and the correction
appeared as 59 minutes, 59 seconds. I clicked on the SetClock button
and the SetClock GUI now says the PC Clock is 1:37 and the Accurate
Clock is 1:37. The Windows Clock now says 12:37, which is an hour
early of the correct time.

Your Windows clock must be set wrong.
Am I not understanding your instructions or is there something wrong
here?

If there IS something wrong, where is the problem? In XP? Or could
there be a bug in your SetClock program?

The problem is in your environment, not the software you keep trying
to blame.

Actually, I'm NOT trying to blame Roedy's SetClock, I'm trying to
understand the behaviour I am getting. If the problem is in Windows,
that's fine, I'm quite prepared to accept that. For the moment, I'm just
trying to be sure that I'm following Roedy's instructions correctly. The
first point of failure with this kind of thing is usually communications
issues over understanding the instructions, at least in my case.
 

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,982
Messages
2,570,185
Members
46,738
Latest member
JinaMacvit

Latest Threads

Top