Solved

Cannot compare 2 date

Posted on 2002-03-14
6
187 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
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 

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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Apps blocked by Java 9 81
xampp tool 12 51
runtime exception 2 28
Android development question 2 37
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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 how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

809 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