001: /*
002: * This file is part of DrFTPD, Distributed FTP Daemon.
003: *
004: * DrFTPD is free software; you can redistribute it and/or modify
005: * it under the terms of the GNU General Public License as published by
006: * the Free Software Foundation; either version 2 of the License, or
007: * (at your option) any later version.
008: *
009: * DrFTPD is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
012: * GNU General Public License for more details.
013: *
014: * You should have received a copy of the GNU General Public License
015: * along with DrFTPD; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: */
018: package net.sf.drftpd.util;
019:
020: import org.apache.log4j.Logger;
021:
022: import java.util.Calendar;
023:
024: /**
025: * @author mog
026: * @version $Id: CalendarUtils.java 690 2004-08-03 20:14:12Z zubov $
027: */
028: public class CalendarUtils {
029: private static final Logger logger = Logger
030: .getLogger(CalendarUtils.class);
031:
032: private CalendarUtils() {
033: }
034:
035: public static void floorAllLessThanDay(Calendar cal) {
036: cal.set(Calendar.MILLISECOND, 0);
037: cal.set(Calendar.SECOND, 0);
038: cal.set(Calendar.MINUTE, 0);
039: cal.set(Calendar.HOUR_OF_DAY, 0);
040: }
041:
042: public static void ceilAllLessThanDay(Calendar cal) {
043: cal.set(Calendar.MILLISECOND, cal
044: .getActualMaximum(Calendar.MILLISECOND));
045: cal.set(Calendar.SECOND, cal.getActualMaximum(Calendar.SECOND));
046: cal.set(Calendar.MINUTE, cal.getActualMaximum(Calendar.MINUTE));
047: cal.set(Calendar.HOUR_OF_DAY, cal
048: .getActualMaximum(Calendar.HOUR_OF_DAY));
049: }
050:
051: public static void floorDayOfWeek(Calendar cal) {
052: // workaround for bug in Calendar
053: // see http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4967383
054: cal.get(Calendar.WEEK_OF_MONTH);
055:
056: cal.set(Calendar.DAY_OF_WEEK, cal.getFirstDayOfWeek());
057:
058: if (cal.get(Calendar.DAY_OF_WEEK) != cal.getFirstDayOfWeek()) {
059: logger.error("cal.set(DAY_OF_WEEK) didn't work! "
060: + cal.getTime());
061: }
062: }
063:
064: public static void floorDayOfMonth(Calendar cal) {
065: cal.set(Calendar.DAY_OF_MONTH, cal
066: .getActualMinimum(Calendar.DAY_OF_MONTH));
067: }
068:
069: /**
070: * Increments month of Calendar by one month.
071: *
072: * If the following month does not have enough days,
073: * the first day in the month after the following month is used.
074: */
075: public static void incrementMonth(Calendar cal) {
076: Calendar cal2 = (Calendar) cal.clone();
077: floorDayOfMonth(cal2);
078: cal2.add(Calendar.MONTH, 1);
079:
080: if (cal.get(Calendar.DAY_OF_MONTH) > cal2
081: .getActualMaximum(Calendar.DAY_OF_MONTH)) {
082: floorDayOfMonth(cal);
083: cal.add(Calendar.MONTH, 2);
084: }
085:
086: cal.add(Calendar.MONTH, 1);
087: }
088:
089: public static void incrementWeek(Calendar cal) {
090: cal.add(Calendar.WEEK_OF_YEAR, 1);
091: }
092:
093: public static void incrementDay(Calendar cal) {
094: cal.add(Calendar.DAY_OF_MONTH, 1);
095: }
096:
097: public static int getLastDayOfWeek(Calendar cal) {
098: int dow = cal.getFirstDayOfWeek() - 1;
099:
100: if (dow == 0) {
101: dow = Calendar.SATURDAY;
102: }
103:
104: return dow;
105: }
106: }
|