Java TimeUnit Usage getElapsedTime(Date start, Date end)

Here you can find the source of getElapsedTime(Date start, Date end)

Description

get Elapsed Time

License

Open Source License

Declaration

public static String getElapsedTime(Date start, Date end) 

Method Source Code

//package com.java2s;
/*/*from ww w .j  av  a 2s . c  om*/
 * Copyright (c) 2012-2014, John Campbell and other contributors.  All rights reserved.
 *
 * This file is part of Tectonicus. It is subject to the license terms in the LICENSE file found in
 * the top-level directory of this distribution.  The full list of project contributors is contained
 * in the AUTHORS file found in the same location.
 *
 */

import java.util.Date;
import java.util.concurrent.TimeUnit;

public class Main {
    public static String getElapsedTime(Date start, Date end) {
        long duration = end.getTime() - start.getTime();

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

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

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

        final long seconds = TimeUnit.MILLISECONDS.toSeconds(duration);

        String result = "";
        if (days > 0) {
            result += days + " days ";
        }
        if (hours > 0) {
            result += hours + " hours ";
        }
        if (minutes > 0) {
            result += minutes + " minutes ";
        }
        return result + seconds + " seconds";
    }
}

Related

  1. getDurationFromMillis(long millis)
  2. getDurationFromMilliseconds(long duration)
  3. getDurationFromTwoDates(Date startTime, Date endTime)
  4. getDurationHMS(long millis)
  5. getDurationUnit(Map config)
  6. getElapsedTimeString(long elapsed)
  7. getETA(final long seconds)
  8. getFormattedDiffTime(long timeFirst, long timeLast)
  9. getFormattedTime(long timeleft)