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

Mangu Singh Rajpurohit
  • 10,806
  • 4
  • 68
  • 97
  • Working with dates is hard. The years won't just disappear. Try `Period` in the new Java 8 date classes if you can. – Evan Knowles Mar 24 '16 at 05:43
  • @EvanKnowles, I can't use java 8, I am using java7. – Mangu Singh Rajpurohit Mar 24 '16 at 05:45
  • @user2393267 For Java 7 you can use the [ThreeTen-Backport](http://www.threeten.org/threetenbp/) project, a backport of the bulk of the features in java.time. Built by the same people. Or use [Joda-Time](http://www.joda.org/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. – Basil Bourque Mar 24 '16 at 05:49
  • There's a solution present on SO already, Please look at the link – Debosmit Ray Mar 24 '16 at 05:50

1 Answers1

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)));
}
user2004685
  • 9,548
  • 5
  • 37
  • 54
  • Look at my question properly. I want the difference in time. I don't want in terms of milliseconds. – Mangu Singh Rajpurohit Mar 24 '16 at 05:44
  • @user2393267 You can easily format it in any format you want. Check the updated answer. – user2004685 Mar 24 '16 at 05:48
  • 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 ? – Mangu Singh Rajpurohit Mar 24 '16 at 12:50
  • @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. – user2004685 Mar 24 '16 at 13:22