Java TimeUnit Usage toHeaderValue(long timeoutNanos)

Here you can find the source of toHeaderValue(long timeoutNanos)

Description

Serialize the given timeout to a String .

License

Apache License

Declaration

public static String toHeaderValue(long timeoutNanos) 

Method Source Code


//package com.java2s;
/*// w  w w  .  ja  va 2 s  .c om
 * Copyright 2017 LINE Corporation
 *
 * LINE Corporation 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 {
    /**
     * Serialize the given timeout to a {@link String}.
     */
    public static String toHeaderValue(long timeoutNanos) {
        long cutoff = 100000000;
        if (timeoutNanos < 0) {
            throw new IllegalArgumentException("Timeout too small");
        } else if (timeoutNanos < cutoff) {
            return TimeUnit.NANOSECONDS.toNanos(timeoutNanos) + "n";
        } else if (timeoutNanos < cutoff * 1000L) {
            return TimeUnit.NANOSECONDS.toMicros(timeoutNanos) + "u";
        } else if (timeoutNanos < cutoff * 1000L * 1000L) {
            return TimeUnit.NANOSECONDS.toMillis(timeoutNanos) + "m";
        } else if (timeoutNanos < cutoff * 1000L * 1000L * 1000L) {
            return TimeUnit.NANOSECONDS.toSeconds(timeoutNanos) + "S";
        } else if (timeoutNanos < cutoff * 1000L * 1000L * 1000L * 60L) {
            return TimeUnit.NANOSECONDS.toMinutes(timeoutNanos) + "M";
        } else {
            return TimeUnit.NANOSECONDS.toHours(timeoutNanos) + "H";
        }
    }
}

Related

  1. TimeToString(int ticks)
  2. toBias(long offset)
  3. todayInMillis()
  4. toDays(Date date)
  5. todayStart()
  6. toMsTime(final String value)
  7. toString(final Stopwatch watch)
  8. toString(long milliseconds)
  9. toUnixTime(Date date)