Calculate Difference between two times in Android

后端 未结 7 2080
-上瘾入骨i
-上瘾入骨i 2020-11-27 21:47

I have two string variables such as StartTime and EndTime. I need to Calculate the TotalTime by subtracting the EndTime with StartTime.

The Format of StartTime and E

相关标签:
7条回答
  • 2020-11-27 22:33

    Have a look at DateFormat, you can use it to parse your strings with the parse(String source) method and the you can easily manipulate the two Dates object to obtain what you want.

    DateFormat df = DateFormat.getInstance();
    Date date1 = df.parse(string1);
    Date date2 = df.parse(string2);
    long difference = date1.getTime() - date2.getTime();
    
    days = (int) (difference / (1000*60*60*24));  
    hours = (int) ((difference - (1000*60*60*24*days)) / (1000*60*60)); 
    min = (int) (difference - (1000*60*60*24*days) - (1000*60*60*hours)) / (1000*60);
    
    String diffHours = df.format(hours);
    

    For date difference

    Date myDate = new Date(difference);
    

    The to show the Date :

    String diff = df.format(myDate);
    
    0 讨论(0)
提交回复
热议问题