From: Rhino on
Lew <noone(a)lewscanon.com> wrote in news:ho35l0$j7k$1(a)news.albasani.net:

> Rhino said :
>>>>> How do I get the Java routines to get the right time WHILE STILL
>>>>> HAVING WINDOWS ITSELF DISPLAY THE CORRECT TIME?
>
> Lew wrote:
>>> Provide an SSCCE!
>
> Rhino wrote:
>> 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 don't appreciate the "duh". I'm not an idiot but I'm not a psychic
either. I didn't understand what you meant.

I don't think there's anything wrong with the date routines themselves
since they've been working fine for ages. But I did post them in the
original question.

>> 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.
>
Obviously, any SSCCE isn't going to show you the screens that come up
when I click on the Windows clock so this example only shows you the Java
code that I am running to display the current time.

---------------------------------------------------
import java.util.Calendar;
import java.util.GregorianCalendar;

public class TestHour {

public static void main(String[] args) {

new TestHour();
}

public TestHour() {

System.out.println("The current hour on the 12 hour clock is: " +
getCurrentHour12HourClock() + ".");
System.out.println("The current hour on the 24 hour clock is: " +
getCurrentHour24HourClock() + ".");
System.out.println("The current time is: " + getCurrentTime() +
".");
}

public int getCurrentHour12HourClock() {

GregorianCalendar now = new GregorianCalendar();
return now.get(Calendar.HOUR);
}

public int getCurrentHour24HourClock() {

GregorianCalendar now = new GregorianCalendar();
return now.get(Calendar.HOUR_OF_DAY);
}

public String getCurrentTime() {

GregorianCalendar now = new GregorianCalendar();
int intCurrentHour = now.get(Calendar.HOUR_OF_DAY);
int intCurrentMinute = now.get(Calendar.MINUTE);
int intCurrentSecond = now.get(Calendar.SECOND);

String strCurrentHour = pad(intCurrentHour, '0', 'L', 2);
String strCurrentMinute = pad(intCurrentMinute, '0', 'L', 2);
String strCurrentSecond = pad(intCurrentSecond, '0', 'L', 2);

return strCurrentHour + ":" + strCurrentMinute + ":" +
strCurrentSecond;
}

public String pad(long input, char padChar, char padPosition, int
finalLength) {

String strInput = String.valueOf(input);

if (padPosition != 'L' & padPosition != 'l' & padPosition != 'T'
& padPosition != 't') {
throw new IllegalArgumentException("Type of padding must be 'L',
'l', 'T', or 't'.");
}

if (finalLength < strInput.length()) {
throw new IllegalArgumentException("The value you are
padding is already longer than the final length you want.");
}

int numPadChars = finalLength - strInput.length();
StringBuffer strbuffPaddedString = new StringBuffer();
if (padPosition == 'L' | padPosition == 'l') {
for (int ix = 0; ix < numPadChars; ix++)
strbuffPaddedString.append(padChar);
strbuffPaddedString.append(strInput);
} else {
strbuffPaddedString.append(strInput);
for (int ix = 0; ix < numPadChars; ix++)
strbuffPaddedString.append(padChar);
}

return (strbuffPaddedString.toString());
}

}

---------------------------------------------------

This is the output I got when I ran the SSCCE a few moments ago (at 5:11
PM EDT.)

=============================================
The current hour on the 12 hour clock is: 6.
The current hour on the 24 hour clock is: 18.
The current time is: 18:11:39.
=============================================

Do you agree that the Java code should give me the correct hour?
--
Rhino
From: Rhino on
On Mar 19, 8:31 pm, Arne Vajhøj <a...(a)vajhoej.dk> wrote:
> On 19-03-2010 00:02, Rhino wrote:
>
> > Joshua Cranmer<Pidgeo...(a)verizon.invalid>  wrote innews:hnuh8d$aro$1(a)news-
> > int2.gatech.edu:
> >> 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>.
>
> > This is the first I've heard of them. Downloading now....
>
> It is only relevant for old Java versions.
>

That's a relief. I was never able to download those files. I've
forgotten the name of my account so I've been trying to create a new
one but the process keeps failing inexplicably. I'm assuming it has
something to do with Oracle taking over Sun and messing with the
systems.

--
Rhino

From: Eric Sosman on
On 3/20/2010 5:21 PM, Rhino wrote:
> [...]
>
> This is the output I got when I ran the SSCCE a few moments ago (at 5:11
> PM EDT.)
>
> =============================================
> The current hour on the 12 hour clock is: 6.
> The current hour on the 24 hour clock is: 18.
> The current time is: 18:11:39.
> =============================================

You're getting the data from a bunch of GregorianCalendar
instances (not all the same instance, for some reason), and
you've created them with `new GregorianCalendar()' rather than
with the recommended `Calendar.getInstance()'. That may or may
not have something to do with your problem.

Suggestions:

1) Use one Calendar object instead of several.

2) Print the toString() of that Calendar instance (my bet
is that you'll find it's still in EST).

3) Try using Calendar.getInstance(), and print the toString()
of that object, too, to see if you get EDT this time.

4) Learn to use the format() methods of PrintStream and/or
of String.

5) Refrain from calling non-final instance methods on objects
that haven't finished construction! This is a Big No-No;
don't do it!

--
Eric Sosman
esosman(a)ieee-dot-org.invalid
From: Rhino on
On Mar 19, 11:45 pm, Lew <no...(a)lewscanon.com> wrote:
> 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??
> >>>> ...
> >>>> I'm running Java 1.6.18, which is pretty recent if I'm not mistaken ;-)
> Arne Vajhøj wrote:
> > 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.
>
> --
> Lew

I'm prepared to revisit the code and rework it completely if
necessary, although I'd certainly be reluctant to do so since they've
been working fine up until now.

--
Rhino
From: Lew on
Eric Sosman wrote:
> Suggestions:
....
> 4) Learn to use the format() methods of PrintStream and/or
> of String.

Or use java.text.DateFormat / SimpleDateFormat.
<http://java.sun.com/javase/6/docs/api/java/text/DateFormat.html>
<http://java.sun.com/javase/6/docs/api/java/text/SimpleDateFormat.html>

--
Lew