aspose file tools
The moose likes Java in General and the fly likes Date vs GregorianCalender Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "Date vs GregorianCalender" Watch "Date vs GregorianCalender" New topic
Author

Date vs GregorianCalender

sahidul karim
Greenhorn

Joined: Sep 19, 2006
Posts: 28
When should i use Date and when should go for GregorianCalender?
Darryl Failla
Ranch Hand

Joined: Oct 16, 2001
Posts: 127
Methods in Date been mostly deprecated. There is very little you are encouraged to do with it other than compare two Dates or display the Date in the default format. GregorianCalendar is useful if you are using it across the BC/AD transition.

If you are using it for custom displays in contemporary times, use Calendar and SimpleDateFormat.


Darryl Failla
Sun Certified Java 2 Programmer
Joel McNary
Bartender

Joined: Aug 20, 2001
Posts: 1815
You should rarely use GregorianCalendar explicitly; if you need to use a Calendar, call Calendar.getInstance() and use whatever subclass of Calendar is returned.

That said, you really only need to use Calendars when doing date manipulation (adding days, etc.). Most of the time, I find that I am simply storing and displaying dates, and java.util.Date and java.text.DateFormat are sufficient for that task. I rarely use Calendar, except in utility methods.


Piscis Babelis est parvus, flavus, et hiridicus, et est probabiliter insolitissima raritas in toto mundo.
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18670
[Darryl]: Methods in Date been mostly deprecated. There is very little you are encouraged to do with it other than compare two Dates or display the Date in the default format.

I disagree. Just because many of the original methods and constructors were ill-conceived does not mean that the remaining constructors and methods should not be used. For example the DateFormat class (and SimpleDateFormat) certainly use Dates when parsing and formatting. A Date is a simple, small object, very convenient for simply representing a date/time. The only thing smaller and simpler is a long (the one you'd get from Date.getTime()).

But using Date instead has a very important benefit - it automatically documents that the data here represents a date/time. If you pass a long around, there's more chance that another programmer will see the data without understanding its significance (the fact that it represents milliseconds since 000 GMT, Jan 1, 1970) unless you spend extra effort documenting this. By using Date, that becomes obvious. All the documentation you need is already present in the class.

I agree with Joel's statements about Calendars. For many applications they're needlessly complex, and contain many possible sources of error for programmers. (E.g. day of month starts with 1, but month starts with 0.) I avoid using Calendar except when the nature of the problem requires it. I find at least 3/4 of the time, Date and DateFormat (or SimpleDateFormat) are all I need.

If you find you need to do a lot of date/time calculations, I recommend the Joda Time library. It's much less painful to use than Calendar.


"I'm not back." - Bill Harding, Twister
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
The TimeAndMoney package has some things that work and are good for your brain, too. Read their pitch; I can't say any more than that.
[ September 23, 2006: Message edited by: Stan James ]

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: Date vs GregorianCalender
 
Similar Threads
choosing the Classes
using Dates and times
GregorianCalendar or Calender
same date should entered in another date field
java date