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


 401     /**
 402      * Obtains an instance of {@code LocalDateTime} using seconds from the
 403      * epoch of 1970-01-01T00:00:00Z.
 404      * <p>
 405      * This allows the {@link ChronoField#INSTANT_SECONDS epoch-second} field
 406      * to be converted to a local date-time. This is primarily intended for
 407      * low-level conversions rather than general application usage.
 408      *
 409      * @param epochSecond  the number of seconds from the epoch of 1970-01-01T00:00:00Z
 410      * @param nanoOfSecond  the nanosecond within the second, from 0 to 999,999,999
 411      * @param offset  the zone offset, not null
 412      * @return the local date-time, not null
 413      * @throws DateTimeException if the result exceeds the supported range,
 414      *  or if the nano-of-second is invalid
 415      */
 416     public static LocalDateTime ofEpochSecond(long epochSecond, int nanoOfSecond, ZoneOffset offset) {
 417         Objects.requireNonNull(offset, "offset");
 418         NANO_OF_SECOND.checkValidValue(nanoOfSecond);
 419         long localSecond = epochSecond + offset.getTotalSeconds();  // overflow caught later
 420         long localEpochDay = Math.floorDiv(localSecond, SECONDS_PER_DAY);
 421         int secsOfDay = (int)Math.floorMod(localSecond, SECONDS_PER_DAY);
 422         LocalDate date = LocalDate.ofEpochDay(localEpochDay);
 423         LocalTime time = LocalTime.ofNanoOfDay(secsOfDay * NANOS_PER_SECOND + nanoOfSecond);
 424         return new LocalDateTime(date, time);
 425     }
 426 
 427     //-----------------------------------------------------------------------
 428     /**
 429      * Obtains an instance of {@code LocalDateTime} from a temporal object.
 430      * <p>
 431      * This obtains a local date-time based on the specified temporal.
 432      * A {@code TemporalAccessor} represents an arbitrary set of date and time information,
 433      * which this factory converts to an instance of {@code LocalDateTime}.
 434      * <p>
 435      * The conversion extracts and combines the {@code LocalDate} and the
 436      * {@code LocalTime} from the temporal object.
 437      * Implementations are permitted to perform optimizations such as accessing
 438      * those fields that are equivalent to the relevant objects.
 439      * <p>
 440      * This method matches the signature of the functional interface {@link TemporalQuery}
 441      * allowing it to be used as a query via method reference, {@code LocalDateTime::from}.


   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


 401     /**
 402      * Obtains an instance of {@code LocalDateTime} using seconds from the
 403      * epoch of 1970-01-01T00:00:00Z.
 404      * <p>
 405      * This allows the {@link ChronoField#INSTANT_SECONDS epoch-second} field
 406      * to be converted to a local date-time. This is primarily intended for
 407      * low-level conversions rather than general application usage.
 408      *
 409      * @param epochSecond  the number of seconds from the epoch of 1970-01-01T00:00:00Z
 410      * @param nanoOfSecond  the nanosecond within the second, from 0 to 999,999,999
 411      * @param offset  the zone offset, not null
 412      * @return the local date-time, not null
 413      * @throws DateTimeException if the result exceeds the supported range,
 414      *  or if the nano-of-second is invalid
 415      */
 416     public static LocalDateTime ofEpochSecond(long epochSecond, int nanoOfSecond, ZoneOffset offset) {
 417         Objects.requireNonNull(offset, "offset");
 418         NANO_OF_SECOND.checkValidValue(nanoOfSecond);
 419         long localSecond = epochSecond + offset.getTotalSeconds();  // overflow caught later
 420         long localEpochDay = Math.floorDiv(localSecond, SECONDS_PER_DAY);
 421         int secsOfDay = Math.floorMod(localSecond, SECONDS_PER_DAY);
 422         LocalDate date = LocalDate.ofEpochDay(localEpochDay);
 423         LocalTime time = LocalTime.ofNanoOfDay(secsOfDay * NANOS_PER_SECOND + nanoOfSecond);
 424         return new LocalDateTime(date, time);
 425     }
 426 
 427     //-----------------------------------------------------------------------
 428     /**
 429      * Obtains an instance of {@code LocalDateTime} from a temporal object.
 430      * <p>
 431      * This obtains a local date-time based on the specified temporal.
 432      * A {@code TemporalAccessor} represents an arbitrary set of date and time information,
 433      * which this factory converts to an instance of {@code LocalDateTime}.
 434      * <p>
 435      * The conversion extracts and combines the {@code LocalDate} and the
 436      * {@code LocalTime} from the temporal object.
 437      * Implementations are permitted to perform optimizations such as accessing
 438      * those fields that are equivalent to the relevant objects.
 439      * <p>
 440      * This method matches the signature of the functional interface {@link TemporalQuery}
 441      * allowing it to be used as a query via method reference, {@code LocalDateTime::from}.