Using Instant and Duration Classes - Java Date Time

Java examples for Date Time:Duration

Description

Using Instant and Duration Classes

Demo Code

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);
    //  w w  w  .j ava 2 s. com
    Duration d1 = Duration.ofSeconds(55);
    Duration d2 = Duration.ofSeconds(-17);
    System.out.println("d1:" + d1);
    System.out.println("d2:" + d2);
    
    // Compare instants
    System.out.println("i1.isBefore(i2):" + i1.isBefore(i2));
    System.out.println("i1.isAfter(i2):" + i1.isAfter(i2));
    
    // 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);
    
    // Add two durations
    Duration d3 = d1.plus(d2);
    System.out.println("d1.plus(d2):" + d3);
  }
}

Result

Human-Scale Date and Time Classes and their Components

Class Name Date Time Zone Offset Zone Rule
LocalDate YesNo No No
LocalTimeNoYesNo No
LocalDateTime YesYesNo No
OffsetTime NoYesYes No
OffsetDateTime YesYesYes No
ZonedDateTime YesYesYes Yes
ZoneOffset NoNo Yes No
ZoneId NoNo Yes Yes

Related Tutorials