Your achievements

Level 1

0% to

Level 2

Tip /
Sign in

Sign in to Community

to gain points, level up, and earn exciting badges like the new
Bedrock Mission!

Learn more

View all

Sign in to view all badges

Adobe Summit 2023 [19th to 23rd March, Las Vegas and Virtual] | Complete AEM Session & Lab list

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


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


Correct answer by
Employee Advisor
0 Replies



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) {

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






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){
long duration = endDate.getTime() - startDate.getTime();

 Hope this helps!


Correct answer by
Employee Advisor

A flexible formatting (but not using the Java Time API yet) is provided by