KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > lucane > applications > calendar > util > CalendarUtils


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

19 package org.lucane.applications.calendar.util;
20
21 import java.util.*;
22 import org.lucane.applications.calendar.*;
23
24 public class CalendarUtils
25 {
26     public static int getFirstDayOfMonth(Calendar c, boolean sundayFirst)
27     {
28         Calendar clone = (Calendar)c.clone();
29         clone.set(Calendar.DAY_OF_MONTH, 1);
30         
31         //as day labels don't respect locales, we have to enforce monday
32
//instead of getFirstDayOfWeek() here to stay consistent
33
int res = clone.get(Calendar.DAY_OF_WEEK) - Calendar.MONDAY;
34         if(sundayFirst)
35             res = clone.get(Calendar.DAY_OF_WEEK) - Calendar.SUNDAY;
36         
37         return res < 0 ? 6 : res;
38     }
39     
40     public static int getDaysOfMonth(Calendar c)
41     {
42         return c.getActualMaximum(Calendar.DAY_OF_MONTH);
43     }
44     
45     public static ArrayList[] dispatchEventsInMonth(Calendar calendar, Iterator events, boolean sundayFirst)
46     {
47         ArrayList[] list = new ArrayList[42];
48         for(int i=0;i<list.length;i++)
49             list[i] = new ArrayList();
50         
51         int day = getFirstDayOfMonth(calendar, sundayFirst);
52         int max = getDaysOfMonth(calendar);
53         Calendar clone = (Calendar)calendar.clone();
54         
55         while(events.hasNext())
56         {
57             Event event = (Event)events.next();
58             
59             long start = event.getStartDate().getTime();
60             if(start < calendar.getTimeInMillis())
61                 start = calendar.getTimeInMillis();
62             clone.setTimeInMillis(start);
63             int dayOfMonth = clone.get(Calendar.DAY_OF_MONTH);
64             
65             long duration = event.getEndDate().getTime() - start;
66             int dayLength = 24*60*60*1000;
67             for(int i=0;i*dayLength<=duration;i++)
68             {
69                 int dayIndex = i + dayOfMonth + day -1;
70                 if(dayIndex > max)
71                     break;
72                 
73                 list[dayIndex].add(event);
74             }
75         }
76         
77         return list;
78     }
79     
80     public static ArrayList[] dispatchEventsInWeek(Calendar calendar, Iterator events, boolean sundayFirst)
81     {
82         ArrayList[] list = new ArrayList[7];
83         for(int i=0;i<list.length;i++)
84             list[i] = new ArrayList();
85         
86         Calendar clone = (Calendar)calendar.clone();
87         if(sundayFirst)
88             clone.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
89         else
90             clone.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
91         clone.set(Calendar.HOUR_OF_DAY, 0);
92         clone.set(Calendar.MINUTE, 0);
93         
94         Calendar startC = Calendar.getInstance();
95         Calendar endC = Calendar.getInstance();
96         
97         while(events.hasNext())
98         {
99             Event e = (Event)events.next();
100             startC.setTime(e.getStartDate());
101             endC.setTime(e.getEndDate());
102             
103             Calendar dayStart = Calendar.getInstance();
104             Calendar dayEnd = Calendar.getInstance();
105             dayStart.setTime(clone.getTime());
106             dayEnd.setTime(clone.getTime());
107             dayEnd.add(Calendar.DAY_OF_YEAR, 1);
108
109             for(int d=0;d<7;d++)
110             {
111                 if(startC.after(dayStart) && startC.before(dayEnd))
112                     list[d].add(e);
113                 else if(endC.after(dayStart) && endC.before(dayEnd))
114                     list[d].add(e);
115                 else if(startC.before(dayStart) && endC.after(dayEnd))
116                     list[d].add(e);
117                 
118                 dayStart.add(Calendar.DAY_OF_YEAR, 1);
119                 dayEnd.add(Calendar.DAY_OF_YEAR, 1);
120             }
121         }
122         
123         return list;
124     }
125 }
126
127
Popular Tags