• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

can anyone explain this ?

 
Nasir Munir
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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,
Thanks,
 
Krep Lock
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Pie
Posts: 20546
56
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic