jdk/src/share/classes/java/time/Instant.java
changeset 17474 8c100beabcc0
parent 16852 60207b2b4b42
child 19030 32f129cb6351
equal deleted inserted replaced
17473:35cd9b3a98ff 17474:8c100beabcc0
   140  * alterations to the length of the notional second. As of 2012, discussions are underway
   140  * alterations to the length of the notional second. As of 2012, discussions are underway
   141  * to change the definition of UTC again, with the potential to remove leap seconds or
   141  * to change the definition of UTC again, with the potential to remove leap seconds or
   142  * introduce other changes.
   142  * introduce other changes.
   143  * <p>
   143  * <p>
   144  * Given the complexity of accurate timekeeping described above, this Java API defines
   144  * Given the complexity of accurate timekeeping described above, this Java API defines
   145  * its own time-scale with a simplification. The Java time-scale is defined as follows:
   145  * its own time-scale, the <i>Java Time-Scale</i>.
       
   146  * <p>
       
   147  * The Java Time-Scale divides each calendar day into exactly 86400
       
   148  * subdivisions, known as seconds.  These seconds may differ from the
       
   149  * SI second.  It closely matches the de facto international civil time
       
   150  * scale, the definition of which changes from time to time.
       
   151  * <p>
       
   152  * The Java Time-Scale has slightly different definitions for different
       
   153  * segments of the time-line, each based on the consensus international
       
   154  * time scale that is used as the basis for civil time. Whenever the
       
   155  * internationally-agreed time scale is modified or replaced, a new
       
   156  * segment of the Java Time-Scale must be defined for it.  Each segment
       
   157  * must meet these requirements:
   146  * <p><ul>
   158  * <p><ul>
   147  * <li>midday will always be exactly as defined by the agreed international civil time</li>
   159  * <li>the Java Time-Scale shall closely match the underlying international
   148  * <li>other times during the day will be broadly in line with the agreed international civil time</li>
   160  *  civil time scale;</li>
   149  * <li>the day will be divided into exactly 86400 subdivisions, referred to as "seconds"</li>
   161  * <li>the Java Time-Scale shall exactly match the international civil
   150  * <li>the Java "second" may differ from an SI second</li>
   162  *  time scale at noon each day;</li>
   151  * <li>a well-defined algorithm must be specified to map each second in the accurate agreed
   163  * <li>the Java Time-Scale shall have a precisely-defined relationship to
   152  *  international civil time to each "second" in this time-scale</li>
   164  *  the international civil time scale.</li>
   153  * </ul><p>
   165  * </ul><p>
   154  * Agreed international civil time is the base time-scale agreed by international convention,
   166  * There are currently, as of 2013, two segments in the Java time-scale.
   155  * which in 2012 is UTC (with leap-seconds).
   167  * <p>
   156  * <p>
   168  * For the segment from 1972-11-03 (exact boundary discussed below) until
   157  * In 2012, the definition of the Java time-scale is the same as UTC for all days except
   169  * further notice, the consensus international time scale is UTC (with
   158  * those where a leap-second occurs. On days where a leap-second does occur, the time-scale
   170  * leap seconds).  In this segment, the Java Time-Scale is identical to
   159  * effectively eliminates the leap-second, maintaining the fiction of 86400 seconds in the day.
       
   160  * The approved well-defined algorithm to eliminate leap-seconds is specified as
       
   161  * <a href="http://www.cl.cam.ac.uk/~mgk25/time/utc-sls/">UTC-SLS</a>.
   171  * <a href="http://www.cl.cam.ac.uk/~mgk25/time/utc-sls/">UTC-SLS</a>.
   162  * <p>
   172  * This is identical to UTC on days that do not have a leap second.
   163  * UTC-SLS is a simple algorithm that smoothes the leap-second over the last 1000 seconds of
   173  * On days that do have a leap second, the leap second is spread equally
   164  * the day, making each of the last 1000 seconds 1/1000th longer or shorter than an SI second.
   174  * over the last 1000 seconds of the day, maintaining the appearance of
   165  * Implementations built on an accurate leap-second aware time source should use UTC-SLS.
   175  * exactly 86400 seconds per day.
   166  * Use of a different algorithm risks confusion and misinterpretation of instants around a
   176  * <p>
   167  * leap-second and is discouraged.
   177  * For the segment prior to 1972-11-03, extending back arbitrarily far,
   168  * <p>
   178  * the consensus international time scale is defined to be UT1, applied
   169  * The main benefit of always dividing the day into 86400 subdivisions is that it matches the
   179  * proleptically, which is equivalent to the (mean) solar time on the
   170  * expectations of most users of the API. The alternative is to force every user to understand
   180  * prime meridian (Greenwich). In this segment, the Java Time-Scale is
   171  * what a leap second is and to force them to have special logic to handle them.
   181  * identical to the consensus international time scale. The exact
   172  * Most applications do not have access to a clock that is accurate enough to record leap-seconds.
   182  * boundary between the two segments is the instant where UT1 = UTC
   173  * Most applications also do not have a problem with a second being a very small amount longer or
   183  * between 1972-11-03T00:00 and 1972-11-04T12:00.
   174  * shorter than a real SI second during a leap-second.
   184  * <p>
   175  * <p>
   185  * Implementations of the Java time-scale using the JSR-310 API are not
   176  * One final problem is the definition of the agreed international civil time before the
   186  * required to provide any clock that is sub-second accurate, or that
   177  * introduction of modern UTC in 1972. This includes the Java epoch of {@code 1970-01-01}.
   187  * progresses monotonically or smoothly. Implementations are therefore
   178  * It is intended that instants before 1972 be interpreted based on the solar day divided
   188  * not required to actually perform the UTC-SLS slew or to otherwise be
   179  * into 86400 subdivisions, as per the principles of UT1.
   189  * aware of leap seconds. JSR-310 does, however, require that
       
   190  * implementations must document the approach they use when defining a
       
   191  * clock representing the current instant.
       
   192  * See {@link Clock} for details on the available clocks.
   180  * <p>
   193  * <p>
   181  * The Java time-scale is used for all date-time classes.
   194  * The Java time-scale is used for all date-time classes.
   182  * This includes {@code Instant}, {@code LocalDate}, {@code LocalTime}, {@code OffsetDateTime},
   195  * This includes {@code Instant}, {@code LocalDate}, {@code LocalTime}, {@code OffsetDateTime},
   183  * {@code ZonedDateTime} and {@code Duration}.
   196  * {@code ZonedDateTime} and {@code Duration}.
   184  *
   197  *
   185  * <h3>Specification for implementors</h3>
   198  * @implSpec
   186  * This class is immutable and thread-safe.
   199  * This class is immutable and thread-safe.
   187  *
   200  *
   188  * @since 1.8
   201  * @since 1.8
   189  */
   202  */
   190 public final class Instant
   203 public final class Instant
  1028     public Temporal adjustInto(Temporal temporal) {
  1041     public Temporal adjustInto(Temporal temporal) {
  1029         return temporal.with(INSTANT_SECONDS, seconds).with(NANO_OF_SECOND, nanos);
  1042         return temporal.with(INSTANT_SECONDS, seconds).with(NANO_OF_SECOND, nanos);
  1030     }
  1043     }
  1031 
  1044 
  1032     /**
  1045     /**
  1033      * Calculates the period between this instant and another instant in
  1046      * Calculates the amount of time until another instant in terms of the specified unit.
  1034      * terms of the specified unit.
  1047      * <p>
  1035      * <p>
  1048      * This calculates the amount of time between two {@code Instant}
  1036      * This calculates the period between two instants in terms of a single unit.
  1049      * objects in terms of a single {@code TemporalUnit}.
  1037      * The start and end points are {@code this} and the specified instant.
  1050      * The start and end points are {@code this} and the specified instant.
  1038      * The result will be negative if the end is before the start.
  1051      * The result will be negative if the end is before the start.
  1039      * The calculation returns a whole number, representing the number of
  1052      * The calculation returns a whole number, representing the number of
  1040      * complete units between the two instants.
  1053      * complete units between the two instants.
  1041      * The {@code Temporal} passed to this method must be an {@code Instant}.
  1054      * The {@code Temporal} passed to this method must be an {@code Instant}.
  1042      * For example, the period in days between two dates can be calculated
  1055      * For example, the amount in days between two dates can be calculated
  1043      * using {@code startInstant.periodUntil(endInstant, SECONDS)}.
  1056      * using {@code startInstant.periodUntil(endInstant, SECONDS)}.
  1044      * <p>
  1057      * <p>
  1045      * There are two equivalent ways of using this method.
  1058      * There are two equivalent ways of using this method.
  1046      * The first is to invoke this method.
  1059      * The first is to invoke this method.
  1047      * The second is to use {@link TemporalUnit#between(Temporal, Temporal)}:
  1060      * The second is to use {@link TemporalUnit#between(Temporal, Temporal)}:
  1062      * passing {@code this} as the first argument and the input temporal as
  1075      * passing {@code this} as the first argument and the input temporal as
  1063      * the second argument.
  1076      * the second argument.
  1064      * <p>
  1077      * <p>
  1065      * This instance is immutable and unaffected by this method call.
  1078      * This instance is immutable and unaffected by this method call.
  1066      *
  1079      *
  1067      * @param endInstant  the end date, which must be a {@code LocalDate}, not null
  1080      * @param endInstant  the end date, which must be an {@code Instant}, not null
  1068      * @param unit  the unit to measure the period in, not null
  1081      * @param unit  the unit to measure the amount in, not null
  1069      * @return the amount of the period between this date and the end date
  1082      * @return the amount of time between this instant and the end instant
  1070      * @throws DateTimeException if the period cannot be calculated
  1083      * @throws DateTimeException if the amount cannot be calculated
  1071      * @throws UnsupportedTemporalTypeException if the unit is not supported
  1084      * @throws UnsupportedTemporalTypeException if the unit is not supported
  1072      * @throws ArithmeticException if numeric overflow occurs
  1085      * @throws ArithmeticException if numeric overflow occurs
  1073      */
  1086      */
  1074     @Override
  1087     @Override
  1075     public long periodUntil(Temporal endInstant, TemporalUnit unit) {
  1088     public long periodUntil(Temporal endInstant, TemporalUnit unit) {
  1076         if (endInstant instanceof Instant == false) {
  1089         if (endInstant instanceof Instant == false) {
  1077             Objects.requireNonNull(endInstant, "endInstant");
  1090             Objects.requireNonNull(endInstant, "endInstant");
  1078             throw new DateTimeException("Unable to calculate period between objects of two different types");
  1091             throw new DateTimeException("Unable to calculate amount as objects are of two different types");
  1079         }
  1092         }
  1080         Instant end = (Instant) endInstant;
  1093         Instant end = (Instant) endInstant;
  1081         if (unit instanceof ChronoUnit) {
  1094         if (unit instanceof ChronoUnit) {
  1082             ChronoUnit f = (ChronoUnit) unit;
  1095             ChronoUnit f = (ChronoUnit) unit;
  1083             switch (f) {
  1096             switch (f) {