< prev index next >

src/java.base/share/classes/java/time/OffsetDateTime.java

Print this page
rev 55940 : [mq]: 8211990
   1 /*
   2  * Copyright (c) 2012, 2018, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any


1637      * <p>
1638      * If the unit is not a {@code ChronoUnit}, then the result of this method
1639      * is obtained by invoking {@code TemporalUnit.between(Temporal, Temporal)}
1640      * passing {@code this} as the first argument and the converted input temporal
1641      * as the second argument.
1642      * <p>
1643      * This instance is immutable and unaffected by this method call.
1644      *
1645      * @param endExclusive  the end date, exclusive, which is converted to an {@code OffsetDateTime}, not null
1646      * @param unit  the unit to measure the amount in, not null
1647      * @return the amount of time between this date-time and the end date-time
1648      * @throws DateTimeException if the amount cannot be calculated, or the end
1649      *  temporal cannot be converted to an {@code OffsetDateTime}
1650      * @throws UnsupportedTemporalTypeException if the unit is not supported
1651      * @throws ArithmeticException if numeric overflow occurs
1652      */
1653     @Override
1654     public long until(Temporal endExclusive, TemporalUnit unit) {
1655         OffsetDateTime end = OffsetDateTime.from(endExclusive);
1656         if (unit instanceof ChronoUnit) {


1657             end = end.withOffsetSameInstant(offset);
1658             return dateTime.until(end.dateTime, unit);




1659         }
1660         return unit.between(this, end);
1661     }
1662 
1663     /**
1664      * Formats this date-time using the specified formatter.
1665      * <p>
1666      * This date-time will be passed to the formatter to produce a string.
1667      *
1668      * @param formatter  the formatter to use, not null
1669      * @return the formatted date-time string, not null
1670      * @throws DateTimeException if an error occurs during printing
1671      */
1672     public String format(DateTimeFormatter formatter) {
1673         Objects.requireNonNull(formatter, "formatter");
1674         return formatter.format(this);
1675     }
1676 
1677     //-----------------------------------------------------------------------
1678     /**


   1 /*
   2  * Copyright (c) 2012, 2019, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any


1637      * <p>
1638      * If the unit is not a {@code ChronoUnit}, then the result of this method
1639      * is obtained by invoking {@code TemporalUnit.between(Temporal, Temporal)}
1640      * passing {@code this} as the first argument and the converted input temporal
1641      * as the second argument.
1642      * <p>
1643      * This instance is immutable and unaffected by this method call.
1644      *
1645      * @param endExclusive  the end date, exclusive, which is converted to an {@code OffsetDateTime}, not null
1646      * @param unit  the unit to measure the amount in, not null
1647      * @return the amount of time between this date-time and the end date-time
1648      * @throws DateTimeException if the amount cannot be calculated, or the end
1649      *  temporal cannot be converted to an {@code OffsetDateTime}
1650      * @throws UnsupportedTemporalTypeException if the unit is not supported
1651      * @throws ArithmeticException if numeric overflow occurs
1652      */
1653     @Override
1654     public long until(Temporal endExclusive, TemporalUnit unit) {
1655         OffsetDateTime end = OffsetDateTime.from(endExclusive);
1656         if (unit instanceof ChronoUnit) {
1657             OffsetDateTime start = this;
1658             try {
1659                 end = end.withOffsetSameInstant(offset);
1660             } catch (DateTimeException ex) {
1661                 // end may be out of valid range. Adjust to end's offset.
1662                 start = withOffsetSameInstant(end.offset);
1663             }
1664             return start.dateTime.until(end.dateTime, unit);
1665         }
1666         return unit.between(this, end);
1667     }
1668 
1669     /**
1670      * Formats this date-time using the specified formatter.
1671      * <p>
1672      * This date-time will be passed to the formatter to produce a string.
1673      *
1674      * @param formatter  the formatter to use, not null
1675      * @return the formatted date-time string, not null
1676      * @throws DateTimeException if an error occurs during printing
1677      */
1678     public String format(DateTimeFormatter formatter) {
1679         Objects.requireNonNull(formatter, "formatter");
1680         return formatter.format(this);
1681     }
1682 
1683     //-----------------------------------------------------------------------
1684     /**


< prev index next >