1 /*
2  * Copyright (c) 2003, 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
23  * questions.
24  */
25 
26 package sun.util.calendar;
27 
28 import java.util.HashMap;
29 import java.util.Map;
30 
31 public class CalendarUtils {
32 
33     /**
34      * Returns whether the specified year is a leap year in the Gregorian
35      * calendar system.
36      *
37      * @param gregorianYear a Gregorian calendar year
38      * @return true if the given year is a leap year in the Gregorian
39      * calendar system.
40      * @see CalendarDate#isLeapYear
41      */
isGregorianLeapYear(int gregorianYear)42     public static final boolean isGregorianLeapYear(int gregorianYear) {
43         return (((gregorianYear % 4) == 0)
44                 && (((gregorianYear % 100) != 0) || ((gregorianYear % 400) == 0)));
45     }
46 
47     /**
48      * Returns whether the specified year is a leap year in the Julian
49      * calendar system. The year number must be a normalized one
50      * (e.g., 45 B.C.E. is 1-45).
51      *
52      * @param normalizedJulianYear a normalized Julian calendar year
53      * @return true if the given year is a leap year in the Julian
54      * calendar system.
55      * @see CalendarDate#isLeapYear
56      */
isJulianLeapYear(int normalizedJulianYear)57     public static final boolean isJulianLeapYear(int normalizedJulianYear) {
58         return (normalizedJulianYear % 4) == 0;
59     }
60 
61     /**
62      * Divides two integers and returns the floor of the quotient.
63      * For example, <code>floorDivide(-1, 4)</code> returns -1 while
64      * -1/4 is 0.
65      *
66      * @param n the numerator
67      * @param d a divisor that must be greater than 0
68      * @return the floor of the quotient
69      */
floorDivide(long n, long d)70     public static final long floorDivide(long n, long d) {
71         return ((n >= 0) ?
72                 (n / d) : (((n + 1L) / d) - 1L));
73     }
74 
75     /**
76      * Divides two integers and returns the floor of the quotient.
77      * For example, <code>floorDivide(-1, 4)</code> returns -1 while
78      * -1/4 is 0.
79      *
80      * @param n the numerator
81      * @param d a divisor that must be greater than 0
82      * @return the floor of the quotient
83      */
floorDivide(int n, int d)84     public static final int floorDivide(int n, int d) {
85         return ((n >= 0) ?
86                 (n / d) : (((n + 1) / d) - 1));
87     }
88 
89     /**
90      * Divides two integers and returns the floor of the quotient and
91      * the modulus remainder.  For example,
92      * <code>floorDivide(-1,4)</code> returns <code>-1</code> with
93      * <code>3</code> as its remainder, while <code>-1/4</code> is
94      * <code>0</code> and <code>-1%4</code> is <code>-1</code>.
95      *
96      * @param n the numerator
97      * @param d a divisor which must be {@literal > 0}
98      * @param r an array of at least one element in which the value
99      * <code>mod(n, d)</code> is returned.
100      * @return the floor of the quotient.
101      */
floorDivide(int n, int d, int[] r)102     public static final int floorDivide(int n, int d, int[] r) {
103         if (n >= 0) {
104             r[0] = n % d;
105             return n / d;
106         }
107         int q = ((n + 1) / d) - 1;
108         r[0] = n - (q * d);
109         return q;
110     }
111 
112     /**
113      * Divides two integers and returns the floor of the quotient and
114      * the modulus remainder.  For example,
115      * <code>floorDivide(-1,4)</code> returns <code>-1</code> with
116      * <code>3</code> as its remainder, while <code>-1/4</code> is
117      * <code>0</code> and <code>-1%4</code> is <code>-1</code>.
118      *
119      * @param n the numerator
120      * @param d a divisor which must be {@literal > 0}
121      * @param r an array of at least one element in which the value
122      * <code>mod(n, d)</code> is returned.
123      * @return the floor of the quotient.
124      */
floorDivide(long n, int d, int[] r)125     public static final int floorDivide(long n, int d, int[] r) {
126         if (n >= 0) {
127             r[0] = (int)(n % d);
128             return (int)(n / d);
129         }
130         int q = (int)(((n + 1) / d) - 1);
131         r[0] = (int)(n - (q * d));
132         return q;
133     }
134 
mod(long x, long y)135     public static final long mod(long x, long y) {
136         return (x - y * floorDivide(x, y));
137     }
138 
mod(int x, int y)139     public static final int mod(int x, int y) {
140         return (x - y * floorDivide(x, y));
141     }
142 
amod(int x, int y)143     public static final int amod(int x, int y) {
144         int z = mod(x, y);
145         return (z == 0) ? y : z;
146     }
147 
amod(long x, long y)148     public static final long amod(long x, long y) {
149         long z = mod(x, y);
150         return (z == 0) ? y : z;
151     }
152 
153     /**
154      * Mimics sprintf(buf, "%0*d", decaimal, width).
155      */
sprintf0d(StringBuilder sb, int value, int width)156     public static final StringBuilder sprintf0d(StringBuilder sb, int value, int width) {
157         long d = value;
158         if (d < 0) {
159             sb.append('-');
160             d = -d;
161             --width;
162         }
163         int n = 10;
164         for (int i = 2; i < width; i++) {
165             n *= 10;
166         }
167         for (int i = 1; i < width && d < n; i++) {
168             sb.append('0');
169             n /= 10;
170         }
171         sb.append(d);
172         return sb;
173     }
174 
sprintf0d(StringBuffer sb, int value, int width)175     public static final StringBuffer sprintf0d(StringBuffer sb, int value, int width) {
176         long d = value;
177         if (d < 0) {
178             sb.append('-');
179             d = -d;
180             --width;
181         }
182         int n = 10;
183         for (int i = 2; i < width; i++) {
184             n *= 10;
185         }
186         for (int i = 1; i < width && d < n; i++) {
187             sb.append('0');
188             n /= 10;
189         }
190         sb.append(d);
191         return sb;
192     }
193 }
194