Java Reference
In-Depth Information
The multipliedBy( ), dividedBy( ), and negated( ) Methods
Multiplication, division, and negation do not make sense on dates and times. They are applicable to the datetime
types that denote an amount of time such as Duration and Period . Durations and periods can be added and
subtracted. The Date-Time API supports negative durations and periods.
Duration d = Duration.ofSeconds(200); // PT3M20S (3 minutes and 20 seconds)
Duration d1 = d.multipliedBy(2); // PT6M40S (6 minutes and 40 seconds)
Duration d2 = d.negated(); // PT-3M-20S (-3 minutes and -20 seconds)
Instants and Durations
A timeline (or a time axis) is a mathematical representation of the passage of time in terms of instantaneous events
along a unique axis. A machine-scale timeline represents the passage of time as a single incrementing number as
shown in Figure 12-1 .
Duration
Duration
Timeline
-8 -7 -6 -5 -4 -3 -2 -1 0 1 2 3 4 5 6 7 8
An instant
Epoch
(Midnight January 1, 1970 UTC)
Figure 12-1. A timeline representing the pasaage of machine-scale time
An instant is a point representing a unique moment in time on a timeline. An epoch is an instant on a timeline
that is used as a reference point (or the origin) to measure other instants.
An object of the Instant class represents an instant on the timeline. It uses a timeline to represent simplified
UTC to a nanosecond precision. That is, the time interval (or duration) between two consecutive instants on the
timeline is one nanosecond. The timeline uses 1970-01-01T00:00:00Z as the epoch. Instants after the epoch have
positive values; instants before the epoch have negative values. The instant at the epoch is assigned a zero value.
There are different ways you can create an instance of the Instant class. Using its now() method, you can get the
current instant using the system default clock.
// Get the current instant
Instant i1 = Instant.now();
You can obtain an instance of the Instant class using an amount of time in different units from the epoch.
The following snippet of code creates an Instant object to represent 19 seconds from the epoch, which represents
1970-01-01T00:00:19Z:
// An instant: 19 seconds from the epoch
Instant i2 = Instant.ofEpochSecond(19);
 
Search WWH ::




Custom Search