Re: Discrepancy in current hour



On Mar 20, 11:14 pm, Lew <no...@xxxxxxxxxxxxx> wrote:
Lew wrote:
   Calendar calinst = Calendar.getInstance()
   int calendarHour = calinst.get( Calendar.HOUR );
...
I wonder why you write a utility method to get the current hour when the
standard API already provides a one-line way to do so.
Rhino wrote:
I must have missed that way in the standard API. Can you remind what
it is? I'm not sure which class you're thinking of....

It's in the code samples you provided in your original question:

Rhino wrote:
  GregorianCalendar now = new GregorianCalendar();
  return now.get(Calendar.HOUR);

All I did differently was to follow Eric Sosman's advice for "the recommended
`Calendar.getInstance()'".

Note that if you don't need to re-use the calendar instance, you can get the
current hour in a single line:

  int calendarHour = Calendar.getInstance().get( Calendar.HOUR );

Okay, I'm getting a little dizzy here. First, I was doing everything
with Calendar and was told that it would be better to use DateFormat
and SimpleDateFormat so I rewrote the methods to use that approach
(after having to review a bit to figure out how to use them.) Now
you're telling me that the original way was fine?

Just for the heck of it, I wrote a little display method to get all
the values that are available from Calendar:

public void useCalendar() {

System.out.println("The AM_PM is: " +
Calendar.getInstance().get(Calendar.AM_PM));
System.out.println("The DATE is: " +
Calendar.getInstance().get(Calendar.DATE));
System.out.println("The DAY_OF_MONTH is: " +
Calendar.getInstance().get(Calendar.DAY_OF_MONTH));
System.out.println("The DAY_OF_WEEK_IN_MONTH is: " +
Calendar.getInstance().get(Calendar.DAY_OF_WEEK_IN_MONTH));
System.out.println("The DAY_OF_YEAR is: " +
Calendar.getInstance().get(Calendar.DAY_OF_YEAR));
System.out.println("The DST_OFFSET is: " +
Calendar.getInstance().get(Calendar.DST_OFFSET));
System.out.println("The ERA is: " +
Calendar.getInstance().get(Calendar.ERA));
System.out.println("The HOUR is: " +
Calendar.getInstance().get(Calendar.HOUR));
System.out.println("The HOUR_OF_DAY is: " +
Calendar.getInstance().get(Calendar.HOUR_OF_DAY));
System.out.println("The MILLISECOND is: " +
Calendar.getInstance().get(Calendar.MILLISECOND));
System.out.println("The MINUTE is: " +
Calendar.getInstance().get(Calendar.MINUTE));
System.out.println("The MONTH is: " +
Calendar.getInstance().get(Calendar.MONTH));
System.out.println("The SECOND is: " +
Calendar.getInstance().get(Calendar.SECOND));
System.out.println("The WEEK_OF_MONTH is: " +
Calendar.getInstance().get(Calendar.WEEK_OF_MONTH));
System.out.println("The WEEK_OF_YEAR is: " +
Calendar.getInstance().get(Calendar.WEEK_OF_YEAR));
System.out.println("The YEAR is: " +
Calendar.getInstance().get(Calendar.YEAR));
System.out.println("The ZONE_OFFSET is: " +
Calendar.getInstance().get(Calendar.ZONE_OFFSET));
}

The results (I ran this just AFTER midnight):

------------------------------------------------------------------
The AM_PM is: 0
The DATE is: 21
The DAY_OF_MONTH is: 21
The DAY_OF_WEEK_IN_MONTH is: 3
The DAY_OF_YEAR is: 80
The DST_OFFSET is: 3600000
The ERA is: 1
The HOUR is: 1
The HOUR_OF_DAY is: 1
The MILLISECOND is: 328
The MINUTE is: 14
The MONTH is: 2
The SECOND is: 46
The WEEK_OF_MONTH is: 4
The WEEK_OF_YEAR is: 13
The YEAR is: 2010
The ZONE_OFFSET is: -18000000

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

I'm not sure if those results help you at all....

For what it's worth, some of my methods are just getting parts of the
date and like hour and year but others are doing other things, like
ensuring that a given date fits a given format or producing a given
date component as a String rather than an int. I wrote these several
years back but I don't remember why I coded them all the way I did.

I am happy to learn more about Java and improve my code but getting
told to do it one way, spending time to do it, then being told to
change it all back to the way it was is frustrating to say the least.
I'm just trying to be obliging here and accept what I'm being told
rather than make everyone justify their advice. I'm assuming you guys
know more than me. Maybe I have to start getting a little more
assertive and make you justify the changes you are asking me to
make....

In any case, I _DO_ appreciate the help I am getting, even if it is a
little frustrating at times.

Does anyone see anything yet that makes them think this is _NOT_ an OS
issue? For that matter, has anyone tried this code on their own
machines? Did any of you get the same problem I'm getting with the
hour being one hour later than it should be?

--
Rhino

.