Re: Discrepancy in current hour
Lew <noone@lewscanon.com> wrote in news:ho35l0$j7k$1@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:
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