• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

can anyone explain this ?

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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,
 
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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".
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:
 
reply
    Bookmark Topic Watch Topic
  • New Topic