-1

I am new to java. I am having slight confusion regarding date arithmetic in java. I have following scenario, where I want to find differences between two dates :-

java.util.Date objDt1 = getDate1FromSrc1();  // I am obtaining it from src1
java.util.Date objDt2 = getDate2FromOtherSrc(); // I am getting dt2 by other way.

Now, I want to find difference between two dates and the output must be other date object.

So, I have written the following code :-

Calendar objCal1     = Calendar.getInstance();
Calendar objCal2     = Calendar.getInstance();
objCal1.setTime(objDt1);
objCal2.setTime(objDt2);

objCal1.add(Calendar.DAY_OF_MONTH,  -objCal2.get(Calendar.DAY_OF_MONTH));
objCal1.add(Calendar.MONTH,         -objCal2.get(Calendar.MONTH));
objCal1.add(Calendar.YEAR,          -objCal2.get(Calendar.YEAR));
objCal1.add(Calendar.HOUR_OF_DAY,   -objCal2.get(Calendar.HOUR_OF_DAY));
objCal1.add(Calendar.MINUTE,        -objCal2.get(Calendar.MINUTE));
objCal1.add(Calendar.SECOND,        -objCal2.get(Calendar.SECOND));

java.util.Date objDiff = objCal1.getTime();

But, I am getting some wierd results. Eg. If objDt1 is "02/22/2016 09:00:00" and objDt2 is "02/22/2016 11:00:00", then I am expecting objDiff to be "02:00:00" as output, which I am not getting.

Can you suggest me what's wrong I am doing here and what's the right way to approach this problem ?

Thanks in advance.

4
  • Working with dates is hard. The years won't just disappear. Try Period in the new Java 8 date classes if you can. Commented Mar 24, 2016 at 5:43
  • @EvanKnowles, I can't use java 8, I am using java7. Commented Mar 24, 2016 at 5:45
  • @user2393267 For Java 7 you can use the ThreeTen-Backport project, a backport of the bulk of the features in java.time. Built by the same people. Or use Joda-Time, the framework that inspired java.time (also built by the same people). The old date-time classes really are so bad that you should avoid them. Commented Mar 24, 2016 at 5:49
  • There's a solution present on SO already, Please look at the link Commented Mar 24, 2016 at 5:50

1 Answer 1

1

In order to find the difference between two dates you can simply convert it to long as follows:

java.util.Date objDt1 = getDate1FromSrc1();
java.util.Date objDt2 = getDate2FromOtherSrc();

long date1 = objDt1.getTime();
long date2 = objDt2.getTime();
System.out.println("Difference (In Seconds): " + (date2 - date1)/1000);

If you want this difference to be in HH:mm:ss format then you can do something like this:

public static void main (String[] args) throws Exception
{
    SimpleDateFormat sf = new SimpleDateFormat("HH:mm:ss");
    java.util.Date objDt1 = getDate1FromSrc1();
    java.util.Date objDt2 = getDate2FromOtherSrc();

    long date1 = objDt1.getTime();
    long date2 = objDt2.getTime();
    System.out.println("Difference: " + sf.format(new Date((date2 - date1)/1000)));
}
Sign up to request clarification or add additional context in comments.

4 Comments

Look at my question properly. I want the difference in time. I don't want in terms of milliseconds.
@user2393267 You can easily format it in any format you want. Check the updated answer.
For System.out.println("Difference: " + objFmt11.format(new Date(-7200000/1000))); your code gives incorrect output. I am expecting here -2:00:00 as output. But it's giving me something wierd. Can you give explanation for the same ?
@user2393267 First of all it should be -7200000 and not -7200000/1000 if you are expecting that output. Also, time is non-negative and if you are using HH:mm:ss then it will show you 22:00:00 i.e. 24-2 of the previous day on a 24-hour clock.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.