jdk/src/share/classes/java/time/OffsetTime.java
changeset 24256 da9a41004459
parent 22566 4ebe53dd7814
equal deleted inserted replaced
24255:91f5e4399160 24256:da9a41004459
   151 
   151 
   152     //-----------------------------------------------------------------------
   152     //-----------------------------------------------------------------------
   153     /**
   153     /**
   154      * Obtains the current time from the system clock in the default time-zone.
   154      * Obtains the current time from the system clock in the default time-zone.
   155      * <p>
   155      * <p>
   156      * This will query the {@link java.time.Clock#systemDefaultZone() system clock} in the default
   156      * This will query the {@link Clock#systemDefaultZone() system clock} in the default
   157      * time-zone to obtain the current time.
   157      * time-zone to obtain the current time.
   158      * The offset will be calculated from the time-zone in the clock.
   158      * The offset will be calculated from the time-zone in the clock.
   159      * <p>
   159      * <p>
   160      * Using this method will prevent the ability to use an alternate clock for testing
   160      * Using this method will prevent the ability to use an alternate clock for testing
   161      * because the clock is hard-coded.
   161      * because the clock is hard-coded.
   162      *
   162      *
   163      * @return the current time using the system clock, not null
   163      * @return the current time using the system clock and default time-zone, not null
   164      */
   164      */
   165     public static OffsetTime now() {
   165     public static OffsetTime now() {
   166         return now(Clock.systemDefaultZone());
   166         return now(Clock.systemDefaultZone());
   167     }
   167     }
   168 
   168 
   169     /**
   169     /**
   170      * Obtains the current time from the system clock in the specified time-zone.
   170      * Obtains the current time from the system clock in the specified time-zone.
   171      * <p>
   171      * <p>
   172      * This will query the {@link Clock#system(java.time.ZoneId) system clock} to obtain the current time.
   172      * This will query the {@link Clock#system(ZoneId) system clock} to obtain the current time.
   173      * Specifying the time-zone avoids dependence on the default time-zone.
   173      * Specifying the time-zone avoids dependence on the default time-zone.
   174      * The offset will be calculated from the specified time-zone.
   174      * The offset will be calculated from the specified time-zone.
   175      * <p>
   175      * <p>
   176      * Using this method will prevent the ability to use an alternate clock for testing
   176      * Using this method will prevent the ability to use an alternate clock for testing
   177      * because the clock is hard-coded.
   177      * because the clock is hard-coded.
   275      * {@code LocalTime} from the temporal object.
   275      * {@code LocalTime} from the temporal object.
   276      * Implementations are permitted to perform optimizations such as accessing
   276      * Implementations are permitted to perform optimizations such as accessing
   277      * those fields that are equivalent to the relevant objects.
   277      * those fields that are equivalent to the relevant objects.
   278      * <p>
   278      * <p>
   279      * This method matches the signature of the functional interface {@link TemporalQuery}
   279      * This method matches the signature of the functional interface {@link TemporalQuery}
   280      * allowing it to be used in queries via method reference, {@code OffsetTime::from}.
   280      * allowing it to be used as a query via method reference, {@code OffsetTime::from}.
   281      *
   281      *
   282      * @param temporal  the temporal object to convert, not null
   282      * @param temporal  the temporal object to convert, not null
   283      * @return the offset time, not null
   283      * @return the offset time, not null
   284      * @throws DateTimeException if unable to convert to an {@code OffsetTime}
   284      * @throws DateTimeException if unable to convert to an {@code OffsetTime}
   285      */
   285      */
   400     }
   400     }
   401 
   401 
   402     /**
   402     /**
   403      * Checks if the specified unit is supported.
   403      * Checks if the specified unit is supported.
   404      * <p>
   404      * <p>
   405      * This checks if the specified unit can be added to, or subtracted from, this date-time.
   405      * This checks if the specified unit can be added to, or subtracted from, this offset-time.
   406      * If false, then calling the {@link #plus(long, TemporalUnit)} and
   406      * If false, then calling the {@link #plus(long, TemporalUnit)} and
   407      * {@link #minus(long, TemporalUnit) minus} methods will throw an exception.
   407      * {@link #minus(long, TemporalUnit) minus} methods will throw an exception.
   408      * <p>
   408      * <p>
   409      * If the unit is a {@link ChronoUnit} then the query is implemented here.
   409      * If the unit is a {@link ChronoUnit} then the query is implemented here.
   410      * The supported units are:
   410      * The supported units are:
   471     }
   471     }
   472 
   472 
   473     /**
   473     /**
   474      * Gets the value of the specified field from this time as an {@code int}.
   474      * Gets the value of the specified field from this time as an {@code int}.
   475      * <p>
   475      * <p>
   476      * This queries this time for the value for the specified field.
   476      * This queries this time for the value of the specified field.
   477      * The returned value will always be within the valid range of values for the field.
   477      * The returned value will always be within the valid range of values for the field.
   478      * If it is not possible to return the value, because the field is not supported
   478      * If it is not possible to return the value, because the field is not supported
   479      * or for some other reason, an exception is thrown.
   479      * or for some other reason, an exception is thrown.
   480      * <p>
   480      * <p>
   481      * If the field is a {@link ChronoField} then the query is implemented here.
   481      * If the field is a {@link ChronoField} then the query is implemented here.
   482      * The {@link #isSupported(TemporalField) supported fields} will return valid
   482      * The {@link #isSupported(TemporalField) supported fields} will return valid
   483      * values based on this time, except {@code NANO_OF_DAY} and {@code MICRO_OF_DAY}
   483      * values based on this time, except {@code NANO_OF_DAY} and {@code MICRO_OF_DAY}
   484      * which are too large to fit in an {@code int} and throw a {@code DateTimeException}.
   484      * which are too large to fit in an {@code int} and throw a {@code UnsupportedTemporalTypeException}.
   485      * All other {@code ChronoField} instances will throw an {@code UnsupportedTemporalTypeException}.
   485      * All other {@code ChronoField} instances will throw an {@code UnsupportedTemporalTypeException}.
   486      * <p>
   486      * <p>
   487      * If the field is not a {@code ChronoField}, then the result of this method
   487      * If the field is not a {@code ChronoField}, then the result of this method
   488      * is obtained by invoking {@code TemporalField.getFrom(TemporalAccessor)}
   488      * is obtained by invoking {@code TemporalField.getFrom(TemporalAccessor)}
   489      * passing {@code this} as the argument. Whether the value can be obtained,
   489      * passing {@code this} as the argument. Whether the value can be obtained,
   503     }
   503     }
   504 
   504 
   505     /**
   505     /**
   506      * Gets the value of the specified field from this time as a {@code long}.
   506      * Gets the value of the specified field from this time as a {@code long}.
   507      * <p>
   507      * <p>
   508      * This queries this time for the value for the specified field.
   508      * This queries this time for the value of the specified field.
   509      * If it is not possible to return the value, because the field is not supported
   509      * If it is not possible to return the value, because the field is not supported
   510      * or for some other reason, an exception is thrown.
   510      * or for some other reason, an exception is thrown.
   511      * <p>
   511      * <p>
   512      * If the field is a {@link ChronoField} then the query is implemented here.
   512      * If the field is a {@link ChronoField} then the query is implemented here.
   513      * The {@link #isSupported(TemporalField) supported fields} will return valid
   513      * The {@link #isSupported(TemporalField) supported fields} will return valid
   573      * Returns a copy of this {@code OffsetTime} with the specified offset ensuring
   573      * Returns a copy of this {@code OffsetTime} with the specified offset ensuring
   574      * that the result is at the same instant on an implied day.
   574      * that the result is at the same instant on an implied day.
   575      * <p>
   575      * <p>
   576      * This method returns an object with the specified {@code ZoneOffset} and a {@code LocalTime}
   576      * This method returns an object with the specified {@code ZoneOffset} and a {@code LocalTime}
   577      * adjusted by the difference between the two offsets.
   577      * adjusted by the difference between the two offsets.
   578      * This will result in the old and new objects representing the same instant an an implied day.
   578      * This will result in the old and new objects representing the same instant on an implied day.
   579      * This is useful for finding the local time in a different offset.
   579      * This is useful for finding the local time in a different offset.
   580      * For example, if this time represents {@code 10:30+02:00} and the offset specified is
   580      * For example, if this time represents {@code 10:30+02:00} and the offset specified is
   581      * {@code +03:00}, then this method will return {@code 11:30+03:00}.
   581      * {@code +03:00}, then this method will return {@code 11:30+03:00}.
   582      * <p>
   582      * <p>
   583      * To change the offset without adjusting the local time use {@link #withOffsetSameLocal}.
   583      * To change the offset without adjusting the local time use {@link #withOffsetSameLocal}.
   735         return field.adjustInto(this, newValue);
   735         return field.adjustInto(this, newValue);
   736     }
   736     }
   737 
   737 
   738     //-----------------------------------------------------------------------
   738     //-----------------------------------------------------------------------
   739     /**
   739     /**
   740      * Returns a copy of this {@code OffsetTime} with the hour-of-day value altered.
   740      * Returns a copy of this {@code OffsetTime} with the hour-of-day altered.
   741      * <p>
   741      * <p>
   742      * The offset does not affect the calculation and will be the same in the result.
   742      * The offset does not affect the calculation and will be the same in the result.
   743      * <p>
   743      * <p>
   744      * This instance is immutable and unaffected by this method call.
   744      * This instance is immutable and unaffected by this method call.
   745      *
   745      *
   750     public OffsetTime withHour(int hour) {
   750     public OffsetTime withHour(int hour) {
   751         return with(time.withHour(hour), offset);
   751         return with(time.withHour(hour), offset);
   752     }
   752     }
   753 
   753 
   754     /**
   754     /**
   755      * Returns a copy of this {@code OffsetTime} with the minute-of-hour value altered.
   755      * Returns a copy of this {@code OffsetTime} with the minute-of-hour altered.
   756      * <p>
   756      * <p>
   757      * The offset does not affect the calculation and will be the same in the result.
   757      * The offset does not affect the calculation and will be the same in the result.
   758      * <p>
   758      * <p>
   759      * This instance is immutable and unaffected by this method call.
   759      * This instance is immutable and unaffected by this method call.
   760      *
   760      *
   765     public OffsetTime withMinute(int minute) {
   765     public OffsetTime withMinute(int minute) {
   766         return with(time.withMinute(minute), offset);
   766         return with(time.withMinute(minute), offset);
   767     }
   767     }
   768 
   768 
   769     /**
   769     /**
   770      * Returns a copy of this {@code OffsetTime} with the second-of-minute value altered.
   770      * Returns a copy of this {@code OffsetTime} with the second-of-minute altered.
   771      * <p>
   771      * <p>
   772      * The offset does not affect the calculation and will be the same in the result.
   772      * The offset does not affect the calculation and will be the same in the result.
   773      * <p>
   773      * <p>
   774      * This instance is immutable and unaffected by this method call.
   774      * This instance is immutable and unaffected by this method call.
   775      *
   775      *
   780     public OffsetTime withSecond(int second) {
   780     public OffsetTime withSecond(int second) {
   781         return with(time.withSecond(second), offset);
   781         return with(time.withSecond(second), offset);
   782     }
   782     }
   783 
   783 
   784     /**
   784     /**
   785      * Returns a copy of this {@code OffsetTime} with the nano-of-second value altered.
   785      * Returns a copy of this {@code OffsetTime} with the nano-of-second altered.
   786      * <p>
   786      * <p>
   787      * The offset does not affect the calculation and will be the same in the result.
   787      * The offset does not affect the calculation and will be the same in the result.
   788      * <p>
   788      * <p>
   789      * This instance is immutable and unaffected by this method call.
   789      * This instance is immutable and unaffected by this method call.
   790      *
   790      *
   882         return unit.addTo(this, amountToAdd);
   882         return unit.addTo(this, amountToAdd);
   883     }
   883     }
   884 
   884 
   885     //-----------------------------------------------------------------------
   885     //-----------------------------------------------------------------------
   886     /**
   886     /**
   887      * Returns a copy of this {@code OffsetTime} with the specified period in hours added.
   887      * Returns a copy of this {@code OffsetTime} with the specified number of hours added.
   888      * <p>
   888      * <p>
   889      * This adds the specified number of hours to this time, returning a new time.
   889      * This adds the specified number of hours to this time, returning a new time.
   890      * The calculation wraps around midnight.
   890      * The calculation wraps around midnight.
   891      * <p>
   891      * <p>
   892      * This instance is immutable and unaffected by this method call.
   892      * This instance is immutable and unaffected by this method call.
   897     public OffsetTime plusHours(long hours) {
   897     public OffsetTime plusHours(long hours) {
   898         return with(time.plusHours(hours), offset);
   898         return with(time.plusHours(hours), offset);
   899     }
   899     }
   900 
   900 
   901     /**
   901     /**
   902      * Returns a copy of this {@code OffsetTime} with the specified period in minutes added.
   902      * Returns a copy of this {@code OffsetTime} with the specified number of minutes added.
   903      * <p>
   903      * <p>
   904      * This adds the specified number of minutes to this time, returning a new time.
   904      * This adds the specified number of minutes to this time, returning a new time.
   905      * The calculation wraps around midnight.
   905      * The calculation wraps around midnight.
   906      * <p>
   906      * <p>
   907      * This instance is immutable and unaffected by this method call.
   907      * This instance is immutable and unaffected by this method call.
   912     public OffsetTime plusMinutes(long minutes) {
   912     public OffsetTime plusMinutes(long minutes) {
   913         return with(time.plusMinutes(minutes), offset);
   913         return with(time.plusMinutes(minutes), offset);
   914     }
   914     }
   915 
   915 
   916     /**
   916     /**
   917      * Returns a copy of this {@code OffsetTime} with the specified period in seconds added.
   917      * Returns a copy of this {@code OffsetTime} with the specified number of seconds added.
   918      * <p>
   918      * <p>
   919      * This adds the specified number of seconds to this time, returning a new time.
   919      * This adds the specified number of seconds to this time, returning a new time.
   920      * The calculation wraps around midnight.
   920      * The calculation wraps around midnight.
   921      * <p>
   921      * <p>
   922      * This instance is immutable and unaffected by this method call.
   922      * This instance is immutable and unaffected by this method call.
   927     public OffsetTime plusSeconds(long seconds) {
   927     public OffsetTime plusSeconds(long seconds) {
   928         return with(time.plusSeconds(seconds), offset);
   928         return with(time.plusSeconds(seconds), offset);
   929     }
   929     }
   930 
   930 
   931     /**
   931     /**
   932      * Returns a copy of this {@code OffsetTime} with the specified period in nanoseconds added.
   932      * Returns a copy of this {@code OffsetTime} with the specified number of nanoseconds added.
   933      * <p>
   933      * <p>
   934      * This adds the specified number of nanoseconds to this time, returning a new time.
   934      * This adds the specified number of nanoseconds to this time, returning a new time.
   935      * The calculation wraps around midnight.
   935      * The calculation wraps around midnight.
   936      * <p>
   936      * <p>
   937      * This instance is immutable and unaffected by this method call.
   937      * This instance is immutable and unaffected by this method call.
   993         return (amountToSubtract == Long.MIN_VALUE ? plus(Long.MAX_VALUE, unit).plus(1, unit) : plus(-amountToSubtract, unit));
   993         return (amountToSubtract == Long.MIN_VALUE ? plus(Long.MAX_VALUE, unit).plus(1, unit) : plus(-amountToSubtract, unit));
   994     }
   994     }
   995 
   995 
   996     //-----------------------------------------------------------------------
   996     //-----------------------------------------------------------------------
   997     /**
   997     /**
   998      * Returns a copy of this {@code OffsetTime} with the specified period in hours subtracted.
   998      * Returns a copy of this {@code OffsetTime} with the specified number of hours subtracted.
   999      * <p>
   999      * <p>
  1000      * This subtracts the specified number of hours from this time, returning a new time.
  1000      * This subtracts the specified number of hours from this time, returning a new time.
  1001      * The calculation wraps around midnight.
  1001      * The calculation wraps around midnight.
  1002      * <p>
  1002      * <p>
  1003      * This instance is immutable and unaffected by this method call.
  1003      * This instance is immutable and unaffected by this method call.
  1008     public OffsetTime minusHours(long hours) {
  1008     public OffsetTime minusHours(long hours) {
  1009         return with(time.minusHours(hours), offset);
  1009         return with(time.minusHours(hours), offset);
  1010     }
  1010     }
  1011 
  1011 
  1012     /**
  1012     /**
  1013      * Returns a copy of this {@code OffsetTime} with the specified period in minutes subtracted.
  1013      * Returns a copy of this {@code OffsetTime} with the specified number of minutes subtracted.
  1014      * <p>
  1014      * <p>
  1015      * This subtracts the specified number of minutes from this time, returning a new time.
  1015      * This subtracts the specified number of minutes from this time, returning a new time.
  1016      * The calculation wraps around midnight.
  1016      * The calculation wraps around midnight.
  1017      * <p>
  1017      * <p>
  1018      * This instance is immutable and unaffected by this method call.
  1018      * This instance is immutable and unaffected by this method call.
  1023     public OffsetTime minusMinutes(long minutes) {
  1023     public OffsetTime minusMinutes(long minutes) {
  1024         return with(time.minusMinutes(minutes), offset);
  1024         return with(time.minusMinutes(minutes), offset);
  1025     }
  1025     }
  1026 
  1026 
  1027     /**
  1027     /**
  1028      * Returns a copy of this {@code OffsetTime} with the specified period in seconds subtracted.
  1028      * Returns a copy of this {@code OffsetTime} with the specified number of seconds subtracted.
  1029      * <p>
  1029      * <p>
  1030      * This subtracts the specified number of seconds from this time, returning a new time.
  1030      * This subtracts the specified number of seconds from this time, returning a new time.
  1031      * The calculation wraps around midnight.
  1031      * The calculation wraps around midnight.
  1032      * <p>
  1032      * <p>
  1033      * This instance is immutable and unaffected by this method call.
  1033      * This instance is immutable and unaffected by this method call.
  1038     public OffsetTime minusSeconds(long seconds) {
  1038     public OffsetTime minusSeconds(long seconds) {
  1039         return with(time.minusSeconds(seconds), offset);
  1039         return with(time.minusSeconds(seconds), offset);
  1040     }
  1040     }
  1041 
  1041 
  1042     /**
  1042     /**
  1043      * Returns a copy of this {@code OffsetTime} with the specified period in nanoseconds subtracted.
  1043      * Returns a copy of this {@code OffsetTime} with the specified number of nanoseconds subtracted.
  1044      * <p>
  1044      * <p>
  1045      * This subtracts the specified number of nanoseconds from this time, returning a new time.
  1045      * This subtracts the specified number of nanoseconds from this time, returning a new time.
  1046      * The calculation wraps around midnight.
  1046      * The calculation wraps around midnight.
  1047      * <p>
  1047      * <p>
  1048      * This instance is immutable and unaffected by this method call.
  1048      * This instance is immutable and unaffected by this method call.
  1128      * <p>
  1128      * <p>
  1129      * This calculates the amount of time between two {@code OffsetTime}
  1129      * This calculates the amount of time between two {@code OffsetTime}
  1130      * objects in terms of a single {@code TemporalUnit}.
  1130      * objects in terms of a single {@code TemporalUnit}.
  1131      * The start and end points are {@code this} and the specified time.
  1131      * The start and end points are {@code this} and the specified time.
  1132      * The result will be negative if the end is before the start.
  1132      * The result will be negative if the end is before the start.
  1133      * For example, the period in hours between two times can be calculated
  1133      * For example, the amount in hours between two times can be calculated
  1134      * using {@code startTime.until(endTime, HOURS)}.
  1134      * using {@code startTime.until(endTime, HOURS)}.
  1135      * <p>
  1135      * <p>
  1136      * The {@code Temporal} passed to this method is converted to a
  1136      * The {@code Temporal} passed to this method is converted to a
  1137      * {@code OffsetTime} using {@link #from(TemporalAccessor)}.
  1137      * {@code OffsetTime} using {@link #from(TemporalAccessor)}.
  1138      * If the offset differs between the two times, then the specified
  1138      * If the offset differs between the two times, then the specified
  1139      * end time is normalized to have the same offset as this time.
  1139      * end time is normalized to have the same offset as this time.
  1140      * <p>
  1140      * <p>
  1141      * The calculation returns a whole number, representing the number of
  1141      * The calculation returns a whole number, representing the number of
  1142      * complete units between the two times.
  1142      * complete units between the two times.
  1143      * For example, the period in hours between 11:30Z and 13:29Z will only
  1143      * For example, the amount in hours between 11:30Z and 13:29Z will only
  1144      * be one hour as it is one minute short of two hours.
  1144      * be one hour as it is one minute short of two hours.
  1145      * <p>
  1145      * <p>
  1146      * There are two equivalent ways of using this method.
  1146      * There are two equivalent ways of using this method.
  1147      * The first is to invoke this method.
  1147      * The first is to invoke this method.
  1148      * The second is to use {@link TemporalUnit#between(Temporal, Temporal)}:
  1148      * The second is to use {@link TemporalUnit#between(Temporal, Temporal)}:
  1163      * passing {@code this} as the first argument and the converted input temporal
  1163      * passing {@code this} as the first argument and the converted input temporal
  1164      * as the second argument.
  1164      * as the second argument.
  1165      * <p>
  1165      * <p>
  1166      * This instance is immutable and unaffected by this method call.
  1166      * This instance is immutable and unaffected by this method call.
  1167      *
  1167      *
  1168      * @param endExclusive  the end date, exclusive, which is converted to an {@code OffsetTime}, not null
  1168      * @param endExclusive  the end time, exclusive, which is converted to an {@code OffsetTime}, not null
  1169      * @param unit  the unit to measure the amount in, not null
  1169      * @param unit  the unit to measure the amount in, not null
  1170      * @return the amount of time between this time and the end time
  1170      * @return the amount of time between this time and the end time
  1171      * @throws DateTimeException if the amount cannot be calculated, or the end
  1171      * @throws DateTimeException if the amount cannot be calculated, or the end
  1172      *  temporal cannot be converted to an {@code OffsetTime}
  1172      *  temporal cannot be converted to an {@code OffsetTime}
  1173      * @throws UnsupportedTemporalTypeException if the unit is not supported
  1173      * @throws UnsupportedTemporalTypeException if the unit is not supported
  1256      * To compare the underlying local time of two {@code TemporalAccessor} instances,
  1256      * To compare the underlying local time of two {@code TemporalAccessor} instances,
  1257      * use {@link ChronoField#NANO_OF_DAY} as a comparator.
  1257      * use {@link ChronoField#NANO_OF_DAY} as a comparator.
  1258      *
  1258      *
  1259      * @param other  the other time to compare to, not null
  1259      * @param other  the other time to compare to, not null
  1260      * @return the comparator value, negative if less, positive if greater
  1260      * @return the comparator value, negative if less, positive if greater
  1261      * @throws NullPointerException if {@code other} is null
       
  1262      */
  1261      */
  1263     @Override
  1262     @Override
  1264     public int compareTo(OffsetTime other) {
  1263     public int compareTo(OffsetTime other) {
  1265         if (offset.equals(other.offset)) {
  1264         if (offset.equals(other.offset)) {
  1266             return time.compareTo(other.time);
  1265             return time.compareTo(other.time);