• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Date vs Calendar

 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The format method in DateFormat appears to take a Date and not a Calendar. Is there a way to use Calendar with DateFormat?
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Jim Yingst
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic