0

I need to get a time between two dates. Let's say dateStart = 1470712122173 and dateStop = 1470712127320. The difference between this two dates equals to 5147 So, according to this I expect to get an answer = 5 seconds but I see 19:00:05. Where these 19 hours come from?

Code for milliseconds ( = 5147) -> time:

private string foo(long dateStart, long dateStop) {
    long diff = dateStop - dateStart;
    DateFormat simple = SimpleDateFormat.getTimeInstance(); 
    Date date = new Date(diff); 
    return simple.format(date);
}

Thank you for explanation.

P. Savrov
  • 1,064
  • 4
  • 17
  • 29
  • 5
    The difference between two time points is not another time point. It's a duration. – xiaofeng.li Aug 09 '16 at 03:24
  • Maybe you are in UTC-5 timezone. You can use Joda lib for time differences or calculate by yourself in seconds, minutes, .. – Ebrahim Pasbani Aug 09 '16 at 03:25
  • @flkes i dont think timezone with +19 exists. I think that its smth with diff value – P. Savrov Aug 09 '16 at 03:25
  • 3
    Nothing to do with timezones. Just what @LukeLee says: `new Date(diff)` is meaningless. Leave `diff` as a `long`, it's just a number of milliseconds, not a date. – Paul Hicks Aug 09 '16 at 03:28

3 Answers3

5

You need to get the basic concepts right. When you take a difference between two Date object, you get the duration between two points in time, trying to view the difference as another time point makes no sense.

Here's a example using the Java 8 time API to get the difference between two points in time (java.time.Instant):

import java.time.Duration;
import java.time.Instant;

public class TimeDifferenceSample {

    static Duration diff(Instant start, Instant end) {
        return Duration.between(start, end);
    }

    public static void main(String [] args) {
        long start = 1470712122173L;
        long end = 1470712127320L;

        Duration dur = diff(Instant.ofEpochMilli(start), Instant.ofEpochMilli(end));
        System.out.println(dur.getSeconds() + " seconds");
    }
}

Output:

5 seconds

For Android, I am not an expert, but you can check The Joda Time Project, which provides similar functions. I also found an Android verion here.

xiaofeng.li
  • 8,237
  • 2
  • 23
  • 30
2
    Date date = new Date(5147);
   //this method has deprecation, just for illustration
    System.out.println(date.getSeconds());

 //in java 8 this works
        LocalDateTime ldate1 = LocalDateTime.ofInstant(Instant.ofEpochMilli(1470712122173l), ZoneId.systemDefault());
        LocalDateTime ldate2 = LocalDateTime.ofInstant(Instant.ofEpochMilli(1470712127320l), ZoneId.systemDefault());

        System.out.println(Duration.between(ldate1, ldate2).getSeconds());
Ramachandran.A.G
  • 4,788
  • 1
  • 12
  • 24
1

You can also use java.util.concurrent.TimeUnit

Output

0 : 0 : 5

Code

import java.util.concurrent.TimeUnit;

public class HelloWorld {

    public static void main(String[] args) {
        long dateStart = 1470712122173l, dateStop = 1470712127320l;
        long diff = dateStop - dateStart;

        long diffInSeconds = TimeUnit.MILLISECONDS.toSeconds(diff);
        long diffInMinutes = TimeUnit.MILLISECONDS.toMinutes(diff);
        long diffInHours = TimeUnit.MILLISECONDS.toHours(diff);

        System.out.println(diffInHours + " : " + diffInMinutes + " : " + diffInSeconds);
    }
}

When you calculate the difference between two date instances, you're really getting the difference seconds. Let's suppose you get 30 seconds as difference.

Now if you will convert this 30 seconds again to date instance (in 12 hour) format, then you'll get the answer as 12 hr 00 minutes 30 seconds PM. So when you calculate the difference between two dates, you shouldn't convert the difference back to the date instance.

Raman Sahasi
  • 30,180
  • 9
  • 58
  • 71