Solved

Cannot compare 2 date

Posted on 2002-03-14
6
185 Views
Last Modified: 2010-03-31
I have folllowing code:
  if(rs.next()){
                rs.getString("last_upd_date");
                SimpleDateFormat df = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
                Date lastDate = df.parse(rs.getString("last_upd_date"));
                System.out.println("lastDate : " + lastDate.toString() + "  date " + date);

                //result = lastDate.before(date);
                System.out.println("result " + date.after(lastDate) + " " + date.equals(lastDate) +" " date.before(lastDate));
                result = (date.after(lastDate) && !date.equals(lastDate));
                System.out.println("result " + result);
            }

the result is :
lastDate : Tue Mar 12 10:35:15 CST 2002  date Tue Mar 12 10:35:15 CST 2002
result true false false


Although the I print the string of date and lastDate with same value, it cannot compare they are equal and after.
why? how to correct?
0
Comment
Question by:ctlo
6 Comments
 
LVL 16

Expert Comment

by:Peter Kwan
ID: 6867373
What is your definition of variable "date"?
0
 

Expert Comment

by:cini_gm
ID: 6867484
You are not specified about the 'date'. Convert the 'date' variable also to the same format. ie, the format u specified in the SimpleDateFormat.
0
 
LVL 3

Expert Comment

by:daitt
ID: 6867638
you only show to second. The variable "date" might have some extra milliseconds that make it "after" the value of "lastDate"
0
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 

Expert Comment

by:sandro901
ID: 6869443
What means with miliseconds is the code of java.util.Date.equals(Object), it says:

...
return obj instanceof Date && getTime() == ((Date) obj).getTime();
...

Verify if you date have (date1.getTime() == date2.getTime()).

Or visualy do System.out.println(date1.getTime() + "--" + date2.getTime()
0
 

Author Comment

by:ctlo
ID: 6876103
If I also want to compare the 2 days is after or beofre , what can I do ?
P.S the definition of variable "date" is also Date with SimpleDateFormat df .
thx
0
 

Accepted Solution

by:
sandro901 earned 150 total points
ID: 6876988
The type of your response in a request to SQL is java.sql.Date, which was derived of java.util.Date.

If you have necessity to compare at milliseconds, then its working now, you simply don't know the value of millisecond, but, it's there and the compare is working fine.

If you need to compare, at maximum in seconds, or other level, I think you must use SimpleDateFormat to make a filter in the Date, obtaining a String, then transform again to date, and then, compare the two dates generated from SimpleDateFormat.
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now