jdk/src/java.base/share/classes/java/time/LocalDate.java
changeset 33831 022f746e1d74
parent 25859 3317bb8137f4
child 33847 7857fc64d0c0
equal deleted inserted replaced
33830:9f4be4f1c8b6 33831:022f746e1d74
     1 /*
     1 /*
     2  * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved.
     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.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     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
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.  Oracle designates this
     7  * published by the Free Software Foundation.  Oracle designates this
   218      * @param clock  the clock to use, not null
   218      * @param clock  the clock to use, not null
   219      * @return the current date, not null
   219      * @return the current date, not null
   220      */
   220      */
   221     public static LocalDate now(Clock clock) {
   221     public static LocalDate now(Clock clock) {
   222         Objects.requireNonNull(clock, "clock");
   222         Objects.requireNonNull(clock, "clock");
   223         // inline to avoid creating object and Instant checks
       
   224         final Instant now = clock.instant();  // called once
   223         final Instant now = clock.instant();  // called once
   225         ZoneOffset offset = clock.getZone().getRules().getOffset(now);
   224         return ofInstant(now, clock.getZone());
   226         long epochSec = now.getEpochSecond() + offset.getTotalSeconds();  // overflow caught later
       
   227         long epochDay = Math.floorDiv(epochSec, SECONDS_PER_DAY);
       
   228         return LocalDate.ofEpochDay(epochDay);
       
   229     }
   225     }
   230 
   226 
   231     //-----------------------------------------------------------------------
   227     //-----------------------------------------------------------------------
   232     /**
   228     /**
   233      * Obtains an instance of {@code LocalDate} from a year, month and day.
   229      * Obtains an instance of {@code LocalDate} from a year, month and day.
   294         if (dayOfYear > monthEnd) {
   290         if (dayOfYear > monthEnd) {
   295             moy = moy.plus(1);
   291             moy = moy.plus(1);
   296         }
   292         }
   297         int dom = dayOfYear - moy.firstDayOfYear(leap) + 1;
   293         int dom = dayOfYear - moy.firstDayOfYear(leap) + 1;
   298         return new LocalDate(year, moy.getValue(), dom);
   294         return new LocalDate(year, moy.getValue(), dom);
       
   295     }
       
   296 
       
   297     //-----------------------------------------------------------------------
       
   298     /**
       
   299      * Obtains an instance of {@code LocalDate} from an {@code Instant} and zone ID.
       
   300      * <p>
       
   301      * This creates a local date based on the specified instant.
       
   302      * First, the offset from UTC/Greenwich is obtained using the zone ID and instant,
       
   303      * which is simple as there is only one valid offset for each instant.
       
   304      * Then, the instant and offset are used to calculate the local date.
       
   305      *
       
   306      * @param instant  the instant to create the date from, not null
       
   307      * @param zone  the time-zone, which may be an offset, not null
       
   308      * @return the local date, not null
       
   309      * @throws DateTimeException if the result exceeds the supported range
       
   310      */
       
   311     public static LocalDate ofInstant(Instant instant, ZoneId zone) {
       
   312         Objects.requireNonNull(instant, "instant");
       
   313         Objects.requireNonNull(zone, "zone");
       
   314         ZoneRules rules = zone.getRules();
       
   315         ZoneOffset offset = rules.getOffset(instant);
       
   316         long localSecond = instant.getEpochSecond() + offset.getTotalSeconds();
       
   317         long localEpochDay = Math.floorDiv(localSecond, SECONDS_PER_DAY);
       
   318         return ofEpochDay(localEpochDay);
   299     }
   319     }
   300 
   320 
   301     //-----------------------------------------------------------------------
   321     //-----------------------------------------------------------------------
   302     /**
   322     /**
   303      * Obtains an instance of {@code LocalDate} from the epoch day count.
   323      * Obtains an instance of {@code LocalDate} from the epoch day count.