aspose file tools*
The moose likes Java in General and the fly likes Date not fully replaced by Calendar? 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 "Date not fully replaced by Calendar?" Watch "Date not fully replaced by Calendar?" New topic
Author

Date not fully replaced by Calendar?

Michael Brewer
Ranch Hand

Joined: Jun 27, 2002
Posts: 54
I'm constantly entering code like the following into my Java classes:
I do this because after I have extensively perused the JavaDocs for Calendar, I haven't found a way to create a new Calendar object seeded from the system clock or to get a Calendar object from a JDBC source.
I should be able to reduce these three lines to one. Can someone enlighten me to a better way to do this?
[ February 06, 2003: Message edited by: Michael Brewer ]
Maulin Vasavada
Ranch Hand

Joined: Nov 04, 2001
Posts: 1871
hi Michael,
i do following,
Calendar c = Calendar.getInstance();
to get the current system date in Calendar object..
are you referring to something else you want to do?
thanks
maulin
Michael Brewer
Ranch Hand

Joined: Jun 27, 2002
Posts: 54
That's great as far as seeding one from the system clock, thanks!
How about a way to get a Calendar object from a ResultSet?
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Going back to the original question:
I do this because after I have extensively perused the JavaDocs for Calendar, I haven't found a way to create a new Calendar object seeded from the system clock or to get a Calendar object from a JDBC source.
The line
Calendar thisDate = new GregorianCalendar();
also seeds the Calendar with the current time (as read from the system clock of course). See the docs for the GregorianCalendar constructor.
For the ResultSet question - it depends on what sort of data your ResultSet has. You need to know how to use a ResultSet and get data out of it, and how to interpret that data as a date. If you've never used ResultSets before, study some basic JDBC materials first. For more help, please provide more info on what sort of data you have and what you've tried so far.


"I'm not back." - Bill Harding, Twister
Michael Brewer
Ranch Hand

Joined: Jun 27, 2002
Posts: 54
I can get a Date out of a ResultSet using getDate(), but I was hoping for a way to get a Calendar out of a ResultSet. As is, I have to get a Date and use it to set my Calendar.
Maulin Vasavada
Ranch Hand

Joined: Nov 04, 2001
Posts: 1871
hi ,
well, u r first post seems the way out. i don't think u can reduce this operation as u expect....
thanks
maulin
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Drat - sorry I missed your response earlier Michael; the posting time (as seen on the JiG Intermediate page) was so close to when I posted I just assumed my post was still the last one. But Maulin's right - you'll need a couple extra lines of code here. But it's worth noting that often there's no need to create a Calendar at all - it's not so much for storing dates as manipulating them. I think of the relationship between Date and Calendar as similar to that between String and StringBuffer. If I've got a Date and want to revise it somehow (e.g. find the next 9:00 AM Monday morning after the given date) I'll then use a Calendar to make the desired changes - and then getTime() to get a new Date from it. But most of the rest of the program doesn't need a Calendar, it just needs the Date info. The main reasons I do this are (a) the Calendar API is ugly and hard to use, and (b) Calendar uses a lot more memory than is necessary to just store a date. So I try to minimize its occurrance in programs. Often I'll reuse an existing (static field) Calendar rather than creating a new one. So you may want to do someting similar. Or not, or course...
Michael Brewer
Ranch Hand

Joined: Jun 27, 2002
Posts: 54
Originally posted by Jim Yingst:
But it's worth noting that often there's no need to create a Calendar at all - it's not so much for storing dates as manipulating them.

Thanks. By the title of this thread, it should be obvious that at the beginning I thought that Date was fully deprecated (not just a lot of its methods) and that Calendar was the replacement.
I'll still need to use Calendar because it is rare that I use a Date and don't manipulate it in some fashion. Currently, I'm using Calendar's get() method.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Date not fully replaced by Calendar?