aspose file tools*
The moose likes Java in General and the fly likes Java Calendar vs Java.util.Date Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Java Calendar vs Java.util.Date" Watch "Java Calendar vs Java.util.Date" New topic
Author

Java Calendar vs Java.util.Date

Shruti Sharma
Ranch Hand

Joined: Mar 25, 2010
Posts: 55
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

Joined: Aug 19, 2009
Posts: 276

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.


OCPJP 6
Kamil Wojcik
Ranch Hand

Joined: May 19, 2008
Posts: 78

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?

SCJP 6.0 Passed | SCWCD Passed | PSM in progress...
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

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.


"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3018
    
  10
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.
 
 
subject: Java Calendar vs Java.util.Date