wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes can anyone explain this ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "can anyone explain this ?" Watch "can anyone explain this ?" New topic

can anyone explain this ?

Nasir Munir

Joined: May 31, 2002
Posts: 22
Date Today = Calendar.getInstance().getTime() ;
my question: I understand one can create an instance of a class, Calendar in this case and then use its method getInstance(). I don't understand .getTime() part ? Can we use call number of methods at the same time ? I understand Calendar.getInstance() part, but cannot understand Calendar.getInstance().getTime() part. Need some help,
Krep Lock
Ranch Hand

Joined: Sep 19, 2006
Posts: 43
Here's plenny of info on Calendar.

It looks like someone just wants a timestamp, but the getTime() method isn't static, so a temporary instance of Calendar is needed. This instance is generated, but a reference is not saved anywhere, so the object is immediately eligible for garbage collection once this line's execution if finished.

The .getTime() fires off and the results are stored in Today, which would be better named "today".
Rob Spoor

Joined: Oct 27, 2005
Posts: 19655

Slightly offtopic, but "Calendar.getInstance().getTime();" will do exactly the same as just calling "new Date();"

Coming back to the issue, this is shorthand for with one exception: you don't name the Calendar object.

You will want to learn this kind of approach; not only if you want to write it, but also if you want to read Java. A slightly different method of programming can be found in java.lang.StringBuffer, java.lang.StringBuffer and java.lang.ProcessBuilder: most of their methods return the object itself:

This allows for huge chains of calls without ever having a named variable:

How To Ask Questions How To Answer Questions
I agree. Here's the link: http://aspose.com/file-tools
subject: can anyone explain this ?
Similar Threads
How to use calender
Date Insert
Measure Time
Static methods Vs "Instance Methods"