1 59 60 package org.jfree.data.time; 61 62 import java.io.Serializable ; 63 import java.text.DateFormat ; 64 import java.text.ParseException ; 65 import java.text.SimpleDateFormat ; 66 import java.util.Calendar ; 67 import java.util.Date ; 68 import java.util.TimeZone ; 69 70 import org.jfree.date.SerialDate; 71 72 77 public class Day extends RegularTimePeriod implements Serializable { 78 79 80 private static final long serialVersionUID = -7082667380758962755L; 81 82 83 protected static final DateFormat DATE_FORMAT 84 = new SimpleDateFormat ("yyyy-MM-dd"); 85 86 87 protected static final DateFormat 88 DATE_FORMAT_SHORT = DateFormat.getDateInstance(DateFormat.SHORT); 89 90 91 protected static final DateFormat 92 DATE_FORMAT_MEDIUM = DateFormat.getDateInstance(DateFormat.MEDIUM); 93 94 95 protected static final DateFormat 96 DATE_FORMAT_LONG = DateFormat.getDateInstance(DateFormat.LONG); 97 98 99 private SerialDate serialDate; 100 101 105 public Day() { 106 this(new Date ()); 107 } 108 109 116 public Day(int day, int month, int year) { 117 this.serialDate = SerialDate.createInstance(day, month, year); 118 } 119 120 125 public Day(SerialDate serialDate) { 126 if (serialDate == null) { 127 throw new IllegalArgumentException ("Null 'serialDate' argument."); 128 } 129 this.serialDate = serialDate; 130 } 131 132 138 public Day(Date time) { 139 this(time, RegularTimePeriod.DEFAULT_TIME_ZONE); 141 } 142 143 149 public Day(Date time, TimeZone zone) { 150 if (time == null) { 151 throw new IllegalArgumentException ("Null 'time' argument."); 152 } 153 if (zone == null) { 154 throw new IllegalArgumentException ("Null 'zone' argument."); 155 } 156 Calendar calendar = Calendar.getInstance(zone); 157 calendar.setTime(time); 158 int d = calendar.get(Calendar.DAY_OF_MONTH); 159 int m = calendar.get(Calendar.MONTH) + 1; 160 int y = calendar.get(Calendar.YEAR); 161 this.serialDate = SerialDate.createInstance(d, m, y); 162 } 163 164 173 public SerialDate getSerialDate() { 174 return this.serialDate; 175 } 176 177 182 public int getYear() { 183 return this.serialDate.getYYYY(); 184 } 185 186 191 public int getMonth() { 192 return this.serialDate.getMonth(); 193 } 194 195 200 public int getDayOfMonth() { 201 return this.serialDate.getDayOfMonth(); 202 } 203 204 209 public RegularTimePeriod previous() { 210 211 Day result; 212 int serial = this.serialDate.toSerial(); 213 if (serial > SerialDate.SERIAL_LOWER_BOUND) { 214 SerialDate yesterday = SerialDate.createInstance(serial - 1); 215 return new Day(yesterday); 216 } 217 else { 218 result = null; 219 } 220 return result; 221 222 } 223 224 231 public RegularTimePeriod next() { 232 233 Day result; 234 int serial = this.serialDate.toSerial(); 235 if (serial < SerialDate.SERIAL_UPPER_BOUND) { 236 SerialDate tomorrow = SerialDate.createInstance(serial + 1); 237 return new Day(tomorrow); 238 } 239 else { 240 result = null; 241 } 242 return result; 243 244 } 245 246 251 public long getSerialIndex() { 252 return this.serialDate.toSerial(); 253 } 254 255 263 public long getFirstMillisecond(Calendar calendar) { 264 265 int year = this.serialDate.getYYYY(); 266 int month = this.serialDate.getMonth(); 267 int day = this.serialDate.getDayOfMonth(); 268 calendar.clear(); 269 calendar.set(year, month - 1, day, 0, 0, 0); 270 calendar.set(Calendar.MILLISECOND, 0); 271 return calendar.getTime().getTime(); 273 274 } 275 276 284 public long getLastMillisecond(Calendar calendar) { 285 286 int year = this.serialDate.getYYYY(); 287 int month = this.serialDate.getMonth(); 288 int day = this.serialDate.getDayOfMonth(); 289 calendar.clear(); 290 calendar.set(year, month - 1, day, 23, 59, 59); 291 calendar.set(Calendar.MILLISECOND, 999); 292 return calendar.getTime().getTime(); 294 295 } 296 297 307 public boolean equals(Object obj) { 308 309 if (obj == this) { 310 return true; 311 } 312 if (!(obj instanceof Day)) { 313 return false; 314 } 315 Day that = (Day) obj; 316 if (!this.serialDate.equals(that.getSerialDate())) { 317 return false; 318 } 319 return true; 320 321 } 322 323 332 public int hashCode() { 333 return this.serialDate.hashCode(); 334 } 335 336 346 public int compareTo(Object o1) { 347 348 int result; 349 350 if (o1 instanceof Day) { 353 Day d = (Day) o1; 354 result = -d.getSerialDate().compare(this.serialDate); 355 } 356 357 else if (o1 instanceof RegularTimePeriod) { 360 result = 0; 362 } 363 364 else { 367 result = 1; 369 } 370 371 return result; 372 373 } 374 375 380 public String toString() { 381 return this.serialDate.toString(); 382 } 383 384 395 public static Day parseDay(String s) { 396 397 try { 398 return new Day (Day.DATE_FORMAT.parse(s)); 399 } 400 catch (ParseException e1) { 401 try { 402 return new Day (Day.DATE_FORMAT_SHORT.parse(s)); 403 } 404 catch (ParseException e2) { 405 } 407 } 408 return null; 409 410 } 411 412 } 413 | Popular Tags |