src/share/classes/java/time/temporal/ChronoUnit.java

Print this page




  40  *
  41  *  * Neither the name of JSR-310 nor the names of its contributors
  42  *    may be used to endorse or promote products derived from this software
  43  *    without specific prior written permission.
  44  *
  45  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  46  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  47  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  48  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  49  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  50  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  51  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  52  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  53  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  54  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  55  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  56  */
  57 package java.time.temporal;
  58 
  59 import java.time.Duration;



  60 
  61 /**
  62  * A standard set of date periods units.
  63  * <p>
  64  * This set of units provide unit-based access to manipulate a date, time or date-time.
  65  * The standard set of units can be extended by implementing {@link TemporalUnit}.
  66  * <p>
  67  * These units are intended to be applicable in multiple calendar systems.
  68  * For example, most non-ISO calendar systems define units of years, months and days,
  69  * just with slightly different rules.
  70  * The documentation of each unit explains how it operates.
  71  *
  72  * <h3>Specification for implementors</h3>
  73  * This is a final, immutable and thread-safe enum.
  74  *
  75  * @since 1.8
  76  */
  77 public enum ChronoUnit implements TemporalUnit {
  78 
  79     /**


 237     /**
 238      * Checks if this unit is a date unit.
 239      *
 240      * @return true if a date unit, false if a time unit
 241      */
 242     public boolean isDateUnit() {
 243         return this.compareTo(DAYS) >= 0;
 244     }
 245 
 246     /**
 247      * Checks if this unit is a time unit.
 248      *
 249      * @return true if a time unit, false if a date unit
 250      */
 251     public boolean isTimeUnit() {
 252         return this.compareTo(DAYS) < 0;
 253     }
 254 
 255     //-----------------------------------------------------------------------
 256     @Override
 257     public boolean isSupported(Temporal temporal) {
 258         if (this == FOREVER) {
 259             return false;
 260         }
 261         if (temporal instanceof ChronoLocalDate) {
 262             return isDateUnit();
 263         }
 264         if (temporal instanceof ChronoLocalDateTime || temporal instanceof ChronoZonedDateTime) {
 265             return true;
 266         }
 267         return TemporalUnit.super.isSupported(temporal);
 268     }
 269 
 270     @SuppressWarnings("unchecked")
 271     @Override
 272     public <R extends Temporal> R doPlus(R dateTime, long periodToAdd) {
 273         return (R) dateTime.plus(periodToAdd, this);
 274     }
 275 
 276     //-----------------------------------------------------------------------
 277     @Override
 278     public <R extends Temporal> SimplePeriod between(R dateTime1, R dateTime2) {
 279         return new SimplePeriod(dateTime1.periodUntil(dateTime2, this), this);
 280     }
 281 
 282     //-----------------------------------------------------------------------
 283     @Override
 284     public String toString() {
 285         return getName();
 286     }
 287 
 288 }


  40  *
  41  *  * Neither the name of JSR-310 nor the names of its contributors
  42  *    may be used to endorse or promote products derived from this software
  43  *    without specific prior written permission.
  44  *
  45  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  46  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  47  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  48  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  49  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  50  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  51  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  52  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  53  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  54  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  55  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  56  */
  57 package java.time.temporal;
  58 
  59 import java.time.Duration;
  60 import java.time.chrono.ChronoLocalDate;
  61 import java.time.chrono.ChronoLocalDateTime;
  62 import java.time.chrono.ChronoZonedDateTime;
  63 
  64 /**
  65  * A standard set of date periods units.
  66  * <p>
  67  * This set of units provide unit-based access to manipulate a date, time or date-time.
  68  * The standard set of units can be extended by implementing {@link TemporalUnit}.
  69  * <p>
  70  * These units are intended to be applicable in multiple calendar systems.
  71  * For example, most non-ISO calendar systems define units of years, months and days,
  72  * just with slightly different rules.
  73  * The documentation of each unit explains how it operates.
  74  *
  75  * <h3>Specification for implementors</h3>
  76  * This is a final, immutable and thread-safe enum.
  77  *
  78  * @since 1.8
  79  */
  80 public enum ChronoUnit implements TemporalUnit {
  81 
  82     /**


 240     /**
 241      * Checks if this unit is a date unit.
 242      *
 243      * @return true if a date unit, false if a time unit
 244      */
 245     public boolean isDateUnit() {
 246         return this.compareTo(DAYS) >= 0;
 247     }
 248 
 249     /**
 250      * Checks if this unit is a time unit.
 251      *
 252      * @return true if a time unit, false if a date unit
 253      */
 254     public boolean isTimeUnit() {
 255         return this.compareTo(DAYS) < 0;
 256     }
 257 
 258     //-----------------------------------------------------------------------
 259     @Override
 260     public boolean isSupportedBy(Temporal temporal) {
 261         if (this == FOREVER) {
 262             return false;
 263         }
 264         if (temporal instanceof ChronoLocalDate) {
 265             return isDateUnit();
 266         }
 267         if (temporal instanceof ChronoLocalDateTime || temporal instanceof ChronoZonedDateTime) {
 268             return true;
 269         }
 270         return TemporalUnit.super.isSupportedBy(temporal);
 271     }
 272 
 273     @SuppressWarnings("unchecked")
 274     @Override
 275     public <R extends Temporal> R addTo(R temporal, long amount) {
 276         return (R) temporal.plus(amount, this);
 277     }
 278 
 279     //-----------------------------------------------------------------------
 280     @Override
 281     public long between(Temporal temporal1, Temporal temporal2) {
 282         return temporal1.periodUntil(temporal2, this);
 283     }
 284 
 285     //-----------------------------------------------------------------------
 286     @Override
 287     public String toString() {
 288         return getName();
 289     }
 290 
 291 }