• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Java Calendar vs Java.util.Date

 
Shruti Sharma
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Can anyone tell if there is any advantage of using java calendar over java.util.Date.

Thank you,
-Shruti
 
Vinoth Kumar Kannan
Ranch Hand
Posts: 276
Chrome Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Precisely, Date/Time manipulations were previously handled through the java.util.Date class, but all of them are now deprecated.
Instead, we are now supposed to use the java.util.Calendar class for all kinds of Date/Time manipulations.
 
Kamil Wojcik
Ranch Hand
Posts: 78
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
we are now supposed to use the java.util.Calendar class for all kinds of Date/Time manipulations.

But can anyone justify WHY we have to use Calendar and is there any offical reason why java.util.Date is deprecated?
 
Wouter Oet
Saloon Keeper
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well you don't have to use Calendar but it's recommended and much easier. Date is not deprecated but most of it's methods are. Not to worry in JDK 7 there is a completely new date/time system.
 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Note that java.util.Date is not deprecated in general - but many (not all!) of the specific constructors are deprecated.

I think of the relationship between these as similar to the relationship between String and StringBuilder (or StringBuffer). No, Date isn't immutable, but it should have been, and you should treat it as though it was. A Calendar is useful for building a Date up from other data, like starting with one Date and moving to the first Monday of the next month. Use it anytime you would need to know how many days are in each month, or when a leap year is, or which day of the week it it, in order to find an answer. Avoid it any other time, because it has an evil, ugly API. If you just need to represent a particular date or time, that's what java.util.Date is for (still) - and it's much better at that than Calendar is.

Also, don't forget java.text.SimpleDateFormat, which like Calendar is good for generating Dates, but bad for representing them. SimpleDateFormat solves fewer problems than Calendar does, but it's much easier to use for the problems it does solve.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic