Re: Discrepancy in current hour
On Mar 20, 5:48 pm, Rhino <rch...@ymail.com> wrote:
On Mar 20, 4:53 pm, Eric Sosman <esos...@ieee-dot-org.invalid> wrote:
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 object=
s
that haven't finished construction! This is a Big No=
-No;
don't do it!
--
Eric Sosman
esos...@ieee-dot-org.invalid
I think you're saying that there is a fundamental flaw in the design
of my class that is causing my trouble. And that's entirely possible.
I'm basically just trying to write a class containing methods that I
can call with a single line of code to get me basic date/time
information. For example, what is the current time? What is the
current hour? etc. etc. The average program that is going to call this
cl;ass is not going to execute all of the methods in it but only a
small handful of them most of the time. If I'm understanding the
terminology correctly, this class is full of "convenience methods".
Therefore, it seems to me that each method should be standalone and
not count on the caller having executed other methods first, aside
from the getInstance() of course.
Under those circumstances, and given that some of the methods don't
even use Calendar, are you saying in your first point that I should be
getting a single instance of Calendar in the getInstance() method that
is then shared with all other methods in this instance?
With regards to your second and third points, I will do that shortly,
after I finish this post.
with regards to your fourth point, are you saying I should be using
format() instead of my own home-grown pad() method? If so, that's
perfectly valid and I have no problem with it. If that's not what you
mean, could you clarify?
With regards to your fifth point, I'm afraid I still get a little
muddled when someone makes a generalized commment like that without
also pointing to the specific thing I've done wrong. Can you clarify
which object hasn't finished construction?
Here is my amended SSCCE and the actual output I got a few minutes ago
while the hour was still 6 PM.
import java.util.Calendar;
import java.util.GregorianCalendar;
public class TestHour {
final static boolean DEBUG = true;
public static void main(String[] args) {
new TestHour();
}
public TestHour() {
System.out.println("The current hour on the 12 hour clock is: " +
getCurrentHour12HourClock() + ".\n");
System.out.println("The current hour on the 24 hour clock is: " +
getCurrentHour24HourClock() + ".\n");
System.out.println("The current time is: " + getCurrentTime() + ".
\n");
}
public int getCurrentHour12HourClock() {
/* Get the current date, then the current hour (12 hour
clock). */
GregorianCalendar now = new GregorianCalendar();
if (DEBUG) {
System.out.println("now.toString(): " + now.toString());
System.out.println("Calendar.getInstance().toString(): " +
Calendar.getInstance().toString());
}
return now.get(Calendar.HOUR);
}
public int getCurrentHour24HourClock() {
/* Get the current date, then the current hour (24 hour
clock). */
GregorianCalendar now = new GregorianCalendar();
if (DEBUG) {
System.out.println("now.toString(): " + now.toString());
System.out.println("Calendar.getInstance().toString(): " +
Calendar.getInstance().toString());
}
return now.get(Calendar.HOUR_OF_DAY);
}
public String getCurrentTime() {
/* Get the current time. */
GregorianCalendar now = new GregorianCalendar();
System.out.println("Calendar: " + now.toString());
if (DEBUG) {
System.out.println("now.toString(): " + now.toString());
System.out.println("Calendar.getInstance().toString(): " +
Calendar.getInstance().toString());
}
int intCurrentHour = now.get(Calendar.HOUR_OF_DAY);
int intCurrentMinute = now.get(Calendar.MINUTE);
int intCurrentSecond = now.get(Calendar.SECOND);
/*
* Convert hour, minute, and second values to strings. Pad the
values
* with leading zeroes as necessary to put them in ISO format.
*/
String strCurrentHour = pad(intCurrentHour, '0', 'L', 2);
String strCurrentMinute = pad(intCurrentMinute, '0', 'L', 2);
String strCurrentSecond = pad(intCurrentSecond, '0', 'L', 2);
/* Construct the string representing the current time. */
return strCurrentHour + ":" + strCurrentMinute + ":" +
strCurrentSecond;
}
public String pad(long input, char padChar, char padPosition, int
finalLength) {
/* Convert the input integer to a String. */
String strInput = String.valueOf(input);
/*
* Pad position must be 'l' or 'L' for "leading" or 't' or 'T'
for
* trailing.
*/
if (padPosition != 'L' & padPosition != 'l' & padPosition !=
'T'
& padPosition != 't') {
throw new IllegalArgumentException("Type of padding must be
'L', 'l', 'T', or 't'.");
}
/*
* Final length must be greater than or equal to the length of
the input
* string.
*/
if (finalLength < strInput.length()) {
throw new IllegalArgumentException("The value you are
padding is already longer than the final length you want.");
}
/*
* Determine the number of occurrences of the pad character
that will
* have to be added to the input string.
*/
int numPadChars = finalLength - strInput.length();
/*
* If the pad position is 'L' or 'l' for "leading", write the
pad
* characters followed by the input string. Otherwise, write
the pad
* characters after the input string.
*/
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());
}
}
The output wraps horribly so I'll just summarize it verbally: The hour
still shows as 7 PM even though it was not yet 7 PM when I executed
the code.
--
Rhino