Java – Time elapsed in days, hours, minutes, seconds - LearnHowToCode SarkariResult.com Interview Questions and Answers LearnHowToCodeOnline
corejavatutorial

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.mycareerrepublic.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/2017 11:30:10");
Date date2 = simpleDateFormat.parse("13/10/2017 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 2017
endDate : Sun Oct 13 20:35:55 SGT 2017
different : 291945000
3 days, 9 hours, 5 minutes, 45 seconds

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.mycareerrepublic.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/2017 11:30:10");
Date date2 = simpleDateFormat.parse("13/11/2016 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

About Mariano

I'm Ethan Mariano a software engineer by profession and reader/writter by passion.I have good understanding and knowledge of AngularJS, Database, javascript, web development, digital marketing and exploring other technologies related to Software development.

0 comments:

Featured post

Political Full Forms List

Acronym Full Form MLA Member of Legislative Assembly RSS Really Simple Syndication, Rashtriya Swayamsevak Sangh UNESCO United Nations E...

Powered by Blogger.