KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > drftpd > util > CalendarUtils


1 /*
2  * This file is part of DrFTPD, Distributed FTP Daemon.
3  *
4  * DrFTPD is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * DrFTPD is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with DrFTPD; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  */

18 package net.sf.drftpd.util;
19
20 import java.util.Calendar JavaDoc;
21
22 import org.apache.log4j.Logger;
23
24 /**
25  * @author mog
26  * @version $Id: CalendarUtils.java,v 1.6 2004/06/01 15:40:33 mog Exp $
27  */

28 public class CalendarUtils {
29     private static final Logger logger = Logger.getLogger(CalendarUtils.class);
30
31     private CalendarUtils() {
32     }
33
34     public static void floorAllLessThanDay(Calendar JavaDoc cal) {
35         cal.set(Calendar.MILLISECOND, 0);
36         cal.set(Calendar.SECOND, 0);
37         cal.set(Calendar.MINUTE, 0);
38         cal.set(Calendar.HOUR_OF_DAY, 0);
39     }
40
41     public static void ceilAllLessThanDay(Calendar JavaDoc cal) {
42         cal.set(
43             Calendar.MILLISECOND,
44             cal.getActualMaximum(Calendar.MILLISECOND));
45         cal.set(Calendar.SECOND, cal.getActualMaximum(Calendar.SECOND));
46         cal.set(Calendar.MINUTE, cal.getActualMaximum(Calendar.MINUTE));
47         cal.set(
48             Calendar.HOUR_OF_DAY,
49             cal.getActualMaximum(Calendar.HOUR_OF_DAY));
50     }
51
52     public static void floorDayOfWeek(Calendar JavaDoc cal) {
53         // workaround for bug in Calendar
54
// see http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4967383
55
cal.get(Calendar.WEEK_OF_MONTH);
56
57         cal.set(Calendar.DAY_OF_WEEK, cal.getFirstDayOfWeek());
58
59         if(cal.get(Calendar.DAY_OF_WEEK) != cal.getFirstDayOfWeek())
60             logger.error("cal.set(DAY_OF_WEEK) didn't work! " + cal.getTime());
61     }
62
63     public static void floorDayOfMonth(Calendar JavaDoc cal) {
64         cal.set(
65             Calendar.DAY_OF_MONTH,
66             cal.getActualMinimum(Calendar.DAY_OF_MONTH));
67     }
68
69     /**
70      * Increments month of Calendar by one month.
71      *
72      * If the following month does not have enough days,
73      * the first day in the month after the following month is used.
74      */

75     public static void incrementMonth(Calendar JavaDoc cal) {
76         Calendar JavaDoc cal2 = (Calendar JavaDoc) cal.clone();
77         floorDayOfMonth(cal2);
78         cal2.add(Calendar.MONTH, 1);
79         if (cal.get(Calendar.DAY_OF_MONTH)
80             > cal2.getActualMaximum(Calendar.DAY_OF_MONTH)) {
81             floorDayOfMonth(cal);
82             cal.add(Calendar.MONTH, 2);
83         }
84         cal.add(Calendar.MONTH, 1);
85     }
86
87     public static void incrementWeek(Calendar JavaDoc cal) {
88         cal.add(Calendar.WEEK_OF_YEAR, 1);
89     }
90     public static void incrementDay(Calendar JavaDoc cal) {
91         cal.add(Calendar.DAY_OF_MONTH, 1);
92     }
93     public static int getLastDayOfWeek(Calendar JavaDoc cal) {
94         int dow = cal.getFirstDayOfWeek() - 1;
95         if (dow == 0)
96             dow = Calendar.SATURDAY;
97         return dow;
98     }
99 }
100
Popular Tags