1 55 56 package org.jfree.data.time; 57 58 import java.io.Serializable ; 59 import java.util.Calendar ; 60 import java.util.Date ; 61 import java.util.TimeZone ; 62 63 import org.jfree.date.MonthConstants; 64 import org.jfree.date.SerialDate; 65 66 71 public class Quarter extends RegularTimePeriod implements Serializable { 72 73 74 private static final long serialVersionUID = 3810061714380888671L; 75 76 77 public static final int FIRST_QUARTER = 1; 78 79 80 public static final int LAST_QUARTER = 4; 81 82 83 public static final int[] FIRST_MONTH_IN_QUARTER = { 84 0, MonthConstants.JANUARY, MonthConstants.APRIL, MonthConstants.JULY, 85 MonthConstants.OCTOBER 86 }; 87 88 89 public static final int[] LAST_MONTH_IN_QUARTER = { 90 0, MonthConstants.MARCH, MonthConstants.JUNE, MonthConstants.SEPTEMBER, 91 MonthConstants.DECEMBER 92 }; 93 94 95 private Year year; 96 97 98 private int quarter; 99 100 103 public Quarter() { 104 this(new Date ()); 105 } 106 107 113 public Quarter(int quarter, int year) { 114 this(quarter, new Year(year)); 115 } 116 117 123 public Quarter(int quarter, Year year) { 124 if ((quarter < FIRST_QUARTER) && (quarter > LAST_QUARTER)) { 125 throw new IllegalArgumentException ("Quarter outside valid range."); 126 } 127 this.year = year; 128 this.quarter = quarter; 129 } 130 131 136 public Quarter(Date time) { 137 this(time, RegularTimePeriod.DEFAULT_TIME_ZONE); 138 } 139 140 146 public Quarter(Date time, TimeZone zone) { 147 148 Calendar calendar = Calendar.getInstance(zone); 149 calendar.setTime(time); 150 int month = calendar.get(Calendar.MONTH) + 1; 151 this.quarter = SerialDate.monthCodeToQuarter(month); 152 this.year = new Year(calendar.get(Calendar.YEAR)); 153 154 } 155 156 161 public int getQuarter() { 162 return this.quarter; 163 } 164 165 170 public Year getYear() { 171 return this.year; 172 } 173 174 179 public RegularTimePeriod previous() { 180 181 Quarter result; 182 if (this.quarter > FIRST_QUARTER) { 183 result = new Quarter(this.quarter - 1, this.year); 184 } 185 else { 186 Year prevYear = (Year) this.year.previous(); 187 if (prevYear != null) { 188 result = new Quarter(LAST_QUARTER, prevYear); 189 } 190 else { 191 result = null; 192 } 193 } 194 return result; 195 196 } 197 198 203 public RegularTimePeriod next() { 204 205 Quarter result; 206 if (this.quarter < LAST_QUARTER) { 207 result = new Quarter(this.quarter + 1, this.year); 208 } 209 else { 210 Year nextYear = (Year) this.year.next(); 211 if (nextYear != null) { 212 result = new Quarter(FIRST_QUARTER, nextYear); 213 } 214 else { 215 result = null; 216 } 217 } 218 return result; 219 220 } 221 222 227 public long getSerialIndex() { 228 return this.year.getYear() * 4L + this.quarter; 229 } 230 231 241 public boolean equals(Object obj) { 242 243 if (obj != null) { 244 if (obj instanceof Quarter) { 245 Quarter target = (Quarter) obj; 246 return ( 247 (this.quarter == target.getQuarter()) 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.quarter; 273 result = 37 * result + this.year.hashCode(); 274 return result; 275 } 276 277 287 public int compareTo(Object o1) { 288 289 int result; 290 291 if (o1 instanceof Quarter) { 294 Quarter q = (Quarter) o1; 295 result = this.year.getYear() - q.getYear().getYear(); 296 if (result == 0) { 297 result = this.quarter - q.getQuarter(); 298 } 299 } 300 301 else if (o1 instanceof RegularTimePeriod) { 304 result = 0; 306 } 307 308 else { 311 result = 1; 313 } 314 315 return result; 316 317 } 318 319 324 public String toString() { 325 return "Q" + this.quarter + "/" + this.year; 326 } 327 328 336 public long getFirstMillisecond(Calendar calendar) { 337 338 int month = Quarter.FIRST_MONTH_IN_QUARTER[this.quarter]; 339 Day first = new Day(1, month, this.year.getYear()); 340 return first.getFirstMillisecond(calendar); 341 342 } 343 344 352 public long getLastMillisecond(Calendar calendar) { 353 354 int month = Quarter.LAST_MONTH_IN_QUARTER[this.quarter]; 355 int eom = SerialDate.lastDayOfMonth(month, this.year.getYear()); 356 Day last = new Day(eom, month, this.year.getYear()); 357 return last.getLastMillisecond(calendar); 358 359 } 360 361 370 public static Quarter parseQuarter(String s) { 371 372 int i = s.indexOf("Q"); 375 if (i == -1) { 376 throw new TimePeriodFormatException("Missing Q."); 377 } 378 379 if (i == s.length() - 1) { 380 throw new TimePeriodFormatException("Q found at end of string."); 381 } 382 383 String qstr = s.substring(i + 1, i + 2); 384 int quarter = Integer.parseInt(qstr); 385 String remaining = s.substring(0, i) + s.substring(i + 2, s.length()); 386 387 remaining = remaining.replace('/', ' '); 389 remaining = remaining.replace(',', ' '); 390 remaining = remaining.replace('-', ' '); 391 392 Year year = Year.parseYear(remaining.trim()); 394 Quarter result = new Quarter(quarter, year); 395 return result; 396 397 } 398 399 } 400 | Popular Tags |