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 68 public class Second extends RegularTimePeriod implements Serializable { 69 70 71 private static final long serialVersionUID = -6536564190712383466L; 72 73 74 public static final int FIRST_SECOND_IN_MINUTE = 0; 75 76 77 public static final int LAST_SECOND_IN_MINUTE = 59; 78 79 80 private Minute minute; 81 82 83 private int second; 84 85 88 public Second() { 89 this(new Date ()); 90 } 91 92 98 public Second(int second, Minute minute) { 99 if (minute == null) { 100 throw new IllegalArgumentException ("Null 'minute' argument."); 101 } 102 this.minute = minute; 103 this.second = second; 104 } 105 106 116 public Second(int second, int minute, int hour, 117 int day, int month, int year) { 118 this(second, new Minute(minute, hour, day, month, year)); 119 } 120 121 126 public Second(Date time) { 127 this(time, RegularTimePeriod.DEFAULT_TIME_ZONE); 128 } 129 130 136 public Second(Date time, final TimeZone zone) { 137 this.minute = new Minute(time, zone); 138 Calendar calendar = Calendar.getInstance(zone); 139 calendar.setTime(time); 140 this.second = calendar.get(Calendar.SECOND); 141 } 142 143 148 public int getSecond() { 149 return this.second; 150 } 151 152 157 public Minute getMinute() { 158 return this.minute; 159 } 160 161 166 public RegularTimePeriod previous() { 167 168 Second result = null; 169 if (this.second != FIRST_SECOND_IN_MINUTE) { 170 result = new Second(this.second - 1, this.minute); 171 } 172 else { 173 Minute previous = (Minute) this.minute.previous(); 174 if (previous != null) { 175 result = new Second(LAST_SECOND_IN_MINUTE, previous); 176 } 177 } 178 return result; 179 180 } 181 182 187 public RegularTimePeriod next() { 188 189 Second result = null; 190 if (this.second != LAST_SECOND_IN_MINUTE) { 191 result = new Second(this.second + 1, this.minute); 192 } 193 else { 194 Minute next = (Minute) this.minute.next(); 195 if (next != null) { 196 result = new Second(FIRST_SECOND_IN_MINUTE, next); 197 } 198 } 199 return result; 200 201 } 202 203 208 public long getSerialIndex() { 209 return this.minute.getSerialIndex() * 60L + this.second; 210 } 211 212 219 public long getFirstMillisecond(Calendar calendar) { 220 return this.minute.getFirstMillisecond(calendar) + this.second * 1000L; 221 } 222 223 230 public long getLastMillisecond(Calendar calendar) { 231 return this.minute.getFirstMillisecond(calendar) 232 + this.second * 1000L + 999L; 233 } 234 235 246 public boolean equals(Object obj) { 247 if (obj instanceof Second) { 248 Second s = (Second) obj; 249 return ((this.second == s.getSecond()) 250 && (this.minute.equals(s.getMinute()))); 251 } 252 else { 253 return false; 254 } 255 } 256 257 266 public int hashCode() { 267 int result = 17; 268 result = 37 * result + this.second; 269 result = 37 * result + this.minute.hashCode(); 270 return result; 271 } 272 273 282 public int compareTo(Object o1) { 283 284 int result; 285 286 if (o1 instanceof Second) { 289 Second s = (Second) o1; 290 result = this.minute.compareTo(s.minute); 291 if (result == 0) { 292 result = this.second - s.second; 293 } 294 } 295 296 else if (o1 instanceof RegularTimePeriod) { 299 result = 0; 301 } 302 303 else { 306 result = 1; 308 } 309 310 return result; 311 312 } 313 314 323 public static Second parseSecond(String s) { 324 325 Second result = null; 326 s = s.trim(); 327 328 String daystr = s.substring(0, Math.min(10, s.length())); 329 Day day = Day.parseDay(daystr); 330 if (day != null) { 331 String hmsstr = s.substring( 332 Math.min(daystr.length() + 1, s.length()), s.length() 333 ); 334 hmsstr = hmsstr.trim(); 335 336 int l = hmsstr.length(); 337 String hourstr = hmsstr.substring(0, Math.min(2, l)); 338 String minstr = hmsstr.substring(Math.min(3, l), Math.min(5, l)); 339 String secstr = hmsstr.substring(Math.min(6, l), Math.min(8, l)); 340 int hour = Integer.parseInt(hourstr); 341 342 if ((hour >= 0) && (hour <= 23)) { 343 344 int minute = Integer.parseInt(minstr); 345 if ((minute >= 0) && (minute <= 59)) { 346 347 Minute m = new Minute(minute, new Hour(hour, day)); 348 int second = Integer.parseInt(secstr); 349 if ((second >= 0) && (second <= 59)) { 350 result = new Second(second, m); 351 } 352 } 353 } 354 } 355 356 return result; 357 358 } 359 360 } 361 | Popular Tags |