• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

Calendar vs Date class

 
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 479
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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).
 
God is a comedian playing for an audience that is afraid to laugh - Voltair. tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic