java - Difference between two dates (Android) -


i need time between 2 dates. let's datestart = 1470712122173 , datestop = 1470712127320. difference between 2 dates equals 5147 so, according expect answer = 5 seconds see 19:00:05. these 19 hours come from?

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

you need basic concepts right. when take difference between 2 date object, duration between 2 points in time, trying view difference time point makes no sense.

here's example using java 8 time api difference between 2 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, not expert, can check the joda time project, provides similar functions. found android verion here.


Comments