Java – Time elapsed in days, hours, minutes, seconds

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.

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

  1. Java Date API
  2. Joda-Time – Java date and time API
  3. How To Calculate Elapsed / Execute Time In Java
author image

mkyong

Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities. Read all published posts by

Comments

avatar
3000
newest oldest most voted
Johan
Guest
Johan

Should this
new SimpleDateFormat(“dd/M/yyyy hh:mm:ss”);
not be
new SimpleDateFormat(“dd/M/yyyy HH:mm:ss”);
when parsing time in 24 hour format?

Muhammad Tehseen
Guest
Muhammad Tehseen

I want to ask ending date from the user,How can I do that?

Althaf K Backer
Guest
Althaf K Backer

An example using built in [TimeUnit][1] .

long uptime = System.currentTimeMillis();

long days = TimeUnit.MILLISECONDS.toDays(uptime);
uptime -= TimeUnit.DAYS.toMillis(days);

long hours = TimeUnit.MILLISECONDS.toHours(uptime);
uptime -= TimeUnit.HOURS.toMillis(hours);

long minutes = TimeUnit.MILLISECONDS.toMinutes(uptime);
uptime -= TimeUnit.MINUTES.toMillis(minutes);

long seconds = TimeUnit.MILLISECONDS.toSeconds(uptime);

[1]: https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/TimeUnit.html

Guest
Guest
Guest

Thank you

Mudassir
Guest
Mudassir

Very nice. Very useful.

Sky Blue
Guest
Sky Blue

My seems to crash when I do this?

web design sharjah
Guest
web design sharjah

Sorry it is working now 🙂