• Post Reply Bookmark Topic Watch Topic
  • New Topic

Calendar vs Date class  RSS feed

 
Vonique Leary
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 479
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!