1 2 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; 33 34 static final int FEBRUARY = 1; 35 36 38 protected String caption=""; 39 40 public EasyDate now = null; 42 43 45 protected String days[] = { "M", "T", "W","TH", "F", "S","SU"}; 47 protected String months[] = {"January", "February", "March", "April", 48 "May", "June", "July", "August", "September", 49 "October", "" + "November", "December"}; 50 51 protected String [][] 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 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 } 76 public String [][] getTable(){ 77 return this.calendar; 78 } 79 80 public String getCaption(){ 81 return this.caption; 82 } 83 84 public String [] getDays(){ 85 return this.days; 86 } 87 88 protected String getXXNumber(int i){ 89 String 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 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 > 6){ 154 xNum = 0; 155 yNum++; 156 } } 159 160 } 162 163 164 int NumberRowsNeeded(int year, int month) 165 172 { 173 int firstDay; 174 int numCells; 175 176 177 if (year < 1582) return (-1); 178 179 180 if ((month < 0) || (month > 11)) return (-1); 181 182 183 firstDay = CalcFirstOfMonth(year, month); 184 185 186 if ((month == FEBRUARY) && (firstDay == 0) && !IsLeapYear(year)) 187 return (4); 188 189 190 numCells = firstDay + DaysInMonth[month]; 191 192 193 if ((month == FEBRUARY) && (IsLeapYear(year))) numCells++; 194 195 196 return ((numCells <= 35) ? 5 : 6); 197 } 199 int CalcFirstOfMonth(int year, int month) 200 206 { 207 int firstDay; 208 int i; 209 210 211 if (year < 1582) return (-1); 212 213 214 if ((month < 0) || (month > 11)) return (-1); 215 216 217 firstDay = CalcJanuaryFirst(year); 218 219 222 for (i = 0; i < month; i++) 223 firstDay += DaysInMonth[i]; 224 225 226 if ((month > FEBRUARY) && IsLeapYear(year)) firstDay++; 227 228 229 firstDay--; 230 if(firstDay<=0) 231 firstDay+=7; 232 233 return (firstDay % 7); 234 } 236 boolean IsLeapYear(int year) 237 243 { 244 245 246 if ((year % 100) == 0) return((year % 400) == 0); 247 248 249 return ((year % 4) == 0); 250 } 252 int CalcJanuaryFirst(int year) 253 262 { 263 264 if (year < 1582) return (-1); 265 266 267 return ((5 + (year - 1582) + CalcLeapYears(year)) % 7); 268 } 270 int CalcLeapYears(int year) 271 282 { 283 int leapYears; 284 int hundreds; 285 int fourHundreds; 286 287 288 if (year < 1582) return (-1); 289 290 291 leapYears = (year - 1581) / 4; 292 293 296 hundreds = (year - 1501) / 100; 297 leapYears -= hundreds; 298 299 302 fourHundreds = (year - 1201) / 400; 303 leapYears += fourHundreds; 304 305 return (leapYears); 306 } 308 public static void main(String [] 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 |