src/java.base/share/classes/java/time/LocalTime.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


 259     public static LocalTime now(ZoneId zone) {
 260         return now(Clock.system(zone));
 261     }
 262 
 263     /**
 264      * Obtains the current time from the specified clock.
 265      * <p>
 266      * This will query the specified clock to obtain the current time.
 267      * Using this method allows the use of an alternate clock for testing.
 268      * The alternate clock may be introduced using {@link Clock dependency injection}.
 269      *
 270      * @param clock  the clock to use, not null
 271      * @return the current time, not null
 272      */
 273     public static LocalTime now(Clock clock) {
 274         Objects.requireNonNull(clock, "clock");
 275         // inline OffsetTime factory to avoid creating object and InstantProvider checks
 276         final Instant now = clock.instant();  // called once
 277         ZoneOffset offset = clock.getZone().getRules().getOffset(now);
 278         long localSecond = now.getEpochSecond() + offset.getTotalSeconds();  // overflow caught later
 279         int secsOfDay = (int) Math.floorMod(localSecond, SECONDS_PER_DAY);
 280         return ofNanoOfDay(secsOfDay * NANOS_PER_SECOND + now.getNano());
 281     }
 282 
 283     //-----------------------------------------------------------------------
 284     /**
 285      * Obtains an instance of {@code LocalTime} from an hour and minute.
 286      * <p>
 287      * This returns a {@code LocalTime} with the specified hour and minute.
 288      * The second and nanosecond fields will be set to zero.
 289      *
 290      * @param hour  the hour-of-day to represent, from 0 to 23
 291      * @param minute  the minute-of-hour to represent, from 0 to 59
 292      * @return the local time, not null
 293      * @throws DateTimeException if the value of any field is out of range
 294      */
 295     public static LocalTime of(int hour, int minute) {
 296         HOUR_OF_DAY.checkValidValue(hour);
 297         if (minute == 0) {
 298             return HOURS[hour];  // for performance
 299         }


   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


 259     public static LocalTime now(ZoneId zone) {
 260         return now(Clock.system(zone));
 261     }
 262 
 263     /**
 264      * Obtains the current time from the specified clock.
 265      * <p>
 266      * This will query the specified clock to obtain the current time.
 267      * Using this method allows the use of an alternate clock for testing.
 268      * The alternate clock may be introduced using {@link Clock dependency injection}.
 269      *
 270      * @param clock  the clock to use, not null
 271      * @return the current time, not null
 272      */
 273     public static LocalTime now(Clock clock) {
 274         Objects.requireNonNull(clock, "clock");
 275         // inline OffsetTime factory to avoid creating object and InstantProvider checks
 276         final Instant now = clock.instant();  // called once
 277         ZoneOffset offset = clock.getZone().getRules().getOffset(now);
 278         long localSecond = now.getEpochSecond() + offset.getTotalSeconds();  // overflow caught later
 279         int secsOfDay = Math.floorMod(localSecond, SECONDS_PER_DAY);
 280         return ofNanoOfDay(secsOfDay * NANOS_PER_SECOND + now.getNano());
 281     }
 282 
 283     //-----------------------------------------------------------------------
 284     /**
 285      * Obtains an instance of {@code LocalTime} from an hour and minute.
 286      * <p>
 287      * This returns a {@code LocalTime} with the specified hour and minute.
 288      * The second and nanosecond fields will be set to zero.
 289      *
 290      * @param hour  the hour-of-day to represent, from 0 to 23
 291      * @param minute  the minute-of-hour to represent, from 0 to 59
 292      * @return the local time, not null
 293      * @throws DateTimeException if the value of any field is out of range
 294      */
 295     public static LocalTime of(int hour, int minute) {
 296         HOUR_OF_DAY.checkValidValue(hour);
 297         if (minute == 0) {
 298             return HOURS[hour];  // for performance
 299         }