Java Time Different getFormattedTimeWithDiff(DateFormat dateFormat, long finishTime, long startTime)

Here you can find the source of getFormattedTimeWithDiff(DateFormat dateFormat, long finishTime, long startTime)

Description

Formats time in ms and appends difference (finishTime - startTime) as returned by formatTimeDiff().

License

Open Source License

Parameter

Parameter Description
dateFormat date format to use
finishTime fnish time
startTime start time

Return

formatted value.

Declaration

public static String getFormattedTimeWithDiff(DateFormat dateFormat, long finishTime, long startTime) 

Method Source Code

//package com.java2s;

import java.text.DateFormat;

import java.util.Date;

public class Main {
    /**//ww  w. j  av a2s.c  om
     * Formats time in ms and appends difference (finishTime - startTime) as returned by formatTimeDiff(). If finish time is 0, empty string
     * is returned, if start time is 0 then difference is not appended to return value.
     * 
     * @param dateFormat
     *            date format to use
     * @param finishTime
     *            fnish time
     * @param startTime
     *            start time
     * @return formatted value.
     */
    public static String getFormattedTimeWithDiff(DateFormat dateFormat, long finishTime, long startTime) {
        StringBuffer buf = new StringBuffer();
        if (0 != finishTime) {
            buf.append(dateFormat.format(new Date(finishTime)));
            if (0 != startTime) {
                buf.append(" (" + formatTimeDiff(finishTime, startTime) + ")");
            }
        }
        return buf.toString();
    }

    /**
     * 
     * Given a finish and start time in long milliseconds, returns a String in the format Xhrs, Ymins, Z sec, for the time difference
     * between two times. If finish time comes before start time then negative valeus of X, Y and Z wil return.
     * 
     * @param finishTime
     *            finish time
     * @param startTime
     *            start time
     */
    public static String formatTimeDiff(long finishTime, long startTime) {
        long timeDiff = finishTime - startTime;
        return formatTime(timeDiff);
    }

    public static String toString(String[] content, String sign) {
        if (null == content) {
            return null;
        }

        sign = null == sign ? "," : sign;
        StringBuilder strBuilder = new StringBuilder();
        for (int i = 0; i < content.length; i++) {
            strBuilder.append(content[i]);
            if (i < content.length - 1) {
                strBuilder.append(sign);
            }
        }

        return strBuilder.toString();
    }

    /**
     * 
     * Given the time in long milliseconds, returns a String in the format Xhrs, Ymins, Z sec.
     * 
     * @param timeDiff
     *            The time difference to format
     */
    public static String formatTime(long timeDiff) {
        StringBuffer buf = new StringBuffer();
        long hours = timeDiff / (60 * 60 * 1000);
        long rem = (timeDiff % (60 * 60 * 1000));
        long minutes = rem / (60 * 1000);
        rem = rem % (60 * 1000);
        long seconds = rem / 1000;

        if (hours != 0) {
            buf.append(hours);
            buf.append("hrs, ");
        }
        if (minutes != 0) {
            buf.append(minutes);
            buf.append("mins, ");
        }
        // return "0sec if no difference
        buf.append(seconds);
        buf.append("sec");
        return buf.toString();
    }
}

Related

  1. getDiffDateTime(int diff)
  2. getDifferent(final Date timeFrom, final Date timeTo)
  3. getDiffTime(Date beforeTime, Date afterTime)
  4. getDiffTime(long startTime)
  5. getDiffTime(String strDate, int idx)
  6. getTimeDiff(String start, String end, String format, int timeDiff)
  7. getTimeDiffMin(String format, String startDate, String endDate)
  8. isDateDiff_2(String pattern, String time2, String time1)
  9. isLowerThanMinTimeDiff(Date lastUpdate, Date now, Long minTimeDiff)