Expand my Community achievements bar.

Don’t miss the AEM Skill Exchange in SF on Nov 14—hear from industry leaders, learn best practices, and enhance your AEM strategy with practical tips.
SOLVED

Calculating date and time duration (in hours, and days and hours)

Avatar

Level 1

Could someone help with displaying the duration between two different dates and times in hours, and days and hours, if possible? I've reviewed almost every post related to this subject (in the forums and elsewhere) and have tried virtually every recommendation out there, but I can't figure it out. I feel like such a loser. Here's the form if anyone can assist. Date and time duration. Thank you in advance.

1 Accepted Solution

Avatar

Correct answer by
Employee Advisor
3 Replies

Avatar

Employee Advisor

Hello @Daniel_Claire 

I would prefer to use suggested java.util.concurrent.TimeUnit [1] class.

String dateStart = "01/03/22 09:29:58";
String dateStop = "11/07/22 09:33:43";

// Custom date format
SimpleDateFormat format = new SimpleDateFormat("yy/MM/dd HH:mm:ss");  

Date d1 = null;
Date d2 = null;
try {
    d1 = format.parse(dateStart);
    d2 = format.parse(dateStop);

    long diff = d2.getTime() - d1.getTime();
    long seconds = TimeUnit.MILLISECONDS.toSeconds(diff);
    long minutes = TimeUnit.MILLISECONDS.toMinutes(diff); 
    long hours = TimeUnit.MILLISECONDS.toHours(diff); 
    long days = TimeUnit.MILLISECONDS.toDays(diff); 

} catch (ParseException e) {
    e.printStackTrace();
}    

Also, see other discussions on the same [2] [3]

 

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

[2] https://stackoverflow.com/questions/15540801/time-difference-program/15541322#15541322 

[3] https://stackoverflow.com/questions/625433/how-to-convert-milliseconds-to-x-mins-x-seconds-in-java 

Avatar

Community Advisor

You can use Java built-in class, TimeUnit.

Date startDate = // Set start date
Date endDate   = // Set end date

long duration  = endDate.getTime() - startDate.getTime();

long diffInSeconds = TimeUnit.MILLISECONDS.toSeconds(duration);
long diffInMinutes = TimeUnit.MILLISECONDS.toMinutes(duration);
long diffInHours = TimeUnit.MILLISECONDS.toHours(duration);
long diffInDays = TimeUnit.MILLISECONDS.toDays(duration);

If you have dates in string format then convert to date and use above logic to find difference in days, hour, minutes etc.

String start = "01/04/22 08:19:51";
String stop = "16/08/22 07:23:54";

SimpleDateFormat format = new SimpleDateFormat("yy/MM/dd HH:mm:ss");  

Date startDate = null;
Date endDate = null;
try {
    startDate = format.parse(start);
    endDate = format.parse(stop);
}catch(ParseException ex){
ex.printStackTrace();
}
long duration = endDate.getTime() - startDate.getTime();

 Hope this helps!

Avatar

Correct answer by
Employee Advisor

A flexible formatting (but not using the Java Time API yet) is provided by https://commons.apache.org/proper/commons-lang/javadocs/api-3.0/org/apache/commons/lang3/time/Durati...