1 56 57 package org.jfree.data.time; 58 59 import java.io.Serializable ; 60 import java.util.Calendar ; 61 import java.util.Date ; 62 import java.util.TimeZone ; 63 64 import org.jfree.date.MonthConstants; 65 import org.jfree.date.SerialDate; 66 67 71 public class Month extends RegularTimePeriod implements Serializable { 72 73 74 private static final long serialVersionUID = -5090216912548722570L; 75 76 77 private int month; 78 79 80 private Year year; 81 82 85 public Month() { 86 this(new Date ()); 87 } 88 89 95 public Month(int month, int year) { 96 this(month, new Year(year)); 97 } 98 99 105 public Month(int month, Year year) { 106 107 if ((month < 1) && (month > 12)) { 108 throw new IllegalArgumentException ("Month outside valid range."); 109 } 110 this.month = month; 111 this.year = year; 112 113 } 114 115 121 public Month(Date time) { 122 this(time, RegularTimePeriod.DEFAULT_TIME_ZONE); 123 } 124 125 131 public Month(Date time, TimeZone zone) { 132 Calendar calendar = Calendar.getInstance(zone); 133 calendar.setTime(time); 134 this.month = calendar.get(Calendar.MONTH) + 1; 135 this.year = new Year(calendar.get(Calendar.YEAR)); 136 } 137 138 143 public Year getYear() { 144 return this.year; 145 } 146 147 152 public int getYearValue() { 153 return this.year.getYear(); 154 } 155 156 161 public int getMonth() { 162 return this.month; 163 } 164 165 170 public RegularTimePeriod previous() { 171 172 Month result; 173 if (this.month != MonthConstants.JANUARY) { 174 result = new Month(this.month - 1, this.year); 175 } 176 else { 177 Year prevYear = (Year) this.year.previous(); 178 if (prevYear != null) { 179 result = new Month(MonthConstants.DECEMBER, prevYear); 180 } 181 else { 182 result = null; 183 } 184 } 185 return result; 186 187 } 188 189 194 public RegularTimePeriod next() { 195 Month result; 196 if (this.month != MonthConstants.DECEMBER) { 197 result = new Month(this.month + 1, this.year); 198 } 199 else { 200 Year nextYear = (Year) this.year.next(); 201 if (nextYear != null) { 202 result = new Month(MonthConstants.JANUARY, nextYear); 203 } 204 else { 205 result = null; 206 } 207 } 208 return result; 209 } 210 211 216 public long getSerialIndex() { 217 return this.year.getYear() * 12L + this.month; 218 } 219 220 227 public String toString() { 228 return SerialDate.monthCodeToString(this.month) + " " + this.year; 229 } 230 231 241 public boolean equals(Object obj) { 242 243 if (obj != null) { 244 if (obj instanceof Month) { 245 Month target = (Month) obj; 246 return ( 247 (this.month == target.getMonth()) 248 && (this.year.equals(target.getYear())) 249 ); 250 } 251 else { 252 return false; 253 } 254 } 255 else { 256 return false; 257 } 258 259 } 260 261 270 public int hashCode() { 271 int result = 17; 272 result = 37 * result + this.month; 273 result = 37 * result + this.year.hashCode(); 274 return result; 275 } 276 277 286 public int compareTo(Object o1) { 287 288 int result; 289 290 if (o1 instanceof Month) { 293 Month m = (Month) o1; 294 result = this.year.getYear() - m.getYear().getYear(); 295 if (result == 0) { 296 result = this.month - m.getMonth(); 297 } 298 } 299 300 else if (o1 instanceof RegularTimePeriod) { 303 result = 0; 305 } 306 307 else { 310 result = 1; 312 } 313 314 return result; 315 316 } 317 318 326 public long getFirstMillisecond(Calendar calendar) { 327 Day first = new Day(1, this.month, this.year.getYear()); 328 return first.getFirstMillisecond(calendar); 329 } 330 331 339 public long getLastMillisecond(Calendar calendar) { 340 int eom = SerialDate.lastDayOfMonth(this.month, this.year.getYear()); 341 Day last = new Day(eom, this.month, this.year.getYear()); 342 return last.getLastMillisecond(calendar); 343 } 344 345 356 public static Month parseMonth(String s) { 357 358 Month result = null; 359 if (s != null) { 360 361 s = s.trim(); 363 364 int i = Month.findSeparator(s); 365 if (i != -1) { 366 String s1 = s.substring(0, i).trim(); 367 String s2 = s.substring(i + 1, s.length()).trim(); 368 369 Year year = Month.evaluateAsYear(s1); 370 int month; 371 if (year != null) { 372 month = SerialDate.stringToMonthCode(s2); 373 if (month == -1) { 374 throw new TimePeriodFormatException( 375 "Can't evaluate the month." 376 ); 377 } 378 result = new Month(month, year); 379 } 380 else { 381 year = Month.evaluateAsYear(s2); 382 if (year != null) { 383 month = SerialDate.stringToMonthCode(s1); 384 if (month == -1) { 385 throw new TimePeriodFormatException( 386 "Can't evaluate the month." 387 ); 388 } 389 result = new Month(month, year); 390 } 391 else { 392 throw new TimePeriodFormatException( 393 "Can't evaluate the year." 394 ); 395 } 396 } 397 398 } 399 else { 400 throw new TimePeriodFormatException( 401 "Could not find separator." 402 ); 403 } 404 405 } 406 return result; 407 408 } 409 410 418 private static int findSeparator(String s) { 419 420 int result = s.indexOf('-'); 421 if (result == -1) { 422 result = s.indexOf(','); 423 } 424 if (result == -1) { 425 result = s.indexOf(' '); 426 } 427 if (result == -1) { 428 result = s.indexOf('.'); 429 } 430 return result; 431 } 432 433 442 private static Year evaluateAsYear(String s) { 443 444 Year result = null; 445 try { 446 result = Year.parseYear(s); 447 } 448 catch (TimePeriodFormatException e) { 449 } 451 return result; 452 453 } 454 455 } 456 | Popular Tags |