1 60 61 package org.jfree.data.time; 62 63 import java.io.Serializable ; 64 import java.util.Calendar ; 65 import java.util.Date ; 66 import java.util.TimeZone ; 67 68 72 public class Minute extends RegularTimePeriod implements Serializable { 73 74 75 private static final long serialVersionUID = 2144572840034842871L; 76 77 78 public static final int FIRST_MINUTE_IN_HOUR = 0; 79 80 81 public static final int LAST_MINUTE_IN_HOUR = 59; 82 83 84 private Hour hour; 85 86 87 private int minute; 88 89 92 public Minute() { 93 this(new Date ()); 94 } 95 96 102 public Minute(int minute, Hour hour) { 103 if (hour == null) { 104 throw new IllegalArgumentException ("Null 'hour' argument."); 105 } 106 this.minute = minute; 107 this.hour = hour; 108 } 109 110 115 public Minute(Date time) { 116 this(time, RegularTimePeriod.DEFAULT_TIME_ZONE); 118 } 119 120 126 public Minute(Date time, TimeZone zone) { 127 128 if (time == null) { 129 throw new IllegalArgumentException ("Null 'time' argument."); 130 } 131 if (zone == null) { 132 throw new IllegalArgumentException ("Null 'zone' argument."); 133 } 134 Calendar calendar = Calendar.getInstance(zone); 135 calendar.setTime(time); 136 int min = calendar.get(Calendar.MINUTE); 137 this.minute = min; 138 this.hour = new Hour(time, zone); 139 140 } 141 142 151 public Minute(int minute, 152 int hour, 153 int day, 154 int month, 155 int year) { 156 this(minute, new Hour(hour, new Day(day, month, year))); 157 } 158 159 164 public Hour getHour() { 165 return this.hour; 166 } 167 168 173 public int getMinute() { 174 return this.minute; 175 } 176 177 182 public RegularTimePeriod previous() { 183 184 Minute result; 185 if (this.minute != FIRST_MINUTE_IN_HOUR) { 186 result = new Minute(this.minute - 1, this.hour); 187 } 188 else { Hour prevHour = (Hour) this.hour.previous(); 190 if (prevHour != null) { 191 result = new Minute(LAST_MINUTE_IN_HOUR, prevHour); 192 } 193 else { 194 result = null; 195 } 196 } 197 return result; 198 199 } 200 201 206 public RegularTimePeriod next() { 207 208 Minute result; 209 if (this.minute != LAST_MINUTE_IN_HOUR) { 210 result = new Minute(this.minute + 1, this.hour); 211 } 212 else { Hour nextHour = (Hour) this.hour.next(); 214 if (nextHour != null) { 215 result = new Minute(FIRST_MINUTE_IN_HOUR, nextHour); 216 } 217 else { 218 result = null; 219 } 220 } 221 return result; 222 223 } 224 225 230 public long getSerialIndex() { 231 return this.hour.getSerialIndex() * 60L + this.minute; 232 } 233 234 241 public long getFirstMillisecond(Calendar calendar) { 242 243 int year = this.hour.getDay().getYear(); 244 int month = this.hour.getDay().getMonth() - 1; 245 int day = this.hour.getDay().getDayOfMonth(); 246 247 calendar.clear(); 248 calendar.set(year, month, day, this.hour.getHour(), this.minute, 0); 249 calendar.set(Calendar.MILLISECOND, 0); 250 251 return calendar.getTime().getTime(); 253 254 } 255 256 263 public long getLastMillisecond(Calendar calendar) { 264 265 int year = this.hour.getDay().getYear(); 266 int month = this.hour.getDay().getMonth() - 1; 267 int day = this.hour.getDay().getDayOfMonth(); 268 269 calendar.clear(); 270 calendar.set(year, month, day, this.hour.getHour(), this.minute, 59); 271 calendar.set(Calendar.MILLISECOND, 999); 272 273 return calendar.getTime().getTime(); 275 276 } 277 278 289 public boolean equals(Object obj) { 290 if (obj == this) { 291 return true; 292 } 293 if (!(obj instanceof Minute)) { 294 return false; 295 } 296 Minute that = (Minute) obj; 297 if (this.minute != that.minute) { 298 return false; 299 } 300 if (!this.hour.equals(that.hour)) { 301 return false; 302 } 303 return true; 304 } 305 306 315 public int hashCode() { 316 int result = 17; 317 result = 37 * result + this.minute; 318 result = 37 * result + this.hour.hashCode(); 319 return result; 320 } 321 322 332 public int compareTo(Object o1) { 333 334 int result; 335 336 if (o1 instanceof Minute) { 339 Minute m = (Minute) o1; 340 result = getHour().compareTo(m.getHour()); 341 if (result == 0) { 342 result = this.minute - m.getMinute(); 343 } 344 } 345 346 else if (o1 instanceof RegularTimePeriod) { 349 result = 0; 351 } 352 353 else { 356 result = 1; 358 } 359 360 return result; 361 362 } 363 364 374 public static Minute parseMinute(String s) { 375 376 Minute result = null; 377 s = s.trim(); 378 379 String daystr = s.substring(0, Math.min(10, s.length())); 380 Day day = Day.parseDay(daystr); 381 if (day != null) { 382 String hmstr = s.substring( 383 Math.min(daystr.length() + 1, s.length()), s.length() 384 ); 385 hmstr = hmstr.trim(); 386 387 String hourstr = hmstr.substring(0, Math.min(2, hmstr.length())); 388 int hour = Integer.parseInt(hourstr); 389 390 if ((hour >= 0) && (hour <= 23)) { 391 String minstr = hmstr.substring( 392 Math.min(hourstr.length() + 1, hmstr.length()), 393 hmstr.length() 394 ); 395 int minute = Integer.parseInt(minstr); 396 if ((minute >= 0) && (minute <= 59)) { 397 result = new Minute(minute, new Hour(hour, day)); 398 } 399 } 400 } 401 402 return result; 403 404 } 405 406 } 407 | Popular Tags |