1 58 59 package org.jfree.data.time; 60 61 import java.io.Serializable ; 62 import java.util.Calendar ; 63 import java.util.Date ; 64 import java.util.TimeZone ; 65 66 70 public class Hour extends RegularTimePeriod implements Serializable { 71 72 73 private static final long serialVersionUID = -835471579831937652L; 74 75 76 public static final int FIRST_HOUR_IN_DAY = 0; 77 78 79 public static final int LAST_HOUR_IN_DAY = 23; 80 81 82 private Day day; 83 84 85 private int hour; 86 87 90 public Hour() { 91 this(new Date ()); 92 } 93 94 100 public Hour(int hour, Day day) { 101 if (day == null) { 102 throw new IllegalArgumentException ("Null 'day' argument."); 103 } 104 this.hour = hour; 105 this.day = day; 106 } 107 108 116 public Hour(int hour, int day, int month, int year) { 117 this(hour, new Day(day, month, year)); 118 } 119 120 125 public Hour(Date time) { 126 this(time, RegularTimePeriod.DEFAULT_TIME_ZONE); 128 } 129 130 137 public Hour(Date time, TimeZone zone) { 138 if (time == null) { 139 throw new IllegalArgumentException ("Null 'time' argument."); 140 } 141 if (zone == null) { 142 throw new IllegalArgumentException ("Null 'zone' argument."); 143 } 144 Calendar calendar = Calendar.getInstance(zone); 145 calendar.setTime(time); 146 this.hour = calendar.get(Calendar.HOUR_OF_DAY); 147 this.day = new Day(time, zone); 148 } 149 150 155 public int getHour() { 156 return this.hour; 157 } 158 159 164 public Day getDay() { 165 return this.day; 166 } 167 168 173 public int getYear() { 174 return this.day.getYear(); 175 } 176 177 182 public int getMonth() { 183 return this.day.getMonth(); 184 } 185 186 191 public int getDayOfMonth() { 192 return this.day.getDayOfMonth(); 193 } 194 195 200 public RegularTimePeriod previous() { 201 202 Hour result; 203 if (this.hour != FIRST_HOUR_IN_DAY) { 204 result = new Hour(this.hour - 1, this.day); 205 } 206 else { Day prevDay = (Day) this.day.previous(); 208 if (prevDay != null) { 209 result = new Hour(LAST_HOUR_IN_DAY, prevDay); 210 } 211 else { 212 result = null; 213 } 214 } 215 return result; 216 217 } 218 219 224 public RegularTimePeriod next() { 225 226 Hour result; 227 if (this.hour != LAST_HOUR_IN_DAY) { 228 result = new Hour(this.hour + 1, this.day); 229 } 230 else { Day nextDay = (Day) this.day.next(); 232 if (nextDay != null) { 233 result = new Hour(FIRST_HOUR_IN_DAY, nextDay); 234 } 235 else { 236 result = null; 237 } 238 } 239 return result; 240 241 } 242 243 248 public long getSerialIndex() { 249 return this.day.getSerialIndex() * 24L + this.hour; 250 } 251 252 259 public long getFirstMillisecond(Calendar calendar) { 260 261 int year = this.day.getYear(); 262 int month = this.day.getMonth() - 1; 263 int dom = this.day.getDayOfMonth(); 264 265 calendar.set(year, month, dom, this.hour, 0, 0); 266 calendar.set(Calendar.MILLISECOND, 0); 267 268 return calendar.getTime().getTime(); 270 271 } 272 273 280 public long getLastMillisecond(Calendar calendar) { 281 282 int year = this.day.getYear(); 283 int month = this.day.getMonth() - 1; 284 int dom = this.day.getDayOfMonth(); 285 286 calendar.set(year, month, dom, this.hour, 59, 59); 287 calendar.set(Calendar.MILLISECOND, 999); 288 289 return calendar.getTime().getTime(); 291 292 } 293 294 305 public boolean equals(Object obj) { 306 if (obj == this) { 307 return true; 308 } 309 if (!(obj instanceof Hour)) { 310 return false; 311 } 312 Hour that = (Hour) obj; 313 if (this.hour != that.hour) { 314 return false; 315 } 316 if (!this.day.equals(that.day)) { 317 return false; 318 } 319 return true; 320 } 321 322 331 public int hashCode() { 332 int result = 17; 333 result = 37 * result + this.hour; 334 result = 37 * result + this.day.hashCode(); 335 return result; 336 } 337 338 348 public int compareTo(Object o1) { 349 350 int result; 351 352 if (o1 instanceof Hour) { 355 Hour h = (Hour) o1; 356 result = getDay().compareTo(h.getDay()); 357 if (result == 0) { 358 result = this.hour - h.getHour(); 359 } 360 } 361 362 else if (o1 instanceof RegularTimePeriod) { 365 result = 0; 367 } 368 369 else { 372 result = 1; 374 } 375 376 return result; 377 378 } 379 380 390 public static Hour parseHour(String s) { 391 392 Hour result = null; 393 s = s.trim(); 394 395 String daystr = s.substring(0, Math.min(10, s.length())); 396 Day day = Day.parseDay(daystr); 397 if (day != null) { 398 String hourstr = s.substring( 399 Math.min(daystr.length() + 1, s.length()), s.length() 400 ); 401 hourstr = hourstr.trim(); 402 int hour = Integer.parseInt(hourstr); 403 if ((hour >= FIRST_HOUR_IN_DAY) && (hour <= LAST_HOUR_IN_DAY)) { 405 result = new Hour(hour, day); 406 } 407 } 408 409 return result; 410 411 } 412 413 } 414 | Popular Tags |