GeeCON Prague 2014*
The moose likes Java in General and the fly likes Date vs Calendar Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Java in General
Bookmark "Date vs Calendar" Watch "Date vs Calendar" New topic
Author

Date vs Calendar

E Weibust
Ranch Hand

Joined: Jun 13, 2003
Posts: 54
I am playing around and want to display my default TZ, and GMT on the cmd line. Using Date w/ DateFormat seems to be the way to go.
When and why would a person use Calendar? I know this is generic, but how about the main reasons for Calendar over Date, because what I'm reading sounds like Sun would perfer the use of Calendar.
Thanks


---<br />Erik Weibust<br /><a href="http://erik.weibust.net" target="_blank" rel="nofollow">http://erik.weibust.net</a>
Don Kiddick
Ranch Hand

Joined: Dec 12, 2002
Posts: 580
I think generally it is better to use Calendar - they're easier to manipulate. It's quite easy to go from Calendar to Date and vice-versa, using the getTime() and setTime() methods.
D.
E Weibust
Ranch Hand

Joined: Jun 13, 2003
Posts: 54
The format method in DateFormat appears to take a Date and not a Calendar. Is there a way to use Calendar with DateFormat?
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
I try to avoid Calendar as much as possible. It has an API from hell - far more complex than I need most of the time. I favor using the simplest classes that accomplish what I need - usually that's Date and SimpleDateFormat. My code will be easier for others to understand if I don't use classes with a lot of unnecessary methods. And it's extremely counterintuitive to me that (for example) the MONTH field starts at 0 (Jan) while DAY_OF_MONTH and DAY_OF_WEEK start at 1. Plus, a GregorianCalendar instance has a lot more instance data than a Date does. For a single object it's no big deal - but if you have many thousands of rows of table data you want to store in memory, you want to store dates using Date rather than Calendar, or the memory difference will be notable.
Having said that, what is Calendar good for? Well, anything that depends on an understanding of human calendar systems. (Except for parsing and formatting dates, which is best done by SimpleDateFormat.) Examples: is today a weekday? When does the first Tuesday of the next month occur? Is this a leap year? How can I take the current date and set the time part to 5:00 P.M.? These are all questions that are easiest to answer with a Calendar.
I think of the relationship between Calendar and Date as being similar to that between StringBuffer and String. You use a StringBuffer to manipulate a string and change its value - but most of the time when you're done manipulating, you call toString() and just save the equivalent String value after that. Likewise, use a Calendar to change date information when you need to - but when you're done, use getTime() to extract the equivalent Date obejct, which will be simpler for everyone to understand.
Note that Date really should have been immutable (like String), but it isn't. Sun engineers have acknowledged this was a mistake, but they can't really correct it now, since the API has been released to the public. They might one day deprecate setTime() I suppose; hasn't happened yet though.


"I'm not back." - Bill Harding, Twister
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Is there a way to use Calendar with DateFormat?
Only by converting the Calendar back into a Date (using getTime()) as God intended.
E Weibust
Ranch Hand

Joined: Jun 13, 2003
Posts: 54
I hate Calendar! And I think I might hate TimeZone!
For right now, I want to print to "times" on a line. One that is in CST, and one in GMT, and for the life of me I can't get a GMT time. I'm using stuff like:
Calendar c1 = Calendar.getInstance(TimeZone.getTimeZone("GMT-0"));
and it isn't working. Could you tell me how to conver a Date in my local timezone (CST) to (GMT)?
java.util.Date date = new java.util.Date();
System.out.println(date);
// now I want date in GMT. and I'd like to print
//System.out.println(date + " " + date2);
// with date2 being GMT
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Date vs Calendar