Java computing Date and Time Difference

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.