Java TimeUnit Usage getFormattedDiffTime(long timeFirst, long timeLast)

Here you can find the source of getFormattedDiffTime(long timeFirst, long timeLast)

Description

Format diff time from milliseconds

License

Apache License

Parameter

Parameter Description
timeFirst long
timeLast long

Return

String

Declaration

public static String getFormattedDiffTime(long timeFirst, long timeLast) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.util.concurrent.TimeUnit;

public class Main {
    /**//from w w w. j  a v a2s .  c o m
     * Format diff time from milliseconds
     *
     * @param timeFirst long
     * @param timeLast  long
     * @return String
     */
    public static String getFormattedDiffTime(long timeFirst, long timeLast) {
        long timeDiff = timeLast - timeFirst;
        long ms = timeDiff % 1000;
        long sec = TimeUnit.MILLISECONDS.toSeconds(timeDiff)
                - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(timeDiff));
        long min = TimeUnit.MILLISECONDS.toMinutes(timeDiff);
        if (min > 29) {
            return "Time diff > 30m";
        } else if (min > 0) {
            return String.format("%dm %ds %dms", min, sec, ms);
        } else if (sec > 0) {
            return String.format("%ds %dms", sec, ms);
        } else {
            return String.format("%dms", ms);
        }
    }
}

Related

  1. getDurationHMS(long millis)
  2. getDurationUnit(Map config)
  3. getElapsedTime(Date start, Date end)
  4. getElapsedTimeString(long elapsed)
  5. getETA(final long seconds)
  6. getFormattedTime(long timeleft)
  7. getFormattedTimeForMS(Long timeInMillis)
  8. getFriendlyTime(long duration)
  9. getInsertionEndIndex(List list, Delayed key)