Solved

Date Object comparison

Posted on 2004-08-30
8
215 Views
Last Modified: 2010-03-31
hi,
can you please tell me how to find the difference between two date objects.

0
Comment
Question by:roshanmatre
[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
  • 2
  • 2
  • 2
  • +2
8 Comments
 
LVL 86

Accepted Solution

by:
CEHJ earned 30 total points
ID: 11933835
long diff = date1.getTime() - date2.getTime(); // diff in ms
0
 
LVL 2

Assisted Solution

by:bcsonka
bcsonka earned 20 total points
ID: 11934308
Here's a link to a few simple examples . . .

http://www.javaalmanac.com/egs/java.util/CompDates.html
0
 
LVL 2

Expert Comment

by:bcsonka
ID: 11934372
Just a reminder, the examples above are for Calendar objects.  May or may not help you.  But worth a look.
0
SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

 

Expert Comment

by:ipaman
ID: 11934766
java.util;

Date someDate1;
Date someDate2;

int diff = someDate1.compareTo(someDate2);

if( diff < 0 )
{
   //someDate1 is before someDate2
}

if( diff > 0 )
{
   //someDate2 is before someDate1
}

if( diff == 0 )
{
   //the dates are the same
}
0
 
LVL 37

Expert Comment

by:zzynx
ID: 11936241
ipaman,
roshanmatre,

If all you want to know is how the dates relate to each other, you better use the Date functions after(), before() and equals().

- someDate1.before(someDate2) returns true if someDate1 is before someDate2
- someDate1.after(someDate2) returns true if someDate1 is after someDate2
- someDate1.equals(someDate2) returns true if someDate1 equals someDate2 (to the millisecond!)
0
 

Expert Comment

by:ipaman
ID: 11936864
good point zzynx! either way will work. same amount of code. but I believe the compareTo() will be quicker at runtime. function is only called once.
0
 
LVL 3

Expert Comment

by:RuadRauFlessa
ID: 11939231
ipaman that depends on what you want to do with it. If you only want to check one of them no problem then using those are fine. Otherwise use the compareTo() if you want to check for all of the cases. Oh and CEHJ's answer will be faster still cuz it basically does what the compareTo() method does but without that extra stack calls and such needed for the extra method.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12028866
:-)
0

Featured Post

Technology Partners: 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!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
login jsp example 24 105
jsp insert to database example 2 90
Selenium docs api java index 3 98
How to find out if all executorservice threads have finished 2 14
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
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…
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:
Suggested Courses

751 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