Java Instant add and subtract Duration

Description

Java Instant add and subtract Duration

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

public class Main {
  public static void main(String[] args) {
    Instant i1 = Instant.ofEpochSecond(20);
    Instant i2 = Instant.ofEpochSecond(55);
    System.out.println("i1:" + i1);
    System.out.println("i2:" + i2);

    Duration d1 = Duration.ofSeconds(100);
    Duration d2 = Duration.ofSeconds(100);
    //w  w w. j a v  a2 s  .  c  o  m
    // Add and subtract durations to instants
    Instant i3 = i1.plus(d1);
    Instant i4 = i2.minus(d2);
    System.out.println("i1.plus(d1):" + i3);
    System.out.println("i2.minus(d2):" + i4);
  }
}



PreviousNext

Related