File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Calendar or GregorianCalendar - how to get Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Calendar or GregorianCalendar - how to get" Watch "Calendar or GregorianCalendar - how to get" New topic

Calendar or GregorianCalendar - how to get

David Duran
Ranch Hand

Joined: Feb 11, 2002
Posts: 122
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

Joined: Dec 10, 2001
Posts: 7023
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.

[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
Joel McNary

Joined: Aug 20, 2001
Posts: 1824

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.

Piscis Babelis est parvus, flavus, et hiridicus, et est probabiliter insolitissima raritas in toto mundo.
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
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 ]

Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
I agree. Here's the link:
subject: Calendar or GregorianCalendar - how to get
It's not a secret anymore!