0N/A/*
2362N/A * Copyright (c) 2003, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage sun.util.calendar;
0N/A
0N/Aimport java.util.HashMap;
0N/Aimport java.util.Map;
0N/A
0N/Apublic class CalendarUtils {
0N/A
0N/A /**
0N/A * Returns whether the specified year is a leap year in the Gregorian
0N/A * calendar system.
0N/A *
0N/A * @param gregorianYear a Gregorian calendar year
0N/A * @return true if the given year is a leap year in the Gregorian
0N/A * calendar system.
0N/A * @see CalendarDate#isLeapYear
0N/A */
0N/A public static final boolean isGregorianLeapYear(int gregorianYear) {
0N/A return (((gregorianYear % 4) == 0)
0N/A && (((gregorianYear % 100) != 0) || ((gregorianYear % 400) == 0)));
0N/A }
0N/A
0N/A /**
0N/A * Returns whether the specified year is a leap year in the Julian
0N/A * calendar system. The year number must be a normalized one
0N/A * (e.g., 45 B.C.E. is 1-45).
0N/A *
0N/A * @param normalizedJulianYear a normalized Julian calendar year
0N/A * @return true if the given year is a leap year in the Julian
0N/A * calendar system.
0N/A * @see CalendarDate#isLeapYear
0N/A */
0N/A public static final boolean isJulianLeapYear(int normalizedJulianYear) {
0N/A return (normalizedJulianYear % 4) == 0;
0N/A }
0N/A
0N/A /**
0N/A * Divides two integers and returns the floor of the quotient.
0N/A * For example, <code>floorDivide(-1, 4)</code> returns -1 while
0N/A * -1/4 is 0.
0N/A *
0N/A * @param n the numerator
0N/A * @param d a divisor that must be greater than 0
0N/A * @return the floor of the quotient
0N/A */
0N/A public static final long floorDivide(long n, long d) {
0N/A return ((n >= 0) ?
0N/A (n / d) : (((n + 1L) / d) - 1L));
0N/A }
0N/A
0N/A /**
0N/A * Divides two integers and returns the floor of the quotient.
0N/A * For example, <code>floorDivide(-1, 4)</code> returns -1 while
0N/A * -1/4 is 0.
0N/A *
0N/A * @param n the numerator
0N/A * @param d a divisor that must be greater than 0
0N/A * @return the floor of the quotient
0N/A */
0N/A public static final int floorDivide(int n, int d) {
0N/A return ((n >= 0) ?
0N/A (n / d) : (((n + 1) / d) - 1));
0N/A }
0N/A
0N/A /**
0N/A * Divides two integers and returns the floor of the quotient and
0N/A * the modulus remainder. For example,
0N/A * <code>floorDivide(-1,4)</code> returns <code>-1</code> with
0N/A * <code>3</code> as its remainder, while <code>-1/4</code> is
0N/A * <code>0</code> and <code>-1%4</code> is <code>-1</code>.
0N/A *
0N/A * @param n the numerator
0N/A * @param d a divisor which must be > 0
0N/A * @param r an array of at least one element in which the value
0N/A * <code>mod(n, d)</code> is returned.
0N/A * @return the floor of the quotient.
0N/A */
0N/A public static final int floorDivide(int n, int d, int[] r) {
0N/A if (n >= 0) {
0N/A r[0] = n % d;
0N/A return n / d;
0N/A }
0N/A int q = ((n + 1) / d) - 1;
0N/A r[0] = n - (q * d);
0N/A return q;
0N/A }
0N/A
0N/A /**
0N/A * Divides two integers and returns the floor of the quotient and
0N/A * the modulus remainder. For example,
0N/A * <code>floorDivide(-1,4)</code> returns <code>-1</code> with
0N/A * <code>3</code> as its remainder, while <code>-1/4</code> is
0N/A * <code>0</code> and <code>-1%4</code> is <code>-1</code>.
0N/A *
0N/A * @param n the numerator
0N/A * @param d a divisor which must be > 0
0N/A * @param r an array of at least one element in which the value
0N/A * <code>mod(n, d)</code> is returned.
0N/A * @return the floor of the quotient.
0N/A */
0N/A public static final int floorDivide(long n, int d, int[] r) {
0N/A if (n >= 0) {
0N/A r[0] = (int)(n % d);
0N/A return (int)(n / d);
0N/A }
0N/A int q = (int)(((n + 1) / d) - 1);
0N/A r[0] = (int)(n - (q * d));
0N/A return q;
0N/A }
0N/A
0N/A public static final long mod(long x, long y) {
0N/A return (x - y * floorDivide(x, y));
0N/A }
0N/A
0N/A public static final int mod(int x, int y) {
0N/A return (x - y * floorDivide(x, y));
0N/A }
0N/A
0N/A public static final int amod(int x, int y) {
0N/A int z = mod(x, y);
0N/A return (z == 0) ? y : z;
0N/A }
0N/A
0N/A public static final long amod(long x, long y) {
0N/A long z = mod(x, y);
0N/A return (z == 0) ? y : z;
0N/A }
0N/A
0N/A /**
0N/A * Mimics sprintf(buf, "%0*d", decaimal, width).
0N/A */
0N/A public static final StringBuilder sprintf0d(StringBuilder sb, int value, int width) {
0N/A long d = value;
0N/A if (d < 0) {
0N/A sb.append('-');
0N/A d = -d;
0N/A --width;
0N/A }
0N/A int n = 10;
0N/A for (int i = 2; i < width; i++) {
0N/A n *= 10;
0N/A }
0N/A for (int i = 1; i < width && d < n; i++) {
0N/A sb.append('0');
0N/A n /= 10;
0N/A }
0N/A sb.append(d);
0N/A return sb;
0N/A }
0N/A
0N/A public static final StringBuffer sprintf0d(StringBuffer sb, int value, int width) {
0N/A long d = value;
0N/A if (d < 0) {
0N/A sb.append('-');
0N/A d = -d;
0N/A --width;
0N/A }
0N/A int n = 10;
0N/A for (int i = 2; i < width; i++) {
0N/A n *= 10;
0N/A }
0N/A for (int i = 1; i < width && d < n; i++) {
0N/A sb.append('0');
0N/A n /= 10;
0N/A }
0N/A sb.append(d);
0N/A return sb;
0N/A }
0N/A}