[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Cannot compare 2 date

Posted on 2002-03-14
6
Medium Priority
?
192 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

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 450 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

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 …
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
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 …
Suggested Courses

656 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