01: /*
02: * @(#)Calendar.java 1.6 06/10/10
03: *
04: * Copyright 1990-2006 Sun Microsystems, Inc. All Rights Reserved.
05: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
06: *
07: * This program is free software; you can redistribute it and/or
08: * modify it under the terms of the GNU General Public License version
09: * 2 only, as published by the Free Software Foundation.
10: *
11: * This program is distributed in the hope that it will be useful, but
12: * WITHOUT ANY WARRANTY; without even the implied warranty of
13: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14: * General Public License version 2 for more details (a copy is
15: * included at /legal/license.txt).
16: *
17: * You should have received a copy of the GNU General Public License
18: * version 2 along with this work; if not, write to the Free Software
19: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20: * 02110-1301 USA
21: *
22: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
23: * Clara, CA 95054 or visit www.sun.com if you need additional
24: * information or have any questions.
25: */
26:
27: package sun.tools.javazic;
28:
29: import sun.util.calendar.Gregorian;
30:
31: /**
32: * Gregorian calendar utility methods. The algorithms are from
33: * <i>"Calendrical Calculation"</i> by Nachum Dershowitz and Edward
34: * M. Reingold (ISBN: 0-521-56474-3).
35: *
36: * @since 1.4
37: */
38:
39: class Calendar extends Gregorian {
40: /**
41: * Returns a fixed date of the n-th day-of-week which is after or
42: * before the given fixed date.
43: * @param nth specifies the n-th one. A positive number specifies
44: * <em>after</em> the <code>fixedDate</code>. A negative number
45: * or 0 specifies <em>before</em> the <code>fixedDate</code>.
46: * @param dayOfWeek the day of week
47: * @param fixedDate the fixed date
48: * @return the fixed date of the <code>nth dayOfWeek</code> after
49: * or before <code>fixedDate</code>
50: */
51: static int getNthDayOfWeek(int nth, int dayOfWeek, int fixedDate) {
52: if (nth > 0) {
53: return (7 * nth + getDayOfWeekDateBefore(fixedDate,
54: dayOfWeek));
55: }
56: return (7 * nth + getDayOfWeekDateAfter(fixedDate, dayOfWeek));
57: }
58:
59: /**
60: * Returns a date of the given day of week before the given fixed
61: * date.
62: * @param fixedDate the fixed date
63: * @param dayOfWeek the day of week
64: * @return the calculated date
65: */
66: private static int getDayOfWeekDateBefore(int fixedDate,
67: int dayOfWeek) {
68: return (getDayOfWeekDateOnOrBefore(fixedDate - 1, dayOfWeek));
69: }
70:
71: /*
72: * Returns a date of the given day of week that is closest to and
73: * after the given fixed date.
74: * @param fixedDate the fixed date
75: * @param dayOfWeek the day of week
76: * @return the calculated date
77: */
78: private static int getDayOfWeekDateAfter(int fixedDate,
79: int dayOfWeek) {
80: return (getDayOfWeekDateOnOrBefore(fixedDate + 7, dayOfWeek));
81: }
82:
83: /*
84: * Returns a date of the given day of week on or before the given fixed
85: * date.
86: * @param fixedDate the fixed date
87: * @param dayOfWeek the day of week
88: * @return the calculated date
89: */
90: private static int getDayOfWeekDateOnOrBefore(int fixedDate,
91: int dayOfWeek) {
92: --dayOfWeek;
93: return (fixedDate - ((fixedDate - dayOfWeek) % 7));
94: }
95: }
|