Java examples for Date Time:Duration
Using Instant and Duration Classes
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); } }
Human-Scale Date and Time Classes and their Components
Class Name | Date | Time | Zone Offset | Zone Rule |
---|---|---|---|---|
LocalDate | Yes | No | No | No |
LocalTime | No | Yes | No | No |
LocalDateTime | Yes | Yes | No | No |
OffsetTime | No | Yes | Yes | No |
OffsetDateTime | Yes | Yes | Yes | No |
ZonedDateTime | Yes | Yes | Yes | Yes |
ZoneOffset | No | No | Yes | No |
ZoneId | No | No | Yes | Yes |