Java Duration Format PrintDuration(long NanoSeconds)

Here you can find the source of PrintDuration(long NanoSeconds)

Description

Print Duration

License

Apache License

Declaration

public static String PrintDuration(long NanoSeconds) 

Method Source Code

//package com.java2s;
/* ===========================================================================
 * Copyright (C) 2015 CapsicoHealth Inc.
 *
 * 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./*from   w  w  w  .j  a v a2  s. com*/
 */

public class Main {
    public static String PrintDuration(long NanoSeconds) {
        long d = (long) Math.floor(NanoSeconds / (24 * 60 * 60 * 1000000000.0));
        NanoSeconds -= d * 24 * 60 * 60 * 1000000000;
        long h = (long) Math.floor(NanoSeconds / (60 * 60 * 1000000000.0));
        NanoSeconds -= h * 60 * 60 * 1000000000;
        long mn = (long) Math.floor(NanoSeconds / (60 * 1000000000.0));
        NanoSeconds -= mn * 60 * 1000000000;
        long s = (long) Math.floor(NanoSeconds / 1000000000.0);
        NanoSeconds -= s * 1000000000;
        long ms = NanoSeconds / 1000000;
        StringBuilder Str = new StringBuilder();
        if (d != 0)
            Str.append(d).append("d");
        if (h != 0 || Str.length() != 0)
            Str.append(Str.length() != 0 ? " " : "").append(h).append("h");
        if (mn != 0 || Str.length() != 0)
            Str.append(Str.length() != 0 ? " " : "").append(mn).append("mn");
        if (s != 0 || Str.length() != 0)
            Str.append(Str.length() != 0 ? " " : "").append(s).append("s");
        if (ms != 0)
            Str.append(Str.length() != 0 ? " " : "").append(ms).append("ms");
        return Str.toString();
    }
}

Related

  1. formatTime(long duration)
  2. formatTime(long duration)
  3. formatTimeDuration(long timeDuration)
  4. formatTS(long duration)
  5. formatWordyDuration(long milliseconds)
  6. PrintDurationConciseFromMs(long MilliSeconds)
  7. PrintDurationMilliSeconds(long NanoSeconds)