Java TimeUnit Usage 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;
/**//ww w  .j a v  a 2s.  com
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You 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.util.concurrent.TimeUnit;

public class Main {
    /**
     * 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;
        }
        return secondsToHMS((end - start) / 1000);
    }

    /**
     * Show time in seconds as hours, minutes and seconds (hh:mm:ss)
     * 
     * @param seconds
     *          (elapsed) time in seconds
     * @return human readable time string "hh:mm:ss"
     */
    public static String secondsToHMS(long seconds) {
        long hours = TimeUnit.SECONDS.toHours(seconds);
        long minutes = TimeUnit.SECONDS.toMinutes(seconds)
                % TimeUnit.HOURS.toMinutes(1);
        seconds = TimeUnit.SECONDS.toSeconds(seconds)
                % TimeUnit.MINUTES.toSeconds(1);
        return String.format("%02d:%02d:%02d", hours, minutes, seconds);
    }
}

Related

  1. durationInSecs(long startNanos, long endNanos)
  2. durationIsValid(long seconds, int nanos)
  3. durationToString(long duration)
  4. durationToString(long millis)
  5. elapsedMicroSec(long startNanoTime)
  6. elapsedTimeSince(Date d)
  7. format(long elapsed, boolean hours)
  8. formatDuration(long duration)
  9. formatDuration(long millis)