KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > geinuke > util > CalendarTable


1
2  /*
3  -- GeiNuke --
4 Copyright (c) 2005 by Roberto Sidoti [geinuke@users.sourceforge.net]
5  http://www.hostingjava.it/-geinuke/
6
7 This file is part of GeiNuke.
8
9     GeiNuke is free software; you can redistribute it and/or modify
10     it under the terms of the GNU General Public License as published by
11     the Free Software Foundation; either version 2 of the License, or
12     (at your option) any later version.
13
14     GeiNuke is distributed in the hope that it will be useful,
15     but WITHOUT ANY WARRANTY; without even the implied warranty of
16     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17     GNU General Public License for more details.
18
19     You should have received a copy of the GNU General Public License
20     along with GeiNuke; if not, write to the Free Software
21     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 */

23 package com.geinuke.util;
24
25 import com.magic.util.EasyDate;
26
27
28
29
30 public class CalendarTable {
31   
32   static final int NCELLX = 7; /* number of cells across */
33  
34   static final int FEBRUARY = 1; /* special month during leap years */
35
36   // Data entry controls at top.
37

38   protected String JavaDoc caption="";
39   
40   // Date object to get current month and year.
41
public EasyDate now = null;
42
43   // Font for controls at top.
44

45   //String days[] = {"Sunday", "Monday", "Tuesday", "Wednesday","Thursday", "Friday", "Saturday"};
46
protected String JavaDoc days[] = { "M", "T", "W","TH", "F", "S","SU"};
47   protected String JavaDoc months[] = {"January", "February", "March", "April",
48                      "May", "June", "July", "August", "September",
49                      "October", "" + "November", "December"};
50
51   protected String JavaDoc[][] calendar={ {" "," "," "," "," "," "," "},
52                         {" "," "," "," "," "," "," "},
53                         {" "," "," "," "," "," "," "},
54                         {" "," "," "," "," "," "," "},
55                         {" "," "," "," "," "," "," "},
56                         {" "," "," "," "," "," "," "}
57                     };
58   protected int DaysInMonth[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
59
60   // Month and year entered by user.
61
protected int userMonth=0;
62   protected int userYear=0;
63   
64   protected int nextUserMonth=0;
65   protected int nextUserYear=0;
66   
67   protected int prevUserMonth=0;
68   protected int prevUserYear=0;
69
70   public CalendarTable(){
71     now=new EasyDate();
72     userMonth = now.getMese()-1;
73     userYear = now.getAnno();
74   } // init
75

76   public String JavaDoc[][] getTable(){
77     return this.calendar;
78   }
79   
80   public String JavaDoc getCaption(){
81     return this.caption;
82   }
83   
84   public String JavaDoc[] getDays(){
85     return this.days;
86   }
87   
88   protected String JavaDoc getXXNumber(int i){
89      String JavaDoc res=i+"";
90      if(i<10)
91         res=""+res;
92      return res;
93   }
94   
95   public void fillCalendar(){
96     fillCalendar(this.userMonth,this.userYear);
97   }
98   
99   public void fillCalendar(int userM,int userY){
100     this.userMonth=userM;
101     this.userYear=userY;
102     
103     if(this.nextUserMonth==11){
104         this.nextUserMonth=0;
105         this.nextUserYear=this.userYear+1;
106     }else{
107         this.nextUserMonth=this.userMonth+1;
108         this.nextUserYear=this.userYear;
109     }
110     this.nextUserMonth=this.userMonth+1;
111     this.nextUserYear=this.userYear;
112     this.nextUserYear+=(this.nextUserMonth/12);
113     this.nextUserMonth%=12;
114     
115     if(this.userMonth==0){
116         this.prevUserYear=this.userYear-1;
117         this.prevUserMonth=11;
118     }else{
119         this.prevUserMonth=this.userMonth-1;
120         this.prevUserYear=this.userYear;
121     }
122     
123     int numRows;
124     int xNum, yNum;
125     int numDays;
126     String JavaDoc dayStr;
127     
128     
129     
130     this.caption=this.months[userM]+" "+userY;
131     
132     
133     numRows = NumberRowsNeeded(userYear, userMonth);
134
135     
136     xNum = (CalcFirstOfMonth(userYear, userMonth) ) ;
137     yNum = 0;
138
139     
140     numDays = DaysInMonth[userMonth] +
141               ((IsLeapYear(userYear) && (userMonth == FEBRUARY)) ? 1 : 0);
142
143     
144     
145     for (int day = 1; day <= numDays; day++){
146         
147         
148       dayStr = this.getXXNumber(day);
149       this.calendar[yNum][xNum]=dayStr;
150       
151       xNum ++;
152       // If xNum to right of calendar, 'new line'.
153
if (xNum > 6){
154             xNum = 0;
155             yNum++;
156         } // if
157
} // for
158

159    
160     } // paint
161

162   
163
164   int NumberRowsNeeded(int year, int month)
165   /*
166      USE: Calculates number of rows needed for calendar.
167      IN: year = given year after 1582 (start of the Gregorian calendar).
168            month = 0 for January, 1 for February, etc.
169      OUT: Number of rows: 5 or 6, except for a 28 day February with
170            the first of the month on Sunday, requiring only four rows.
171   */

172     {
173     int firstDay; /* day of week for first day of month */
174     int numCells; /* number of cells needed by the month */
175   
176     /* Start at 1582, when modern calendar starts. */
177     if (year < 1582) return (-1);
178   
179     /* Catch month out of range. */
180     if ((month < 0) || (month > 11)) return (-1);
181   
182     /* Get first day of month. */
183     firstDay = CalcFirstOfMonth(year, month);
184   
185     /* Non leap year February with 1st on Sunday: 4 rows. */
186     if ((month == FEBRUARY) && (firstDay == 0) && !IsLeapYear(year))
187       return (4);
188   
189     /* Number of cells needed = blanks on 1st row + days in month. */
190     numCells = firstDay + DaysInMonth[month];
191   
192     /* One more cell needed for the Feb 29th in leap year. */
193     if ((month == FEBRUARY) && (IsLeapYear(year))) numCells++;
194   
195     /* 35 cells or less is 5 rows; more is 6. */
196     return ((numCells <= 35) ? 5 : 6);
197     } // NumberRowsNeeded
198

199   int CalcFirstOfMonth(int year, int month)
200   /*
201      USE: Calculates day of the week the first day of the month falls on.
202      IN: year = given year after 1582 (start of the Gregorian calendar).
203            month = 0 for January, 1 for February, etc.
204      OUT: First day of month: 0 = Sunday, 1 = Monday, etc.
205   */

206     {
207     int firstDay; /* day of week for Jan 1, then first day of month */
208     int i; /* to traverse months before given month */
209   
210     /* Start at 1582, when modern calendar starts. */
211     if (year < 1582) return (-1);
212   
213     /* Catch month out of range. */
214     if ((month < 0) || (month > 11)) return (-1);
215   
216     /* Get day of week for Jan 1 of given year. */
217     firstDay = CalcJanuaryFirst(year);
218   
219     /* Increase firstDay by days in year before given month to get first day
220      * of month.
221      */

222     for (i = 0; i < month; i++)
223       firstDay += DaysInMonth[i];
224   
225     /* Increase by one if month after February and leap year. */
226     if ((month > FEBRUARY) && IsLeapYear(year)) firstDay++;
227   
228     /* Convert to day of the week and return. */
229     firstDay--;
230     if(firstDay<=0)
231         firstDay+=7;
232     
233     return (firstDay % 7);
234    } // CalcFirstOfMonth
235

236   boolean IsLeapYear(int year)
237   /*
238      USE: Determines if given year is a leap year.
239      IN: year = given year after 1582 (start of the Gregorian calendar).
240      OUT: TRUE if given year is leap year, FALSE if not.
241      NOTE: Formulas capture definition of leap years; cf CalcLeapYears().
242   */

243     {
244   
245     /* If multiple of 100, leap year iff multiple of 400. */
246     if ((year % 100) == 0) return((year % 400) == 0);
247   
248     /* Otherwise leap year iff multiple of 4. */
249     return ((year % 4) == 0);
250     } // IsLeapYear
251

252   int CalcJanuaryFirst(int year)
253   /*
254      USE: Calculate day of the week on which January 1 falls for given year.
255      IN: year = given year after 1582 (start of the Gregorian calendar).
256      OUT: Day of week for January 1: 0 = Sunday, 1 = Monday, etc.
257      NOTE: Formula starts with a 5, since January 1, 1582 was a Friday; then
258            advances the day of the week by one for every year, adding the
259            number of leap years intervening, because those years Jan 1
260            advanced by two days. Calculate mod 7 to get the day of the week.
261   */

262     {
263     /* Start at 1582, when modern calendar starts. */
264     if (year < 1582) return (-1);
265   
266     /* Start Fri 01-01-1582; advance a day for each year, 2 for leap yrs. */
267     return ((5 + (year - 1582) + CalcLeapYears(year)) % 7);
268     } // CalcJanuaryFirst
269

270   int CalcLeapYears(int year)
271   /*
272      USE: Calculate number of leap years since 1582.
273      IN: year = given year after 1582 (start of the Gregorian calendar).
274      OUT: number of leap years since the given year, -1 if year < 1582
275      NOTE: Count doesn't include the given year if it is a leap year.
276            In the Gregorian calendar, used since 1582, every fourth year
277            is a leap year, except for years that are a multiple of a
278            hundred, but not a multiple of 400, which are no longer leap
279            years. Years that are a multiple of 400 are still leap years:
280            1700, 1800, 1990 were not leap years, but 2000 will be.
281   */

282     {
283     int leapYears; /* number of leap years to return */
284     int hundreds; /* number of years multiple of a hundred */
285     int fourHundreds; /* number of years multiple of four hundred */
286   
287     /* Start at 1582, when modern calendar starts. */
288     if (year < 1582) return (-1);
289   
290     /* Calculate number of years in interval that are a multiple of 4. */
291     leapYears = (year - 1581) / 4;
292   
293     /* Calculate number of years in interval that are a multiple of 100;
294      * subtract, since they are not leap years.
295      */

296     hundreds = (year - 1501) / 100;
297     leapYears -= hundreds;
298   
299     /* Calculate number of years in interval that are a multiple of 400;
300      * add back in, since they are still leap years.
301      */

302     fourHundreds = (year - 1201) / 400;
303     leapYears += fourHundreds;
304   
305     return (leapYears);
306     } // CalcLeapYears
307

308   public static void main(String JavaDoc [] ar){
309         CalendarTable o=new CalendarTable();
310         o.fillCalendar(11,2005);
311         System.out.println(o.caption);
312         for(int i=0;i<o.calendar.length;i++){
313             for(int j=0;j<o.calendar[i].length;j++)
314                 System.out.print(o.calendar[i][j]+" ");
315             System.out.println();
316         }
317         
318   }
319   
320 public int getNextUserMonth() {
321     return nextUserMonth;
322 }
323 public int getNextUserYear() {
324     return nextUserYear;
325 }
326 public int getPrevUserMonth() {
327     return prevUserMonth;
328 }
329 public int getPrevUserYear() {
330     return prevUserYear;
331 }
332 public int getUserMonth() {
333     return userMonth;
334 }
335 public int getUserYear() {
336     return userYear;
337 }
338 }
339
340
Popular Tags