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
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
Joined: Jan 30, 2000
Is there a way to use Calendar with DateFormat? Only by converting the Calendar back into a Date (using getTime()) as God intended.
Joined: Jun 13, 2003
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