This article uses two examples to calculate the time difference between two dates. 1. Use the Java SDK. 2. Use the Joda library. 1. Use the Java SDK to calculate the time difference between two dates. The basic idea is to convert the Date to ms (microseconds) and then calculate the time difference between the two microseconds. The time redemption rules are as follows: 1 s seconds = Ms MS 1 min sub-type = 60 s seconds 1hours hour = 60 min minutes 1day day = 24hours hour package com. qiyadeng. date; import java. text. simpleDateFormat; import java. util. date; public class DateDifferentExample {public static void main (String [] args) {String dateStart = "09:29:58"; String dateStop = "11:31:48 "; simpleDateFormat format = new SimpleDateFormat ("yyyy-MM-dd HH: mm: ss"); Date d1 = null; Date d2 = null; try {d1 = format. parse (dateStart); d2 = format. parse (dateStop); // millisecond ms long diff = d2.getTime ()-d1.getTime (); long diffSeconds = diff/1000% 60; long diffMinutes = diff/(60*1000) % 60; long diffHours = diff/(60*60*1000) % 24; long diffDays = diff/(24*60*60*1000); System. out. print ("two time differences:"); System. out. print (diffDays + "days,"); System. out. print (diffHours + "hour,"); System. out. print (diffMinutes + "minutes,"); System. out. print (diffSeconds + "seconds. ");} catch (Exception e) {e. printStackTrace () ;}} running result: two time differences: 1 day, 2 hours, 1 minute, 50 seconds. 2. joda time library package com. qiyadeng. date; import java. text. simpleDateFormat; import java. util. date; import org. joda. time. dateTime; import org. joda. time. days; import org. joda. time. hours; import org. joda. time. minutes; import org. joda. time. seconds; public class JodaDateDifferentExample {public static void main (String [] args) {String dateStart = "09:29:58"; String dateStop = "11:31:48 "; simpleDateFormat format = new SimpleDateFormat ("yyyy-MM-dd HH: mm: ss"); Date d1 = null; Date d2 = null; try {d1 = format. parse (dateStart); d2 = format. parse (dateStop); DateTime dt1 = new DateTime (d1); DateTime dt2 = new DateTime (d2); System. out. print ("two time differences:"); System. out. print (Days. daysBetween (dt1, dt2 ). getDays () + "day,"); System. out. print (Hours. hoursBetween (dt1, dt2 ). getHours () % 24 + "hour,"); www.2cto.com System. out. print (Minutes. minutesBetween (dt1, dt2 ). getMinutes () % 60 + "minutes,"); System. out. print (Seconds. secondsBetween (dt1, dt2 ). getSeconds () % 60 + "seconds. ");} catch (Exception e) {e. printStackTrace () ;}} running result: two time differences: 1 day, 2 hours, 1 minute, 50 seconds.