Prev: Basic JUnit questions
Next: Discount CHLOE True leather AAA Wholesale (www.supertradeonline06.com )
From: Rhino on 21 Mar 2010 00:32 On Mar 20, 11:14 pm, Lew <no...(a)lewscanon.com> 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
From: Lew on 21 Mar 2010 00:54 Lew wrote: >> IOW, where your code 'main()' now has 'new TestHour()', it would have instead >> something similar to >> >> TestHour th = new TestHour(); >> th.showResults(); >> >> or simply >> >> new TestHour().showResults(); >> Rhino wrote: > Okay, I'm starting to get this. But what work would be done in the > constructor and what are you envisioning taking place in > showResults()? 'showResults()' would be all the code you wrote to show the results of your actions, i.e., those 'println()' and related calls you showed us. The constructor, well, constructs the object - initializes the state to usability. Often it's nothing more than allocating memory, in which case one can often simply rely on the default constructor and put all the meat into the methods. Anything not related to constructing an instance and making it ready for use really doesn't (usually) belong in a constructor. > The real DateTimeUtils has the same methods (aside from the > localization stuff) but has private constructors and public > getInstance() methods so it's not the same as TestHour in that way. The question isn't whether the constructor is private, but whether the calls to those methods occur within it. -- Lew
From: Lew on 21 Mar 2010 00:59 Rhino wrote: > 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. No one told you to "change it all back to the way it was". You presented a whole bunch of complicated code at one point that was dragging out integers from Calendar fields and padding them with characters into Strings. All that messy conversion seemed better suited to use of DateFormat. As a separate question, I asked why you were wrapping a simple call to Calendar.get(Calendar.HOUR), a small piece of the overall stringification process only. Separate issue, and a question, not advice. > 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.... How about distinguishing between advice ("Try X") and a request for information ("Why Y?"). The latter assumes you have a reason for doing something that the questioner has interest in knowing. An example of advice: Eric Sosman wrote: >> Suggestions: > ... >> 4) Learn to use the format() methods of PrintStream and/or >> of String. Lew wrote: > 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> Those related to providing String representations. An example of a request for information: > 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. Advice there would have been redundant since you were already using the technique in the body of your utility method, so clearly you knew of it. It was just one programmer looking to find out what another programmer's reasoning was. "Get the current hour" is a different task from "provide a String representation of a Calendar instance". Advice appropriate to one task might not apply to the other. -- Lew
From: Arved Sandstrom on 21 Mar 2010 08:40 Lew wrote: > Lew wrote: >>> IOW, where your code 'main()' now has 'new TestHour()', it would have >>> instead >>> something similar to >>> >>> TestHour th = new TestHour(); >>> th.showResults(); >>> >>> or simply >>> >>> new TestHour().showResults(); >>> > > Rhino wrote: >> Okay, I'm starting to get this. But what work would be done in the >> constructor and what are you envisioning taking place in >> showResults()? > > 'showResults()' would be all the code you wrote to show the results of > your actions, i.e., those 'println()' and related calls you showed us. > > The constructor, well, constructs the object - initializes the state to > usability. Often it's nothing more than allocating memory, in which > case one can often simply rely on the default constructor and put all > the meat into the methods. Anything not related to constructing an > instance and making it ready for use really doesn't (usually) belong in > a constructor. [ SNIP ] To add to this, the constructor will ensure that the member fields of the object are either initialized to the values you've supplied as ctor arguments (so this initialization will rely on statements you've written in the ctor body), or to default values, or as set by instance variable initializers or instance initializers. AHS
From: Rhino on 21 Mar 2010 09:26
On Mar 21, 7:40 am, Arved Sandstrom <dces...(a)hotmail.com> wrote: > Lew wrote: > > Lew wrote: > >>> IOW, where your code 'main()' now has 'new TestHour()', it would have > >>> instead > >>> something similar to > > >>> TestHour th = new TestHour(); > >>> th.showResults(); > > >>> or simply > > >>> new TestHour().showResults(); > > > Rhino wrote: > >> Okay, I'm starting to get this. But what work would be done in the > >> constructor and what are you envisioning taking place in > >> showResults()? > > > 'showResults()' would be all the code you wrote to show the results of > > your actions, i.e., those 'println()' and related calls you showed us. > > > The constructor, well, constructs the object - initializes the state to > > usability. Often it's nothing more than allocating memory, in which > > case one can often simply rely on the default constructor and put all > > the meat into the methods. Anything not related to constructing an > > instance and making it ready for use really doesn't (usually) belong in > > a constructor. > > [ SNIP ] > > To add to this, the constructor will ensure that the member fields of > the object are either initialized to the values you've supplied as ctor > arguments (so this initialization will rely on statements you've written > in the ctor body), or to default values, or as set by instance variable > initializers or instance initializers. > That all makes sense. And it's a good lesson to me too. Sometimes, I see code in examples in the newsgroup or elsewhere online and imitate them without fully understanding what they are doing. I think I must have seen something like: new TestHour(); used in a fragment once, noticed that it was more concise than: TestHour testHour = new TestHour(); and saw that the former was more concise than the latter, and just assumed it was exactly equivalent. That assumption turns out to be wrong as I've learned in this thread and now I know why. I can stay away from that approach and modify any other code that does that in "real" programs, as opposed to SSCCEs. Thanks all for explaining this point to me! -- Rhino |