Java Time elapsed in days, hours, minutes, seconds
By:Roy.LiuLast updated:2019-08-17
Two Java examples show you how to print the elapsed time in days, hours, minutes and seconds format.
1. Standard JDK Date APIs
You need to calculate the elapsed time manually.
DateTimeUtils.java
package com.mkyong.dateUtils; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; public class DateTimeUtils { public static void main(String[] args) { DateTimeUtils obj = new DateTimeUtils(); SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd/M/yyyy hh:mm:ss"); try { Date date1 = simpleDateFormat.parse("10/10/2013 11:30:10"); Date date2 = simpleDateFormat.parse("13/10/2013 20:35:55"); obj.printDifference(date1, date2); } catch (ParseException e) { e.printStackTrace(); //1 minute = 60 seconds //1 hour = 60 x 60 = 3600 //1 day = 3600 x 24 = 86400 public void printDifference(Date startDate, Date endDate){ //milliseconds long different = endDate.getTime() - startDate.getTime(); System.out.println("startDate : " + startDate); System.out.println("endDate : "+ endDate); System.out.println("different : " + different); long secondsInMilli = 1000; long minutesInMilli = secondsInMilli * 60; long hoursInMilli = minutesInMilli * 60; long daysInMilli = hoursInMilli * 24; long elapsedDays = different / daysInMilli; different = different % daysInMilli; long elapsedHours = different / hoursInMilli; different = different % hoursInMilli; long elapsedMinutes = different / minutesInMilli; different = different % minutesInMilli; long elapsedSeconds = different / secondsInMilli; System.out.printf( "%d days, %d hours, %d minutes, %d seconds%n", elapsedDays, elapsedHours, elapsedMinutes, elapsedSeconds);
Output
startDate : Thu Oct 10 11:30:10 SGT 2013 endDate : Sun Oct 13 20:35:55 SGT 2013 different : 291945000 3 days, 9 hours, 5 minutes, 45 seconds
How about years and months?
It’s very troublesome to calculate the year and month manually, suggest you use the joda time library, see the next example.
It’s very troublesome to calculate the year and month manually, suggest you use the joda time library, see the next example.
2. Joda Time Example
Joda-time is the prefer Java date and time library, it contains APIs to calculate the elapsed time, and get it in years, months, days, hours, minutes and seconds format.
DateTimeUtils.java
package com.mkyong.dateUtils; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; import org.joda.time.Interval; import org.joda.time.Period; public class DateTimeUtils { public static void main(String[] args) { DateTimeUtils obj = new DateTimeUtils(); SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd/M/yyyy hh:mm:ss"); try { Date date1 = simpleDateFormat.parse("10/10/2013 11:30:10"); Date date2 = simpleDateFormat.parse("13/11/2014 20:35:55"); obj.printDifference(date1, date2); } catch (ParseException e) { e.printStackTrace(); public void printDifference(Date startDate, Date endDate){ Interval interval = new Interval(startDate.getTime(), endDate.getTime()); Period period = interval.toPeriod(); System.out.printf( "%d years, %d months, %d days, %d hours, %d minutes, %d seconds%n", period.getYears(), period.getMonths(), period.getDays(), period.getHours(), period.getMinutes(), period.getSeconds());
Output
1 years, 1 months, 3 days, 9 hours, 5 minutes, 45 seconds
References
From:一号门
Previous:Java How to calculate leap year
COMMENTS