-2

I want time difference in second using new Date().getTime() in java. But i will return me long digit like this 1537865065664. How can i get difference between two Date() object. I will attached few part of my code below.

     Date starttime=new Date();
      long diff=  new Date().getTime() - starttime.getTime()
 System.out.println("hangupClicked New Time :: "+new Date().getTime()+" :: "+starttime.getTime());

Output :: hangupClicked New Time :: 1537865248609 :: 1537865348612

Rahul
  • 131
  • 3
  • 10
  • 1
    `new Date().getTime()` is an ineffecient way of doing `System.currentTimeMillis()` – Peter Lawrey Sep 25 '18 at 11:46
  • Is this what you want? [How do I time a method's execution in Java?](https://stackoverflow.com/questions/180158/how-do-i-time-a-methods-execution-in-java) Also I didn’t really get your problem. Your calculation of the difference looks correct, but you are not printing the difference. – Ole V.V. Sep 25 '18 at 15:06
  • And why use `Date`? The `Date` class is long outdated and has a greater number of design problems. I recommend you either use `System.currentTimeMillis()` and then `TimeUnit` for conversion to seconds, or still nicer `Instant` and `Duration` for finding the difference and converting to seconds. – Ole V.V. Sep 25 '18 at 15:09

5 Answers5

1

The difference obtained using date1.getTime() - date2.getTime() is in milliseconds. You can divide the output by 1000 to get difference in seconds.

(date1.getTime() - date2.getTime())/1000;
S.K.
  • 3,597
  • 2
  • 16
  • 31
1

new Date().getTime() returns time in milliseconds. So if you want in seconds, you need to divide the difference by 1000.

 Date starttime=new Date();
  long diff=  (new Date().getTime() - starttime.getTime())/1000;
Pooja Aggarwal
  • 1,163
  • 6
  • 14
1

java.time

    Instant startTime = Instant.now();
    Duration diff = Duration.between(startTime, Instant.now());
    System.out.println("Difference in seconds: " + diff.getSeconds());

This snippet just printed:

Difference in seconds: 0

This is as expected since there isn’t a full second between the two calls to Instant.now().

What went wrong in your code?

You were calculating the difference in milliseconds between your two Date objects correctly. You did not print the difference and therefore never saw that it was correct. And you did not convert it to seconds.

To illustrate the first point I added this line to your code:

    System.out.println("diff (milliseconds):: " + diff);

Then I got this output from it:

hangupClicked New Time :: 1537890219204 :: 1537890219204
diff (milliseconds):: 0

That said, you shouldn’t use Date in 2018. That class is long outdated and poorly designed at the same time. You should prefer to use java.time, the modern Java date and time API.

Links

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
0

If you want in second then divide the diff by 1000 then you will get in second. Date().getTime() return the value in milliseconds.

Null Pointer
  • 255
  • 1
  • 5
  • 21
0

The return type of getTime() is milliseconds from 01-01-1970 to the specified date. So in order to get difference in seconds use below

(date.getTime() - date2.getTime())/1000;
  • Still I got hangupClicked differenceInSeconds :: 100 hangupClicked New Time :: 1537873474080 :: 1537873374061 – Rahul Sep 25 '18 at 11:07
  • new Date().getTime() returns ==> 1537865065664 – Rahul Sep 25 '18 at 11:25
  • Date starttime=new Date(); long diff= (new Date().getTime() - starttime.getTime())/1000; System.out.println(" Difference in seconds "+diff); – Naveen Rapuru Sep 25 '18 at 11:59