This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Java in General and the fly likes comparing dates Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "comparing dates" Watch "comparing dates" New topic
Author

comparing dates

D Wynn
Ranch Hand

Joined: May 03, 2004
Posts: 38
Hi--
I have a question I want to compare dates in a java servlet one date I'm getting off the a database and the other date I'm using the Gregorian Calendar to go back 48 hours to get the other date. Right now when I get the date off the database I save it in string array and the other I was saving as a string also (when I first wrote the pgm I didn't need to compare date within the pgm I just used to date for my sql now I need to compare the two dates in the pgm). What's the easiest way to now compare the dates...the date coming from the db is used a lot already in the pgm as a string. Is there a way for me to cast both dates to compare them or do I need to look at storing it another way of the db?

thanks for your help!
todd runstein
Ranch Hand

Joined: Feb 15, 2005
Posts: 64
Perhaps I'm missing something here . . . could you just create a static method that takes two String objects, tries to convert them to dates, and returns the difference (in milliseconds, or whatever you want). That way you don't have to change the rest of your application, but you can do the comparison.

Again, I may be missing something. It seems that my answer is pretty simplistic, so perhaps there more to the problem than I'm reading in your post.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

If I were you, when I got the dates from the database I would keep them as dates (i.e. java.util.Date or java.sql.Date or java.util.Calendar or something like that). You'll notice that those classes all have "before" and "after" methods which are specifically designed for comparing dates or timestamps.

When you convert a date to a string, you make it much harder to compare to other dates. (Unless you use the ISO format which looks like YYYY-MM-DD, but you didn't, did you?) So keep the dates as dates, and format them into strings when it comes time to display them to a person.
D Wynn
Ranch Hand

Joined: May 03, 2004
Posts: 38
I really don't want to touch my other code with it being so late in the game. So you're saying what I could do is convert the date strings first and then compare them or just compare the date stings without converting them? How can I do this?
Scott Johnson
Ranch Hand

Joined: Aug 24, 2005
Posts: 518
No, don't compare dates stored as strings. That will cause more problems than it solves.

Paul's response is the best solution. Get the date from the database as a Date.

The next best solution is to convert the date string back to a Date object and compare Date objects. You can convert a date stored in a String to a Date by using the SimpleDateFormat class.
D Wynn
Ranch Hand

Joined: May 03, 2004
Posts: 38
Question when I try and parse a string I get the wrong date what is going on..here is the code...
SimpleDateFormat sdf = new SimpleDateFormat("mm-dd-yyyy hh:mm");
java.util.Date date = null;
java.util.Date date2 = null;
java.util.Date date3 = null;

try {
out.println("B " + tstStr);
out.println("B " + schedDeptDateArr[0]);

date = sdf.parse(tstStr);
date2 = sdf.parse(schedDeptDateArr[0]);
date3 = sdf.parse("08-15-2006 12:15");

out.println("Date " + date);
out.println("Date2 " + date2);
out.println("Date3 " + date3);

I'm getting this output...
B 8-18-2006 3:41 B 2006-08-19 00:00:00.0 Date Wed Jan 18 03:41:00 CST 2006 Date2 Sun Jan 08 00:00:00 CST 0019 Date3 Sun Jan 15 00:15:00 CST 2006
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
"mm-dd-yyyy hh:mm"

do you really want 'minutes' at the start, and end
D Wynn
Ranch Hand

Joined: May 03, 2004
Posts: 38
The the dates I'm trying to parse are Aug dates I'm confused as to why the parsed dates I get back are Jan. dates. I must be missing something
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18545
    
  40

Originally posted by D Wynn:
The the dates I'm trying to parse are Aug dates I'm confused as to why the parsed dates I get back are Jan. dates. I must be missing something


Please re-read Michael's (the previous) post -- basically, you are processing the month field as minutes. It is January, because that is the default value when it is not assigned.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: comparing dates
 
Similar Threads
Comparing two Dates
What fields does java.util.Date.equals compare?
Help with MS Access & Date
I need some dating advice
dates, strings, and confusion