Java instant minus day

Java instant minus day

An instantaneous point on the time-line. This class models a single instantaneous point on the time-line. This might be used to record event time-stamps in the application. The range of an instant requires the storage of a number larger than a long . To achieve this, the class stores a long representing epoch-seconds and an int representing nanosecond-of-second, which will always be between 0 and 999,999,999. The epoch-seconds are measured from the standard Java epoch of 1970-01-01T00:00:00Z where instants after the epoch have positive values, and earlier instants have negative values. For both the epoch-second and nanosecond parts, a larger value is always later on the time-line than a smaller value.

Time-scale

  • the Java Time-Scale shall closely match the underlying international civil time scale;
  • the Java Time-Scale shall exactly match the international civil time scale at noon each day;
  • the Java Time-Scale shall have a precisely-defined relationship to the international civil time scale.

For the segment from 1972-11-03 (exact boundary discussed below) until further notice, the consensus international time scale is UTC (with leap seconds). In this segment, the Java Time-Scale is identical to UTC-SLS. This is identical to UTC on days that do not have a leap second. On days that do have a leap second, the leap second is spread equally over the last 1000 seconds of the day, maintaining the appearance of exactly 86400 seconds per day.

For the segment prior to 1972-11-03, extending back arbitrarily far, the consensus international time scale is defined to be UT1, applied proleptically, which is equivalent to the (mean) solar time on the prime meridian (Greenwich). In this segment, the Java Time-Scale is identical to the consensus international time scale. The exact boundary between the two segments is the instant where UT1 = UTC between 1972-11-03T00:00 and 1972-11-04T12:00.

Implementations of the Java time-scale using the JSR-310 API are not required to provide any clock that is sub-second accurate, or that progresses monotonically or smoothly. Implementations are therefore not required to actually perform the UTC-SLS slew or to otherwise be aware of leap seconds. JSR-310 does, however, require that implementations must document the approach they use when defining a clock representing the current instant. See Clock for details on the available clocks.

Читайте также:  Send php email with gmail

The Java time-scale is used for all date-time classes. This includes Instant , LocalDate , LocalTime , OffsetDateTime , ZonedDateTime and Duration .

This is a value-based class; use of identity-sensitive operations (including reference equality ( == ), identity hash code, or synchronization) on instances of Instant may have unpredictable results and should be avoided. The equals method should be used for comparisons.

Источник

java.time.Instant.minus() Method Example

The java.time.Instant.minus(long amountToSubtract, TemporalUnit unit) method returns a copy of this instant with the specified amount subtracted.

Declaration

Following is the declaration for java.time.Instant.minus(long amountToSubtract, TemporalUnit unit) method.

public Instant minus(long amountToSubtract, TemporalUnit unit)

Parameters

  • amountToSubtract − the amount of the unit to subtract from the result, may be negative.
  • unit − the unit of the amount to subtract, not null.

Return Value

an Instant based on this instant with the specified amount subtracted, not null.

Exceptions

  • DateTimeException − if the subtraction cannot be made.
  • UnsupportedTemporalTypeException − if the unit is not supported.
  • ArithmeticException − if numeric overflow occurs.

Example

The following example shows the usage of java.time.Instant.minus(long amountToSubtract, TemporalUnit unit) method.

package com.tutorialspoint; import java.time.Instant; import java.time.temporal.ChronoUnit; public class InstantDemo < public static void main(String[] args) < Instant instant = Instant.parse("2017-02-03T10:37:30.00Z"); Instant result = instant.minus(10, ChronoUnit.MINUTES); System.out.println(result); >>

Let us compile and run the above program, this will produce the following result −

Источник

Class Instant

This class models a single instantaneous point on the time-line. This might be used to record event time-stamps in the application.

The range of an instant requires the storage of a number larger than a long . To achieve this, the class stores a long representing epoch-seconds and an int representing nanosecond-of-second, which will always be between 0 and 999,999,999. The epoch-seconds are measured from the standard Java epoch of 1970-01-01T00:00:00Z where instants after the epoch have positive values, and earlier instants have negative values. For both the epoch-second and nanosecond parts, a larger value is always later on the time-line than a smaller value.

Time-scale

The length of the solar day is the standard way that humans measure time. This has traditionally been subdivided into 24 hours of 60 minutes of 60 seconds, forming a 86400 second day.

Читайте также:  Java lang illegalargumentexception target values must be absolute

Modern timekeeping is based on atomic clocks which precisely define an SI second relative to the transitions of a Caesium atom. The length of an SI second was defined to be very close to the 86400th fraction of a day.

Unfortunately, as the Earth rotates the length of the day varies. In addition, over time the average length of the day is getting longer as the Earth slows. As a result, the length of a solar day in 2012 is slightly longer than 86400 SI seconds. The actual length of any given day and the amount by which the Earth is slowing are not predictable and can only be determined by measurement. The UT1 time-scale captures the accurate length of day, but is only available some time after the day has completed.

The UTC time-scale is a standard approach to bundle up all the additional fractions of a second from UT1 into whole seconds, known as leap-seconds. A leap-second may be added or removed depending on the Earth’s rotational changes. As such, UTC permits a day to have 86399 SI seconds or 86401 SI seconds where necessary in order to keep the day aligned with the Sun.

The modern UTC time-scale was introduced in 1972, introducing the concept of whole leap-seconds. Between 1958 and 1972, the definition of UTC was complex, with minor sub-second leaps and alterations to the length of the notional second. As of 2012, discussions are underway to change the definition of UTC again, with the potential to remove leap seconds or introduce other changes.

Given the complexity of accurate timekeeping described above, this Java API defines its own time-scale, the Java Time-Scale.

The Java Time-Scale divides each calendar day into exactly 86400 subdivisions, known as seconds. These seconds may differ from the SI second. It closely matches the de facto international civil time scale, the definition of which changes from time to time.

  • the Java Time-Scale shall closely match the underlying international civil time scale;
  • the Java Time-Scale shall exactly match the international civil time scale at noon each day;
  • the Java Time-Scale shall have a precisely-defined relationship to the international civil time scale.

For the segment from 1972-11-03 (exact boundary discussed below) until further notice, the consensus international time scale is UTC (with leap seconds). In this segment, the Java Time-Scale is identical to UTC-SLS. This is identical to UTC on days that do not have a leap second. On days that do have a leap second, the leap second is spread equally over the last 1000 seconds of the day, maintaining the appearance of exactly 86400 seconds per day.

Читайте также:  Wordpress php in page template

For the segment prior to 1972-11-03, extending back arbitrarily far, the consensus international time scale is defined to be UT1, applied proleptically, which is equivalent to the (mean) solar time on the prime meridian (Greenwich). In this segment, the Java Time-Scale is identical to the consensus international time scale. The exact boundary between the two segments is the instant where UT1 = UTC between 1972-11-03T00:00 and 1972-11-04T12:00.

Implementations of the Java time-scale using the JSR-310 API are not required to provide any clock that is sub-second accurate, or that progresses monotonically or smoothly. Implementations are therefore not required to actually perform the UTC-SLS slew or to otherwise be aware of leap seconds. JSR-310 does, however, require that implementations must document the approach they use when defining a clock representing the current instant. See Clock for details on the available clocks.

The Java time-scale is used for all date-time classes. This includes Instant , LocalDate , LocalTime , OffsetDateTime , ZonedDateTime and Duration .

This is a value-based class; programmers should treat instances that are equal as interchangeable and should not use instances for synchronization, or unpredictable behavior may occur. For example, in a future release, synchronization may fail. The equals method should be used for comparisons.

Источник

Java Data Type How to — Plus and Minus days and seconds from Instant

We would like to know how to plus and Minus days and seconds from Instant.

Answer

/*from w w w .j a v a 2 s. co m*/ import java.time.Duration; import java.time.Instant; import java.time.temporal.ChronoUnit; public class Main < public static void main(String[] args) < // same time in millis Instant now = Instant.ofEpochMilli(1262347200000l); // native plusSeconds() method to add 10 seconds Instant nowPlusTenSeconds = now.plusSeconds(10); // no native support for units like days. Instant nowPlusTwoDays = now.plus(2, ChronoUnit.DAYS); Instant nowMinusTwoDays = now.minus(Duration.ofDays(2)); System.out.println(nowPlusTenSeconds); System.out.println(nowPlusTwoDays); System.out.println(nowMinusTwoDays); > >

The code above generates the following result.

java2s.com | © Demo Source and Support. All rights reserved.

Источник

Оцените статью