Java Instant Calculate plus(Instant instant, Duration add)

Here you can find the source of plus(Instant instant, Duration add)

Description

plus

License

Open Source License

Declaration

public static Instant plus(Instant instant, Duration add) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.time.Duration;
import java.time.Instant;

import java.time.temporal.ChronoUnit;

import java.util.Date;

public class Main {
    public static final Duration INF_POSITIVE = ChronoUnit.FOREVER.getDuration();
    public static final Duration INF_NEGATIVE = INF_POSITIVE.negated();
    public static final Instant INF_FUTURE = Instant.MAX;
    public static final Instant INF_PAST = Instant.MIN;

    public static Instant plus(Instant instant, Duration add) {
        if (isInfFuture(instant)) {
            return INF_FUTURE;
        } else if (isInfPast(instant)) {
            return INF_PAST;
        } else if (isInfPositive(add)) {
            return INF_FUTURE;
        } else if (isInfNegative(add)) {
            return INF_PAST;
        } else {/*  w ww  . jav a2s. c o  m*/
            return instant.plus(add);
        }
    }

    public static boolean isInfFuture(Instant instant) {
        return INF_FUTURE.equals(instant);
    }

    public static boolean isInfFuture(Date date) {
        return date.getYear() > 8000; // Hacky, but needs to match Ruby's Time::INF_FUTURE
    }

    public static boolean isInfPast(Instant instant) {
        return INF_PAST.equals(instant);
    }

    public static boolean isInfPast(Date date) {
        return date.getYear() < -10000;
    }

    public static boolean isInfPositive(Duration duration) {
        return INF_POSITIVE.equals(duration);
    }

    public static boolean isInfNegative(Duration duration) {
        return INF_NEGATIVE.equals(duration);
    }
}

Related

  1. getPeriodInstant(LocalDate localDate)
  2. getSnowflakeFromTimestamp(Instant date)
  3. isEqualOrAfterNow(Instant now, Instant instant)
  4. maxInstant(Instant v1, Instant v2)
  5. nanos(Instant instant)
  6. plusMilli(Instant _date, long _milli)
  7. restrictToDaytime(Instant instantToRestrict, Instant thresholdTime, ZoneId userZoneId)
  8. roundUp(final Instant time, final Duration duration)
  9. secondsAgo(Instant end)