Java Time Format formatTimeDifference(long time1, long time2)

Here you can find the source of formatTimeDifference(long time1, long time2)

Description

format Time Difference

License

Open Source License

Declaration

public static String formatTimeDifference(long time1, long time2) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

public class Main {
    public static String formatTimeDifference(long time1, long time2) {
        return formatTime(getTimeDifference(time1, time2));
    }//from w  ww .  j  a v  a 2 s  . c om

    public static String formatTime(long time) {
        StringBuilder ret = new StringBuilder();
        int days = (int) Math.floor(time / (1000 * 3600 * 24));
        int hours = (int) Math.floor((time % (1000 * 3600 * 24)) / (1000 * 3600));
        int minutes = (int) Math.floor((time % (1000 * 3600 * 24)) % (1000 * 3600) / (1000 * 60));
        int seconds = (int) Math.floor(time % (1000 * 3600 * 24) % (1000 * 3600) % (1000 * 60) / 1000);

        if (days != 0)
            ret.append(days + "d");
        if (hours != 0 || days != 0)
            ret.append(hours + "h");
        if (minutes != 0 || hours != 0 || days != 0)
            ret.append(minutes + "m");
        ret.append(seconds + "s");

        return ret.toString();
    }

    public static long getTimeDifference(long time1, long time2) {
        return (time2 - time1);
    }
}

Related

  1. formatTimeArea(Integer hour)
  2. formatTimeDeltaValue(long delta)
  3. formatTimeDiff(long finishTime, long startTime)
  4. formatTimeDiff(long finishTime, long startTime)
  5. formatTimeDifference(long ms)
  6. formatTimeDigital(int time)
  7. formatTimeDuringHour(long mss)
  8. formatTimeElapsed(long p_millis)
  9. formatTimeInfo(long time)