Path: blob/master/src/java.base/share/classes/sun/util/calendar/CalendarUtils.java
41159 views
/*1* Copyright (c) 2003, 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*/2425package sun.util.calendar;2627import java.util.HashMap;28import java.util.Map;2930public class CalendarUtils {3132/**33* Returns whether the specified year is a leap year in the Gregorian34* calendar system.35*36* @param gregorianYear a Gregorian calendar year37* @return true if the given year is a leap year in the Gregorian38* calendar system.39* @see CalendarDate#isLeapYear40*/41public static final boolean isGregorianLeapYear(int gregorianYear) {42return (((gregorianYear % 4) == 0)43&& (((gregorianYear % 100) != 0) || ((gregorianYear % 400) == 0)));44}4546/**47* Returns whether the specified year is a leap year in the Julian48* calendar system. The year number must be a normalized one49* (e.g., 45 B.C.E. is 1-45).50*51* @param normalizedJulianYear a normalized Julian calendar year52* @return true if the given year is a leap year in the Julian53* calendar system.54* @see CalendarDate#isLeapYear55*/56public static final boolean isJulianLeapYear(int normalizedJulianYear) {57return (normalizedJulianYear % 4) == 0;58}5960/**61* Divides two integers and returns the floor of the quotient.62* For example, <code>floorDivide(-1, 4)</code> returns -1 while63* -1/4 is 0.64*65* @param n the numerator66* @param d a divisor that must be greater than 067* @return the floor of the quotient68*/69public static final long floorDivide(long n, long d) {70return ((n >= 0) ?71(n / d) : (((n + 1L) / d) - 1L));72}7374/**75* Divides two integers and returns the floor of the quotient.76* For example, <code>floorDivide(-1, 4)</code> returns -1 while77* -1/4 is 0.78*79* @param n the numerator80* @param d a divisor that must be greater than 081* @return the floor of the quotient82*/83public static final int floorDivide(int n, int d) {84return ((n >= 0) ?85(n / d) : (((n + 1) / d) - 1));86}8788/**89* Divides two integers and returns the floor of the quotient and90* the modulus remainder. For example,91* <code>floorDivide(-1,4)</code> returns <code>-1</code> with92* <code>3</code> as its remainder, while <code>-1/4</code> is93* <code>0</code> and <code>-1%4</code> is <code>-1</code>.94*95* @param n the numerator96* @param d a divisor which must be {@literal > 0}97* @param r an array of at least one element in which the value98* <code>mod(n, d)</code> is returned.99* @return the floor of the quotient.100*/101public static final int floorDivide(int n, int d, int[] r) {102if (n >= 0) {103r[0] = n % d;104return n / d;105}106int q = ((n + 1) / d) - 1;107r[0] = n - (q * d);108return q;109}110111/**112* Divides two integers and returns the floor of the quotient and113* the modulus remainder. For example,114* <code>floorDivide(-1,4)</code> returns <code>-1</code> with115* <code>3</code> as its remainder, while <code>-1/4</code> is116* <code>0</code> and <code>-1%4</code> is <code>-1</code>.117*118* @param n the numerator119* @param d a divisor which must be {@literal > 0}120* @param r an array of at least one element in which the value121* <code>mod(n, d)</code> is returned.122* @return the floor of the quotient.123*/124public static final int floorDivide(long n, int d, int[] r) {125if (n >= 0) {126r[0] = (int)(n % d);127return (int)(n / d);128}129int q = (int)(((n + 1) / d) - 1);130r[0] = (int)(n - (q * d));131return q;132}133134public static final long mod(long x, long y) {135return (x - y * floorDivide(x, y));136}137138public static final int mod(int x, int y) {139return (x - y * floorDivide(x, y));140}141142public static final int amod(int x, int y) {143int z = mod(x, y);144return (z == 0) ? y : z;145}146147public static final long amod(long x, long y) {148long z = mod(x, y);149return (z == 0) ? y : z;150}151152/**153* Mimics sprintf(buf, "%0*d", decaimal, width).154*/155public static final StringBuilder sprintf0d(StringBuilder sb, int value, int width) {156long d = value;157if (d < 0) {158sb.append('-');159d = -d;160--width;161}162int n = 10;163for (int i = 2; i < width; i++) {164n *= 10;165}166for (int i = 1; i < width && d < n; i++) {167sb.append('0');168n /= 10;169}170sb.append(d);171return sb;172}173174public static final StringBuffer sprintf0d(StringBuffer sb, int value, int width) {175long d = value;176if (d < 0) {177sb.append('-');178d = -d;179--width;180}181int n = 10;182for (int i = 2; i < width; i++) {183n *= 10;184}185for (int i = 1; i < width && d < n; i++) {186sb.append('0');187n /= 10;188}189sb.append(d);190return sb;191}192}193194195