Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/make/src/classes/build/tools/tzdb/LocalDate.java
32287 views
/*1* Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425/*26* This file is available under and governed by the GNU General Public27* License version 2 only, as published by the Free Software Foundation.28* However, the following notice accompanied the original version of this29* file:30*31* Copyright (c) 2007-2012, Stephen Colebourne & Michael Nascimento Santos32*33* All rights reserved.34*35* Redistribution and use in source and binary forms, with or without36* modification, are permitted provided that the following conditions are met:37*38* * Redistributions of source code must retain the above copyright notice,39* this list of conditions and the following disclaimer.40*41* * Redistributions in binary form must reproduce the above copyright notice,42* this list of conditions and the following disclaimer in the documentation43* and/or other materials provided with the distribution.44*45* * Neither the name of JSR-310 nor the names of its contributors46* may be used to endorse or promote products derived from this software47* without specific prior written permission.48*49* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS50* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT51* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR52* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR53* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,54* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,55* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR56* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF57* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING58* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS59* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.60*/61package build.tools.tzdb;6263import static build.tools.tzdb.Utils.*;64import static build.tools.tzdb.LocalTime.SECONDS_PER_DAY;65import static build.tools.tzdb.ChronoField.DAY_OF_MONTH;66import static build.tools.tzdb.ChronoField.MONTH_OF_YEAR;67import static build.tools.tzdb.ChronoField.YEAR;6869import java.util.Objects;7071/**72* A date without a time-zone in the ISO-8601 calendar system,73* such as {@code 2007-12-03}.74*75* @since 1.876*/77final class LocalDate {7879/**80* The minimum supported {@code LocalDate}, '-999999999-01-01'.81* This could be used by an application as a "far past" date.82*/83public static final LocalDate MIN = new LocalDate(YEAR_MIN_VALUE, 1, 1);84/**85* The maximum supported {@code LocalDate}, '+999999999-12-31'.86* This could be used by an application as a "far future" date.87*/88public static final LocalDate MAX = new LocalDate(YEAR_MAX_VALUE, 12, 31);8990/**91* The number of days in a 400 year cycle.92*/93private static final int DAYS_PER_CYCLE = 146097;94/**95* The number of days from year zero to year 1970.96* There are five 400 year cycles from year zero to 2000.97* There are 7 leap years from 1970 to 2000.98*/99static final long DAYS_0000_TO_1970 = (DAYS_PER_CYCLE * 5L) - (30L * 365L + 7L);100101/**102* The year.103*/104private final int year;105/**106* The month-of-year.107*/108private final short month;109/**110* The day-of-month.111*/112private final short day;113114/**115* Obtains an instance of {@code LocalDate} from a year, month and day.116* <p>117* The day must be valid for the year and month, otherwise an exception will be thrown.118*119* @param year the year to represent, from MIN_YEAR to MAX_YEAR120* @param month the month-of-year to represent, from 1 (January) to 12 (December)121* @param dayOfMonth the day-of-month to represent, from 1 to 31122* @return the local date, not null123* @throws DateTimeException if the value of any field is out of range124* @throws DateTimeException if the day-of-month is invalid for the month-year125*/126public static LocalDate of(int year, int month, int dayOfMonth) {127YEAR.checkValidValue(year);128MONTH_OF_YEAR.checkValidValue(month);129DAY_OF_MONTH.checkValidValue(dayOfMonth);130if (dayOfMonth > 28 && dayOfMonth > lengthOfMonth(month, isLeapYear(year))) {131if (dayOfMonth == 29) {132throw new DateTimeException("Invalid date 'February 29' as '" + year + "' is not a leap year");133} else {134throw new DateTimeException("Invalid date '" + month + " " + dayOfMonth + "'");135}136}137return new LocalDate(year, month, dayOfMonth);138}139140/**141* Constructor, previously validated.142*143* @param year the year to represent, from MIN_YEAR to MAX_YEAR144* @param month the month-of-year to represent, not null145* @param dayOfMonth the day-of-month to represent, valid for year-month, from 1 to 31146*/147private LocalDate(int year, int month, int dayOfMonth) {148this.year = year;149this.month = (short) month;150this.day = (short) dayOfMonth;151}152153/**154* Gets the year field.155* <p>156* This method returns the primitive {@code int} value for the year.157* <p>158* The year returned by this method is proleptic as per {@code get(YEAR)}.159* To obtain the year-of-era, use {@code get(YEAR_OF_ERA}.160*161* @return the year, from MIN_YEAR to MAX_YEAR162*/163public int getYear() {164return year;165}166167/**168* Gets the month-of-year field as an int from 1 to 12.169*170* @return the month-of-year171*/172public int getMonth() {173return month;174}175176/**177* Gets the day-of-month field.178* <p>179* This method returns the primitive {@code int} value for the day-of-month.180*181* @return the day-of-month, from 1 to 31182*/183public int getDayOfMonth() {184return day;185}186187/**188* Gets the day-of-week field, which is an int from 1 to 7.189*190* @return the day-of-week191*/192public int getDayOfWeek() {193return (int)floorMod(toEpochDay() + 3, 7) + 1;194}195196/**197* Returns a copy of this {@code LocalDate} with the specified number of days added.198* <p>199* This method adds the specified amount to the days field incrementing the200* month and year fields as necessary to ensure the result remains valid.201* The result is only invalid if the maximum/minimum year is exceeded.202* <p>203* For example, 2008-12-31 plus one day would result in 2009-01-01.204* <p>205* This instance is immutable and unaffected by this method call.206*207* @param daysToAdd the days to add, may be negative208* @return a {@code LocalDate} based on this date with the days added, not null209* @throws DateTimeException if the result exceeds the supported date range210*/211public LocalDate plusDays(long daysToAdd) {212if (daysToAdd == 0) {213return this;214}215long mjDay = addExact(toEpochDay(), daysToAdd);216return LocalDate.ofEpochDay(mjDay);217}218219/**220* Returns a copy of this {@code LocalDate} with the specified number of days subtracted.221* <p>222* This method subtracts the specified amount from the days field decrementing the223* month and year fields as necessary to ensure the result remains valid.224* The result is only invalid if the maximum/minimum year is exceeded.225* <p>226* For example, 2009-01-01 minus one day would result in 2008-12-31.227* <p>228* This instance is immutable and unaffected by this method call.229*230* @param daysToSubtract the days to subtract, may be negative231* @return a {@code LocalDate} based on this date with the days subtracted, not null232* @throws DateTimeException if the result exceeds the supported date range233*/234public LocalDate minusDays(long daysToSubtract) {235return (daysToSubtract == Long.MIN_VALUE ? plusDays(Long.MAX_VALUE).plusDays(1) : plusDays(-daysToSubtract));236}237238/**239* Obtains an instance of {@code LocalDate} from the epoch day count.240* <p>241* The Epoch Day count is a simple incrementing count of days242* where day 0 is 1970-01-01. Negative numbers represent earlier days.243*244* @param epochDay the Epoch Day to convert, based on the epoch 1970-01-01245* @return the local date, not null246* @throws DateTimeException if the epoch days exceeds the supported date range247*/248public static LocalDate ofEpochDay(long epochDay) {249long zeroDay = epochDay + DAYS_0000_TO_1970;250// find the march-based year251zeroDay -= 60; // adjust to 0000-03-01 so leap day is at end of four year cycle252long adjust = 0;253if (zeroDay < 0) {254// adjust negative years to positive for calculation255long adjustCycles = (zeroDay + 1) / DAYS_PER_CYCLE - 1;256adjust = adjustCycles * 400;257zeroDay += -adjustCycles * DAYS_PER_CYCLE;258}259long yearEst = (400 * zeroDay + 591) / DAYS_PER_CYCLE;260long doyEst = zeroDay - (365 * yearEst + yearEst / 4 - yearEst / 100 + yearEst / 400);261if (doyEst < 0) {262// fix estimate263yearEst--;264doyEst = zeroDay - (365 * yearEst + yearEst / 4 - yearEst / 100 + yearEst / 400);265}266yearEst += adjust; // reset any negative year267int marchDoy0 = (int) doyEst;268269// convert march-based values back to january-based270int marchMonth0 = (marchDoy0 * 5 + 2) / 153;271int month = (marchMonth0 + 2) % 12 + 1;272int dom = marchDoy0 - (marchMonth0 * 306 + 5) / 10 + 1;273yearEst += marchMonth0 / 10;274275// check year now we are certain it is correct276int year = YEAR.checkValidValue((int)yearEst);277return new LocalDate(year, month, dom);278}279280public long toEpochDay() {281long y = year;282long m = month;283long total = 0;284total += 365 * y;285if (y >= 0) {286total += (y + 3) / 4 - (y + 99) / 100 + (y + 399) / 400;287} else {288total -= y / -4 - y / -100 + y / -400;289}290total += ((367 * m - 362) / 12);291total += day - 1;292if (m > 2) {293total--;294if (isLeapYear(year) == false) {295total--;296}297}298return total - DAYS_0000_TO_1970;299}300301/**302* Compares this date to another date.303* <p>304* The comparison is primarily based on the date, from earliest to latest.305* It is "consistent with equals", as defined by {@link Comparable}.306* <p>307* If all the dates being compared are instances of {@code LocalDate},308* then the comparison will be entirely based on the date.309* If some dates being compared are in different chronologies, then the310* chronology is also considered, see {@link java.time.temporal.ChronoLocalDate#compareTo}.311*312* @param other the other date to compare to, not null313* @return the comparator value, negative if less, positive if greater314*/315public int compareTo(LocalDate otherDate) {316int cmp = (year - otherDate.year);317if (cmp == 0) {318cmp = (month - otherDate.month);319if (cmp == 0) {320cmp = (day - otherDate.day);321}322}323return cmp;324}325326/**327* Checks if this date is equal to another date.328* <p>329* Compares this {@code LocalDate} with another ensuring that the date is the same.330* <p>331* Only objects of type {@code LocalDate} are compared, other types return false.332* To compare the dates of two {@code TemporalAccessor} instances, including dates333* in two different chronologies, use {@link ChronoField#EPOCH_DAY} as a comparator.334*335* @param obj the object to check, null returns false336* @return true if this is equal to the other date337*/338@Override339public boolean equals(Object obj) {340if (this == obj) {341return true;342}343if (obj instanceof LocalDate) {344return compareTo((LocalDate) obj) == 0;345}346return false;347}348349/**350* A hash code for this date.351*352* @return a suitable hash code353*/354@Override355public int hashCode() {356int yearValue = year;357int monthValue = month;358int dayValue = day;359return (yearValue & 0xFFFFF800) ^ ((yearValue << 11) + (monthValue << 6) + (dayValue));360}361362}363364365