Java TimeUnit Usage durationIsValid(long seconds, int nanos)

Here you can find the source of durationIsValid(long seconds, int nanos)

Description

Returns true if the given number of seconds and nanos is a valid Duration .

License

Apache License

Declaration

private static boolean durationIsValid(long seconds, int nanos) 

Method Source Code

//package com.java2s;
/*//ww  w . ja v a 2 s.c om
 * Copyright 2018 The gRPC Authors
 *
 * 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.util.concurrent.TimeUnit;

public class Main {
    private static final long DURATION_SECONDS_MIN = -315576000000L;
    private static final long DURATION_SECONDS_MAX = 315576000000L;
    private static final long NANOS_PER_SECOND = TimeUnit.SECONDS.toNanos(1);

    /**
     * Returns true if the given number of seconds and nanos is a valid {@code Duration}. The {@code
     * seconds} value must be in the range [-315,576,000,000, +315,576,000,000]. The {@code nanos}
     * value must be in the range [-999,999,999, +999,999,999].
     *
     * <p><b>Note:</b> Durations less than one second are represented with a 0 {@code seconds} field
     * and a positive or negative {@code nanos} field. For durations of one second or more, a non-zero
     * value for the {@code nanos} field must be of the same sign as the {@code seconds} field.
     *
     * <p>Copy of {@link com.google.protobuf.util.Duration#isValid}.</p>
     */
    private static boolean durationIsValid(long seconds, int nanos) {
        if (seconds < DURATION_SECONDS_MIN || seconds > DURATION_SECONDS_MAX) {
            return false;
        }
        if (nanos < -999999999L || nanos >= NANOS_PER_SECOND) {
            return false;
        }
        if (seconds < 0 || nanos < 0) {
            if (seconds > 0 || nanos > 0) {
                return false;
            }
        }
        return true;
    }
}

Related

  1. daysBetween(Date initDate, Date endDate)
  2. daysBetweenDates(long toDate, long fromDate)
  3. daysFromToday(long toDate)
  4. daysToMillis(int days)
  5. durationInSecs(long startNanos, long endNanos)
  6. durationToString(long duration)
  7. durationToString(long millis)
  8. elapsedMicroSec(long startNanoTime)
  9. elapsedTime(long start, long end)