Java Long Number to Time elapsedTime(long start, long end)

Here you can find the source of elapsedTime(long start, long end)

Description

Calculate the elapsed time between two times specified in milliseconds.

License

Apache License

Parameter

Parameter Description
start The start of the time period
end The end of the time period

Return

a string of the form "XhYmZs" when the elapsed time is X hours, Y minutes and Z seconds or null if start > end.

Declaration

public static String elapsedTime(long start, long end) 

Method Source Code

//package com.java2s;
/*/*from   w  w  w  .  j  a  v a 2 s  .  co m*/
 * Copyright 2013 Ke Meng (mengke@icloud.com)
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.text.NumberFormat;

public class Main {
    private static long[] TIME_FACTOR = { 60 * 60 * 1000, 60 * 1000, 1000 };

    /**
     * Calculate the elapsed time between two times specified in milliseconds.
     *
     * @param start The start of the time period
     * @param end   The end of the time period
     * @return a string of the form "XhYmZs" when the elapsed time is X hours, Y minutes and Z seconds or null if start > end.
     */
    public static String elapsedTime(long start, long end) {
        if (start > end) {
            return null;
        }

        long[] elapsedTime = new long[TIME_FACTOR.length];

        for (int i = 0; i < TIME_FACTOR.length; i++) {
            elapsedTime[i] = start > end ? -1 : (end - start) / TIME_FACTOR[i];
            start += TIME_FACTOR[i] * elapsedTime[i];
        }

        NumberFormat nf = NumberFormat.getInstance();
        nf.setMinimumIntegerDigits(2);
        StringBuffer buf = new StringBuffer();
        for (int i = 0; i < elapsedTime.length; i++) {
            if (i > 0) {
                buf.append(":");
            }
            buf.append(nf.format(elapsedTime[i]));
        }
        return buf.toString();
    }
}

Related

  1. convertTimeUnit(long epochTime, String timeUnit)
  2. convertUnixTime(long unixtime)
  3. convertUnixTimeStampToMySql(long timestamp)
  4. createCookie(String name, String value, String path, String domain, long stateTimeToLive)
  5. createFileDateTimeString(final Long ms)
  6. elapsedTimeToString(long elapsedTime)
  7. formattaData(long date)
  8. formatTime(long date)
  9. formatTime(long ms)