The Artima Developer Community
Sponsored Link

Java Answers Forum
error?

9 replies on 1 page. Most recent reply: Mar 22, 2004 2:50 PM by Lynn Hollerman

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 9 replies on 1 page
Lynn Hollerman

Posts: 67
Nickname: gmholler
Registered: Mar, 2002

error? Posted: Mar 17, 2004 7:54 AM
Reply to this message Reply
Advertisement
I recently saw this in a .java file:

"long time2 = Calendar.getInstance().getTime().getTime();"

Maybe it's just the way I'm looking at this, but that repeat of "getTime()" confuses me - maybe I've just never seen something like this before or I have and don't realize it. I *think* what this line is saying is to get an instance of a Calendar object and run the getTime()(of Calendar) method on it - and then is the 2nd "getTime()" there to convert to a long using the getTime() method of the Date class?

Thanks!

Lynn.


twc

Posts: 129
Nickname: twc
Registered: Feb, 2004

Re: error? Posted: Mar 17, 2004 11:22 AM
Reply to this message Reply
> "long time2 = Calendar.getInstance().getTime().getTime();"

Here is a longer version of the code that you saw with some (I hope) helpful comments.
//returns a reference to a Calendar object to be stored in now
Calendar now = Calendar.getInstance();
//returns a reference to a Date object representing the time of the Calendar object now to be stored in time
Date time = today.getTime();
//returns a long set to the number of milliseconds since January 1, 1970, 00:00:00 GMT 
//represented by the Date object time to be stored in time2
long time2 = time.getTime();

Lynn Hollerman

Posts: 67
Nickname: gmholler
Registered: Mar, 2002

Re: error? Posted: Mar 17, 2004 1:44 PM
Reply to this message Reply
Yes, that makes much more sense. I thought I was seeing double for a moment there!

Thanks!

Lynn.

Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: error? Posted: Mar 19, 2004 9:50 PM
Reply to this message Reply
I would have thought you'd be little more confused after that example. ;-)

Look just a little closer...

(By the way, the inconsistency of those two getTime() methods was probably an oversight -- probably different people developed those two API classes)

twc

Posts: 129
Nickname: twc
Registered: Feb, 2004

Re: error? Posted: Mar 20, 2004 6:20 AM
Reply to this message Reply
Beginners usually need to see the steps broken down the long way. I work with a lot of beginners, and most cannot follow the code in the first example without seeing its equivalent in the longer example.

Like you, I prefer to code the first way.

Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: error? Posted: Mar 20, 2004 10:47 PM
Reply to this message Reply
No, that's not what I meant. I was refering to the mysterious use of the 'today' variable.

Adam Duffy

Posts: 168
Nickname: adamduffy
Registered: Feb, 2003

Re: error? Posted: Mar 22, 2004 6:03 AM
Reply to this message Reply
Just to clarify Matt's thinking. In the code

Calendar now = Calendar.getInstance();
Date today = now.getTime();
long time = today.getTime();


it might have been better for Sun to replace

Date today = now.getTime();


with

Date today = now.getDate();


to avoid ambiguity.

NB The code at the top compiles. This is just "it would be nice if..." thinking.

Of course, I can't read minds. Matt may have been referring to something else altogether.

Adam

twc

Posts: 129
Nickname: twc
Registered: Feb, 2004

Re: error? Posted: Mar 22, 2004 6:43 AM
Reply to this message Reply
Aaargh! I didn't notice that I did that! I even used the name 'now' in the comment but not in the statement.

Lynn,
If you are still reading, 'today' and 'now' should both be the same. It doesn't matter which name is used as long as they are both the same. Actually, that can be one of the advantages of the original form. Sorry for any confusion.

Revised explanation
//returns a reference to a Calendar object to be stored in now
Calendar now = Calendar.getInstance();
//returns a reference to a Date object representing the time of the Calendar object now to be stored in time
 
Date time = now.getTime();//notice correction!!!!!!
 
//returns a long set to the number of milliseconds since January 1, 1970, 00:00:00 GMT 
//represented by the Date object time to be stored in time2
long time2 = time.getTime();

Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: error? Posted: Mar 22, 2004 2:12 PM
Reply to this message Reply
Don't worry, twc, the form you suggested would be quick and easy to fix with the help of the compiler. In fact it would be better than the original one-liner, which, as exemplified by Lynn's quandry, was too cryptic. Moreover, if the three-method pile-up did have some error, it would be harder to debug.

As far as the style goes, I think your example is quite good. In fact, it is so clear, it doesn't need any comments; it is self-documenting. The only argument against it would be the old premature optimization canard that you are creating unnecessary variables. Even that claim may be false, since temporaries will need to be created anyway (the actual difference may be JVM-dependent and the good JVMs would probably optimize both styles down to the same set of operations).

This self-documenting style is especially useful -- even better than comments -- in this case, where both of the getTime() methods seem to be misnamed. As far as I'm concerned, since neither of them returns a 'Time' object, neither should be called "getTime()" -- the fact that they both return something different is even more confounding. It seems like they should be named getDate() (yes Adam, your psychic powers are intact) and getMilliseconds(), respectively, or something along those lines.

Lynn Hollerman

Posts: 67
Nickname: gmholler
Registered: Mar, 2002

Re: error? Posted: Mar 22, 2004 2:50 PM
Reply to this message Reply
No worries - I got the gist of what was being said after I looked at the javadoc as well. Seeing the two methods just threw me - like it was one more thing to give me problems that day!

Lynn.

Flat View: This topic has 9 replies on 1 page
Topic: Setters/Getters: Related To JavaBeans Only? Previous Topic   Next Topic Topic: How I stop to creat another instance of appl if one is already open

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use