Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and JavaScript forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
  • Piet Souris
  • Frits Walraven
  • Carey Brown

can anyone explain this ?

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,
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".
Posts: 21974
Eclipse IDE Spring VI Editor Chrome Java Ubuntu 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:
    Bookmark Topic Watch Topic
  • New Topic