Solved

Cannot compare 2 date

Posted on 2002-03-14
6
190 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

728 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