Example usage for java.time Instant plus

List of usage examples for java.time Instant plus

Introduction

In this page you can find the example usage for java.time Instant plus.

Prototype

private Instant plus(long secondsToAdd, long nanosToAdd) 

Source Link

Document

Returns a copy of this instant with the specified duration added.

Usage

From source file:Main.java

public static void main(String[] args) {
    Instant instant = Instant.parse("2014-12-03T10:15:30.00Z");
    instant = instant.plus(100, ChronoUnit.DAYS);
    System.out.println(instant);//from  www . j a va2  s  .c  o  m

}

From source file:Main.java

public static void main(String[] args) {
    Instant timestamp = Instant.now();
    System.out.println(timestamp);
    System.out.println(timestamp.plus(1, ChronoUnit.HOURS));

}

From source file:Main.java

public static void main(String[] args) {
    Instant t1 = Instant.now();
    long hours = 2;
    long minutes = 30;
    Instant t2 = t1.plus(hours, ChronoUnit.HOURS).plus(minutes, ChronoUnit.MINUTES);

    System.out.println(String.format("now %s and later %s", t1, t2));
}

From source file:Main.java

public static void main(String[] args) {
    Instant t1 = Instant.now();
    long hours = 2;
    long minutes = 30;
    Instant t2 = t1.plus(hours, ChronoUnit.HOURS).plus(minutes, ChronoUnit.MINUTES);

    long minutesBetween = Duration.between(t1, t2).toMinutes();
    System.out.println(minutesBetween);
}

From source file:Main.java

public static void main(String[] args) {
    Instant t1 = Instant.now();
    long hours = 2;
    long minutes = 30;
    Instant t2 = t1.plus(hours, ChronoUnit.HOURS).plus(minutes, ChronoUnit.MINUTES);

    Duration gap = Duration.ofSeconds(13);
    Instant later = t1.plus(gap);
    System.out.println(later);/*from w  ww  .  j  av  a  2 s . co  m*/

    System.out.println(ChronoUnit.MILLIS.between(t1, t2));
}

From source file:Main.java

public static void main(String[] args) {

    // same time in millis
    Instant now = Instant.ofEpochMilli(1262347200000l);

    // native plusSeconds() method to add 10 seconds
    Instant nowPlusTenSeconds = now.plusSeconds(10);

    // no native support for units like days.
    Instant nowPlusTwoDays = now.plus(2, ChronoUnit.DAYS);
    Instant nowMinusTwoDays = now.minus(Duration.ofDays(2));

    System.out.println(nowPlusTenSeconds);
    System.out.println(nowPlusTwoDays);
    System.out.println(nowMinusTwoDays);
}

From source file:com.muk.services.util.GoogleOAuthService.java

private String createClaim(String scope, String privateKeyAlias, String account) {
    final Instant issued = Instant.now();
    final Instant expirePlusHour = issued.plus(50, ChronoUnit.MINUTES);

    final StringBuilder sb = new StringBuilder();

    sb.append("{\"iss\":\"").append(account).append("\",\"scope\":\"").append(scope).append("\",\"aud\":\"")
            .append(JWT_AUD).append("\",\"exp\":").append(expirePlusHour.getEpochSecond()).append(",\"iat\":")
            .append(issued.getEpochSecond()).append(",\"sub\":\"").append(JWT_SUB).append("\"}");

    /*/* ww  w  .  ja  v a  2  s .  c om*/
     * sb.append("{\"iss\":\"").append(account).append("\",\"scope\":\"").append(scope).append("\",\"aud\":\"")
     * .append(JWT_AUD).append("\",\"exp\":").append(expirePlusHour.getEpochSecond()).append(",\"iat\":")
     * .append(issued.getEpochSecond()).append("}");
     */

    String jwt = JWT_HEADER + "." + cryptoService.encodeUrlSafe(sb.toString().getBytes(StandardCharsets.UTF_8));

    try {
        jwt = jwt + "."
                + cryptoService.signature("SHA256withRSA", jwt, keystoreService.getPrivateKey(privateKeyAlias));
    } catch (final IOException ioEx) {
        LOG.error("Failed to access keystore.", ioEx);
    } catch (final GeneralSecurityException secEx) {
        LOG.error("Failed to sign jwt.", secEx);
    }

    return jwt;
}

From source file:io.pivotal.strepsirrhini.chaosloris.reaper.EventReaperTest.java

@Test
public void doReap() {
    Schedule schedule = new Schedule("test-schedule", "test-name");
    this.scheduleRepository.saveAndFlush(schedule);

    Application application = new Application(UUID.randomUUID());
    this.applicationRepository.saveAndFlush(application);

    Chaos chaos = new Chaos(application, 0.1, schedule);
    this.chaosRepository.saveAndFlush(chaos);

    Instant now = Instant.now();

    Event event1 = new Event(chaos, now.minus(3, DAYS), Collections.emptyList(), Integer.MIN_VALUE);
    this.eventRepository.saveAndFlush(event1);

    Event event2 = new Event(chaos, now, Collections.emptyList(), Integer.MIN_VALUE);
    this.eventRepository.saveAndFlush(event2);

    Event event3 = new Event(chaos, now.plus(3, DAYS), Collections.emptyList(), Integer.MIN_VALUE);
    this.eventRepository.saveAndFlush(event3);

    this.eventReaper.doReap().as(StepVerifier::create).expectNext(1L).expectComplete().verify();

    List<Event> events = this.eventRepository.findAll();
    assertThat(events).containsExactly(event2, event3);
}

From source file:io.pivotal.strepsirrhini.chaosloris.data.EventRepositoryTest.java

@Test
public void findByExecutedAtBefore() throws ExecutionException, InterruptedException {
    Schedule schedule = new Schedule("test-schedule", "test-name");
    this.scheduleRepository.saveAndFlush(schedule);

    Application application = new Application(UUID.randomUUID());
    this.applicationRepository.saveAndFlush(application);

    Chaos chaos = new Chaos(application, 0.1, schedule);
    this.chaosRepository.saveAndFlush(chaos);

    Instant now = Instant.now();

    Event event1 = new Event(chaos, now.minus(1, DAYS), Collections.emptyList(), Integer.MIN_VALUE);
    this.eventRepository.saveAndFlush(event1);

    Event event2 = new Event(chaos, now, Collections.emptyList(), Integer.MIN_VALUE);
    this.eventRepository.saveAndFlush(event2);

    Event event3 = new Event(chaos, now.plus(1, DAYS), Collections.emptyList(), Integer.MIN_VALUE);
    this.eventRepository.saveAndFlush(event3);

    List<Event> events = this.eventRepository.findByExecutedAtBefore(now.minus(1, HOURS));
    assertThat(events).containsExactly(event1);
}

From source file:org.haiku.haikudepotserver.security.AuthenticationServiceImpl.java

/**
 * <p>This will return a JWT (java web token) that is signed by a secret that allows for the client to get
 * that token and for it to be used as a form of authentication for some period of time.</p>
 *///  w  w w .  ja  va  2s.  c o  m

@Override
public String generateToken(User user) {

    Preconditions.checkArgument(null != user, "the user must be provided");

    Instant instant = Instant.now();

    JWTClaimsSet claimsSet = new JWTClaimsSet.Builder()
            .subject(user.getNickname() + SUFFIX_JSONWEBTOKEN_SUBJECT)
            .issueTime(new java.util.Date(instant.toEpochMilli()))
            .expirationTime(new java.util.Date(
                    instant.plus(jsonWebTokenExpirySeconds, ChronoUnit.SECONDS).toEpochMilli()))
            .issuer(jsonWebTokenIssuer).build();

    SignedJWT signedJWT = new SignedJWT(new JWSHeader(JWSAlgorithm.HS256), claimsSet);

    try {
        signedJWT.sign(jsonWebTokenSigner);
    } catch (JOSEException je) {
        throw new IllegalStateException("unable to sign a jwt", je);
    }

    return signedJWT.serialize();
}