001: /**********************************************************************************
002: * $URL: https://source.sakaiproject.org/svn/calendar/tags/sakai_2-4-1/calendar-util/util/src/java/org/sakaiproject/util/CalendarUtil.java $
003: * $Id: CalendarUtil.java 8050 2006-04-20 17:39:55Z ggolden@umich.edu $
004: ***********************************************************************************
005: *
006: * Copyright (c) 2003, 2004, 2005, 2006 The Sakai Foundation.
007: *
008: * Licensed under the Educational Community License, Version 1.0 (the "License");
009: * you may not use this file except in compliance with the License.
010: * You may obtain a copy of the License at
011: *
012: * http://www.opensource.org/licenses/ecl1.php
013: *
014: * Unless required by applicable law or agreed to in writing, software
015: * distributed under the License is distributed on an "AS IS" BASIS,
016: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017: * See the License for the specific language governing permissions and
018: * limitations under the License.
019: *
020: **********************************************************************************/package org.sakaiproject.util;
021:
022: import java.util.Calendar;
023:
024: /**
025: * <p>CalendarUtil is a bunch of utility methods added to a java Calendar object.</p>
026: */
027: public class CalendarUtil {
028: /** The calendar object this is based upon. */
029: Calendar m_calendar = null;
030:
031: /**
032: * Construct.
033: */
034: public CalendarUtil() {
035: m_calendar = Calendar.getInstance();
036:
037: } // CalendarUtil
038:
039: /**
040: * Access the current user.
041: * @return the current year.
042: */
043: public int getYear() {
044: return m_calendar.get(Calendar.YEAR);
045:
046: } // getYear
047:
048: /**
049: * Set the calendar to the next day, and return this.
050: * @return the next day.
051: */
052: public String getNextDate() {
053: m_calendar.set(Calendar.DAY_OF_MONTH, getDayOfMonth() + 1);
054: return getTodayDate();
055:
056: } // getNextDate
057:
058: public void nextDate() {
059: m_calendar.set(Calendar.DAY_OF_MONTH, getDayOfMonth() + 1);
060: }
061:
062: /**
063: * Set the calendar to the prev day, and return this.
064: * @return the prev day.
065: */
066: public String getPrevDate() {
067: m_calendar.set(Calendar.DAY_OF_MONTH, getDayOfMonth() - 1);
068: return getTodayDate();
069: } // getPrevDate
070:
071: public void prevDate() {
072: m_calendar.set(Calendar.DAY_OF_MONTH, getDayOfMonth() - 1);
073: }
074:
075: /**
076: * Set the calendar to the next month, and return this.
077: * @return the next month.
078: */
079: public int getNextMonth() {
080: m_calendar.set(Calendar.MONTH, getMonthInteger());
081:
082: setDay(getYear(), getMonthInteger(), 1);
083:
084: return getMonthInteger();
085:
086: } // getNextMonth
087:
088: /**
089: * Set the calendar to the next year, and return this.
090: * @return the next year.
091: */
092: public void setNextYear() {
093: m_calendar.set(Calendar.YEAR, getYear() + 1);
094: setDay(getYear(), getMonthInteger(), 1);
095:
096: } // setNextYear
097:
098: /**
099: * Set the calendar to the prev month, and return this.
100: * @return the prev month.
101: */
102: public int getPrevMonth() {
103: m_calendar.set(Calendar.MONTH, getMonthInteger() - 2);
104:
105: return (getMonthInteger() - 1);
106:
107: } // getPrevMonth
108:
109: /**
110: * Set the calendar to the prev year, and return this.
111: * @return the prev year.
112: */
113: public void setPrevYear() {
114: m_calendar.set(Calendar.YEAR, getYear() - 1);
115:
116: } // setPrevYear
117:
118: /**
119: * Get the day of the week.
120: * @return the day of the week.
121: */
122: public int getDay_Of_Week() {
123: return m_calendar.get(Calendar.DAY_OF_WEEK);
124:
125: } //. getDay_Of_Week
126:
127: /**
128: * Set the calendar to the next week, and return this.
129: * @return the next week.
130: */
131: public void setNextWeek() {
132: m_calendar.set(Calendar.WEEK_OF_MONTH, getWeekOfMonth() + 1);
133:
134: } // setNextWeek
135:
136: /**
137: * Set the calendar to the prev week, and return this.
138: * @return the prev week.
139: */
140: public void setPrevWeek() {
141: m_calendar.set(Calendar.WEEK_OF_MONTH, getWeekOfMonth() - 1);
142:
143: } // setPrevWeek
144:
145: /**
146: * Get the day of the week in month.
147: * @return the day of week in month.
148: */
149: public int getDayOfWeekInMonth() {
150: return m_calendar.get(Calendar.DAY_OF_WEEK_IN_MONTH);
151:
152: } // getDayOfWeekInMonth
153:
154: /**
155: * Get the week of month.
156: * @return the week of month.
157: */
158: public int getWeekOfMonth() {
159: return m_calendar.get(Calendar.WEEK_OF_MONTH);
160:
161: } // getWeekOfMonth
162:
163: /**
164: * Get the month as an int value.
165: * @return the month as an int value.
166: */
167: public int getMonthInteger() {
168: // 1 will be added here to be able to use this func alone to construct e.g adate
169: // to get the name of the month from getMonth(), 1 must be deducted.
170: return 1 + m_calendar.get(Calendar.MONTH);
171:
172: } // getMonthInteger
173:
174: /**
175: * Get the day of month.
176: * @return the day of month.
177: */
178: public int getDayOfMonth() {
179: int dayofmonth = m_calendar.get(Calendar.DAY_OF_MONTH);
180: return dayofmonth;
181:
182: } // getDayOfMonth
183:
184: /**
185: * Get the current date, formatted.
186: * @return the current date, formatted.
187: */
188: public String getTodayDate() {
189: return getMonthInteger() + "/" + getDayOfMonth() + "/"
190: + getYear();
191:
192: } // getTodayDate
193:
194: /**
195: * Get the number of days.
196: * @return the number of days.
197: */
198: public int getNumberOfDays() {
199: return m_calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
200:
201: } // getNumberOfDays
202:
203: /**
204: * Set the calendar to this day.
205: * @param d the day.
206: */
207: public void setDayOfMonth(int d) {
208: m_calendar.set(Calendar.DAY_OF_MONTH, d);
209:
210: } // setDayOfMonth
211:
212: /**
213: * Set the calendar to this month
214: * @param d the month.
215: */
216: public void setMonth(int d) {
217: m_calendar.set(Calendar.MONTH, d);
218:
219: } // setMonth
220:
221: /**
222: * Set the calendar to the first day of this month, and return this day of week.
223: * @param month The month.
224: * @return The calendar's day of week once set to this month.
225: */
226: public int getFirstDayOfMonth(int month) {
227: m_calendar.set(Calendar.MONTH, month);
228: m_calendar.set(Calendar.DAY_OF_MONTH, 1);
229:
230: return (getDay_Of_Week() - 1);
231:
232: } // getFirstDayOfMonth
233:
234: /**
235: * Set the calendar to this day.
236: * @param year The year.
237: * @param month The month.
238: * @param day The day.
239: */
240: public void setDay(int year, int month, int day) {
241: m_calendar.set(year, month - 1, day);
242:
243: } // setDay
244:
245: } // CalendarUtil
|