Java TimeUnit Usage convertMilisecondsToDetail(long ms)

Here you can find the source of convertMilisecondsToDetail(long ms)

Description

convert Miliseconds To Detail

License

Apache License

Declaration

public static String convertMilisecondsToDetail(long ms) 

Method Source Code

//package com.java2s;
/*/* w  ww  .  j  a  v  a  2s.c  o m*/
 * Copyright (c)2016 General Networks Corporation 
 * 
 * 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. 
 * 
 * The use of the Apache License does not indicate that this project is 
 * affiliated with the Apache Software Foundation. 
 */

import java.util.concurrent.TimeUnit;

public class Main {
    public static String convertMilisecondsToDetail(long ms) {
        String detail = String.format("%d days, %d hours, %d min and %d sec(s)", TimeUnit.MILLISECONDS.toDays(ms),
                TimeUnit.MILLISECONDS.toHours(ms) - TimeUnit.DAYS.toHours(TimeUnit.MILLISECONDS.toDays(ms)),
                TimeUnit.MILLISECONDS.toMinutes(ms) - TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(ms)),
                TimeUnit.MILLISECONDS.toSeconds(ms)
                        - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(ms)));
        // unpretty hack for runs that take less than 1 second - need to replace
        if (detail.equals("0 days, 0 hours, 0 min and 0 sec(s)")) {
            detail = "0 days, 0 hours, 0 min and 1 sec";
        }
        return detail;
    }
}

Related

  1. calculateDaysBetween(final Calendar start, final Calendar end)
  2. calculateRuntime(long startTime, long endTime)
  3. calculateTime(final int seconds)
  4. calculateTime(float seconds)
  5. cancelledFuture()
  6. convertMillis(long millis)
  7. convertMillisecondsToHHMMSS(long milliseconds)
  8. convertNanosecondTimespanToHumanReadableFormat(long aTimespan, boolean aShortFormat, boolean aLongFormat)
  9. convertNanoToSeconds(long nanoTime)