• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Calendar or GregorianCalendar - how to get

 
David Duran
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Which of the two is the more recommended way to get a calendar object or is there no difference? I read once that the Calendar object is a pretty "heavy" object and they recommened using the latter below. Any thoughts?
Calendar cal = Calendar.getInstance();
GregorianCalendar cal = new GregorianCalendar();
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
GregorianCalendar is the type meant to represent the standard calendar used by most of the world (according to the class documentation). For that reason, I'd probably prefer it in most situations I'm likely to make use of a calendar.
 
Joel McNary
Bartender
Posts: 1840
Eclipse IDE Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not sure what is meant by a "heavy" object in this context. I personally use Calendar.getInstance(), because that the most loosely coupled reference (and takes less typing, so it's good in two ways ) Since getInstance returns the Locale-based subclass, I am more than likely to wind up with a GregorianCalendar anyway, unless my code is being run in a strange locale. And if my code is being run in a strange locale, then it's a good thing I used the more loosly-coupled Calendar.
 
Thomas Paul
mister krabs
Ranch Hand
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The Calendar getInstance method eventually runs this method with the default zone and locale:

All Calendar.getInstance() does is create a new GregorianCalendar object with the default zone and locale.
[ June 06, 2003: Message edited by: Thomas Paul ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic