File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes I need some dating advice Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "I need some dating advice" Watch "I need some dating advice" New topic

I need some dating advice

Mike Southgate
Ranch Hand

Joined: Jul 18, 2003
Posts: 183
I've created some classes that get dates from an xml file. I'm using Date() to store the data and SimpleDateFormat to parse it.

I then need to add a number of seconds to the Date and compare it to another Date. Right now I'm using GregorianCalendar.add() and then converting back to a Date. I then use Date.after to compate them.

This all seems a bit complex to me. Might I not be further ahead to store the dates as longs, then simply add (seconds * 1000)? I could compare directly and would only need to treat them as dates on first reading them from the xml, and when displaying to the users.

what is the best practice here?


ms<br />SCJP, SCJD
Greg T Robertson
Ranch Hand

Joined: Nov 18, 2003
Posts: 91
If you're converting from a date to a calendar already - why not just compare the calendar objects (before/after). As a word of warning (because I got nailed by it recently), depending upon how you are reading your dates from XML, database, etc - you have to be careful of TimeZones because your dates can lose some hours. IMHO it's easiest to parse the date field as a Calendar from the XML, then always use Calendar for everything (add, and compares). Of course I'm using JAXB which reads dates from XML as Calendar (at least for the fields I'm using).
Gaurav Saxena

Joined: May 28, 2002
Posts: 29
Dating advice? Go to msn personlas or yahoo personals dude!!... Just kidding. Yeah i agree with whatever Greg said.It's best to use GregorianCalendar for Date related functions.

Gaurav Saxena<br />Programmer Analyst<br />Portland, Oregon
I agree. Here's the link:
subject: I need some dating advice
It's not a secret anymore!