Instants and durations allows us to record timestamps and elapsed time.
We can add and subtract a duration from an instant to get another instant.
By adding two durations we can get another duration.
Instant and Duration classes store second and nanosecond separately.
Instant and Duration are more often to work with machine-scale time.
An instant represents a unique moment in time.
An epoch is an instant used as origin to measure other instants. The epoch is at 1970-01-01T00:00:00Z.
The time between two consecutive instants is one nanosecond.
Instants after the epoch have positive values while instants before the epoch have negative values.
The instant at the epoch is assigned a zero value.
We can use Instant.now() to get the current instant using the system default clock.
import java.time.Instant; public class Main { public static void main(String[] args) { Instant instantNow = Instant.now(); System.out.println(instantNow); } }
The code above generates the following result.
You can create an Instant from epoch offset.
The following code creates an Instant
object to
represent 9 seconds from the epoch.
import java.time.Instant; /*from www . j a v a2 s . com*/ public class Main { public static void main(String[] args) { Instant instance9 = Instant.ofEpochSecond(9); System.out.println(instance9); instance9 = Instant.ofEpochSecond(-9); System.out.println(instance9); } }
The code above generates the following result.
Two instants can be compared to know whether one occurs before or after other.
Instants and durations are used for recording timestamps and elapsed time between two events.
Duration
object represents the time span between two instants.
The Duration class can have both positive and negative value.
We can create the Duration class using one of its ofXXX() static factory methods.
import java.time.Duration; //w ww. j a va 2s . co m public class Main { public static void main(String[] args) { Duration d1 = Duration.ofDays(2); System.out.println(d1); Duration d2 = Duration.ofMinutes(2); System.out.println(d2); } }
The code above generates the following result.
Multiplication, division, and negation are applicable to Duration objects.
import java.time.Duration; // w ww. ja va 2s . co m public class Main { public static void main(String[] args) { Duration d = Duration.ofSeconds(200); // 3 minutes and 20 seconds Duration d1 = d.multipliedBy(2); // 6 minutes and 40 seconds Duration d2 = d.negated(); // -3 minutes and -20 seconds System.out.println(d); System.out.println(d1); System.out.println(d2); } }
The code above generates the following result.
The following code shows how how to get second and nanosecond from an Instant:
import java.time.Instant; //www. jav a2 s. com public class Main { public static void main(String[] args) { Instant i1 = Instant.now(); long seconds = i1.getEpochSecond(); System.out.println(seconds); int nanoSeconds = i1.getNano(); System.out.println(nanoSeconds); } }
The code above generates the following result.
The following code shows how to do Instant and Duration calculation.
import java.time.Duration; import java.time.Instant; // w w w .j av a 2 s. co m public class Main { public static void main(String[] args) { Duration d1 = Duration.ofSeconds(55); Duration d2 = Duration.ofSeconds(-17); Instant i1 = Instant.now(); Instant i4 = i1.plus(d1); Instant i5 = i1.minus(d2); Duration d3 = d1.plus(d2); System.out.println(d3); } }
The code above generates the following result.