The moose likes Beginning Java and the fly likes Calendar vs Date class Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "Calendar vs Date class" Watch "Calendar vs Date class" New topic
Author

Calendar vs Date class

Vonique Leary
Ranch Hand

Joined: Mar 24, 2008
Posts: 100
I've been studying from the SCJP book and notice that even though Date methods are deprecated, they still use it in almost all of their Calendar examples. Is this for backward compatibility? Can one manipulate dates and times using just the Calendar object? Can it do everything the Date class can do?

Thanks,
Von
Ralph Cook
Ranch Hand

Joined: May 29, 2005
Posts: 468
A Calendar class represents date and time according to a system of time measurement and a set of rules -- GregorianCalendar is an example, and its ruleset is obvious from its name. A Date represents a point in time measured in milliseconds from epoch, regardless of any rules; you can't figure out what day and year it is from Date alone, because it doesn't know what rules you're operating with. You have to have a Calendar to figure out things like when the year 1 was and what time zone it is, and only with that info can you make real dates and times.

You can do a lot of before-after-how-long-since with Date, though.

rc
Jesper de Jong
Java Cowboy
Bartender

Joined: Aug 16, 2005
Posts: 11642

Unfortunately the Date and Calendar API of standard Java is not an example of great design. Even though some of the constructors and methods of class Date are deprecated, it doesn't mean that you should not use class Date.

There is a much better and very popular library called Joda Time available. If you're going to start on a project where you're going to work with dates and times, I'd highly recommend using this library. It is much better designed than Java's standard date and time API. The author of Joda Time (Stephen Colebourne) is also busy with a proposal to add a new date and time API to a future version of Java (which will, ofcourse, look a lot like Joda Time).

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
 
 
subject: Calendar vs Date class
 
Threads others viewed
"Date" Problem
Time class
checking for date
Date class
How to manipulate date / time using Calendar and then put the modified data inside the Date class?
MyEclipse, The Clear Choice

cast iron skillet 49er

more from paul wheaton's glorious empire of web junk: cast iron skillet diatomaceous earth rocket mass heater sepp holzer raised garden beds raising chickens lawn care CFL flea control missoula heat permaculture