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

Print this page
rev 12809 : 8023217: Additional floorDiv/floorMod/multiplyExact methods for java.lang.Math
Summary: Add new methods with long, int signatures.
Reviewed-by: XXX
   1 /*
   2  * Copyright (c) 2012, 2013, 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


 241      * Obtains an instance of {@code OffsetTime} from an {@code Instant} and zone ID.
 242      * <p>
 243      * This creates an offset time with the same instant as that specified.
 244      * Finding the offset from UTC/Greenwich is simple as there is only one valid
 245      * offset for each instant.
 246      * <p>
 247      * The date component of the instant is dropped during the conversion.
 248      * This means that the conversion can never fail due to the instant being
 249      * out of the valid range of dates.
 250      *
 251      * @param instant  the instant to create the time from, not null
 252      * @param zone  the time-zone, which may be an offset, not null
 253      * @return the offset time, not null
 254      */
 255     public static OffsetTime ofInstant(Instant instant, ZoneId zone) {
 256         Objects.requireNonNull(instant, "instant");
 257         Objects.requireNonNull(zone, "zone");
 258         ZoneRules rules = zone.getRules();
 259         ZoneOffset offset = rules.getOffset(instant);
 260         long localSecond = instant.getEpochSecond() + offset.getTotalSeconds();  // overflow caught later
 261         int secsOfDay = (int) Math.floorMod(localSecond, SECONDS_PER_DAY);
 262         LocalTime time = LocalTime.ofNanoOfDay(secsOfDay * NANOS_PER_SECOND + instant.getNano());
 263         return new OffsetTime(time, offset);
 264     }
 265 
 266     //-----------------------------------------------------------------------
 267     /**
 268      * Obtains an instance of {@code OffsetTime} from a temporal object.
 269      * <p>
 270      * This obtains an offset time based on the specified temporal.
 271      * A {@code TemporalAccessor} represents an arbitrary set of date and time information,
 272      * which this factory converts to an instance of {@code OffsetTime}.
 273      * <p>
 274      * The conversion extracts and combines the {@code ZoneOffset} and the
 275      * {@code LocalTime} from the temporal object.
 276      * Implementations are permitted to perform optimizations such as accessing
 277      * those fields that are equivalent to the relevant objects.
 278      * <p>
 279      * This method matches the signature of the functional interface {@link TemporalQuery}
 280      * allowing it to be used as a query via method reference, {@code OffsetTime::from}.
 281      *


   1 /*
   2  * Copyright (c) 2012, 2015, 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


 241      * Obtains an instance of {@code OffsetTime} from an {@code Instant} and zone ID.
 242      * <p>
 243      * This creates an offset time with the same instant as that specified.
 244      * Finding the offset from UTC/Greenwich is simple as there is only one valid
 245      * offset for each instant.
 246      * <p>
 247      * The date component of the instant is dropped during the conversion.
 248      * This means that the conversion can never fail due to the instant being
 249      * out of the valid range of dates.
 250      *
 251      * @param instant  the instant to create the time from, not null
 252      * @param zone  the time-zone, which may be an offset, not null
 253      * @return the offset time, not null
 254      */
 255     public static OffsetTime ofInstant(Instant instant, ZoneId zone) {
 256         Objects.requireNonNull(instant, "instant");
 257         Objects.requireNonNull(zone, "zone");
 258         ZoneRules rules = zone.getRules();
 259         ZoneOffset offset = rules.getOffset(instant);
 260         long localSecond = instant.getEpochSecond() + offset.getTotalSeconds();  // overflow caught later
 261         int secsOfDay = Math.floorMod(localSecond, SECONDS_PER_DAY);
 262         LocalTime time = LocalTime.ofNanoOfDay(secsOfDay * NANOS_PER_SECOND + instant.getNano());
 263         return new OffsetTime(time, offset);
 264     }
 265 
 266     //-----------------------------------------------------------------------
 267     /**
 268      * Obtains an instance of {@code OffsetTime} from a temporal object.
 269      * <p>
 270      * This obtains an offset time based on the specified temporal.
 271      * A {@code TemporalAccessor} represents an arbitrary set of date and time information,
 272      * which this factory converts to an instance of {@code OffsetTime}.
 273      * <p>
 274      * The conversion extracts and combines the {@code ZoneOffset} and the
 275      * {@code LocalTime} from the temporal object.
 276      * Implementations are permitted to perform optimizations such as accessing
 277      * those fields that are equivalent to the relevant objects.
 278      * <p>
 279      * This method matches the signature of the functional interface {@link TemporalQuery}
 280      * allowing it to be used as a query via method reference, {@code OffsetTime::from}.
 281      *