Just as an aside, and in my opinion, using a Calendar to represent Dates is not ideal. An instance of Calendar is useful for manipulating Dates, and those are the methods of Date that have been deprecated. As such, I never have (meaningful) class member veriables of type Calendar -- to me, that is just a utility class. To store Dates, just using Date is all that is needed.
Piscis Babelis est parvus, flavus, et hiridicus, et est probabiliter insolitissima raritas in toto mundo.
Joined: Sep 16, 2005
I can see where a Date can suffice, but remember that a Date doesn't include TimeZone state, while a Calendar does. So if you are replacing a Calendar by a Date, you should include a TimeZone, or at least its time offset.
This is of course only in contexts where you care about time zones!
There is no emoticon for what I am feeling!
Joined: Aug 20, 2001
True, but dates are stored in GMT -- when you print them out, they are automatically converted to your time zone. So a date of 0 milliseconds will print out on the US East Coast as Dec 31, 1969 at 7:00 pm -- because that is 1/1/1970 0:00 GMT. So in order to see what that time is in different timezones, you need to use the Calendar (or change the timezone on your computer). Again, Calendar for manipulation, Date for storing. It's not that often that you actually need to store June 20, 1976 at 10:00 pm EDT -- simply storing the universal time of June 21, 1976 02:00 GMT will suffice and will be converted accordingly.
Dates represent a point in time -- they are universal. In general, this is all that you need.
(This is just me continuing my rant on the overuse of Calendars, since it has been my experience that people don't recognize this fact about Dates...)
I have been wanting to try Joda-Time for some time now. You may want to jave a look at tha. Using java util Date/Calender, especially when doing a lot of Date mathematics can get very messy.
Joined: Sep 16, 2005
Originally posted by Joel McNary: So in order to see what that time is in different timezones, you need to use the Calendar (or change the timezone on your computer).
Your point is well-taken. I realize now that I am a calendar abuser!
But I do have a small point to make about the above statement. I assume that to "see" what the time is, is referring to formatting the date as a string. While you can change the default timezone in your JVM by calling TimeZone.setDefault(timeZone) (if you really want to do that!), the cleaner way is to use a DateFormat and explicitly set its TimeZome property:
Note that I'm using no Calendars! DateFormat's API doesn't mention Calendar, which can be a source of bugs in code, because you need to extract both Date and TimeZone from Calendar and pass them separately to DateFormat for it to render the Calendar in the calendar's time zone.
Joined: Nov 06, 2005
Thanks to everyone for their helpful and detailed replies. It has all be most useful and informative.