1 16 package org.joda.time.base; 17 18 import java.util.Date ; 19 20 import org.joda.time.Chronology; 21 import org.joda.time.DateTime; 22 import org.joda.time.DateTimeField; 23 import org.joda.time.DateTimeFieldType; 24 import org.joda.time.DateTimeUtils; 25 import org.joda.time.DateTimeZone; 26 import org.joda.time.Instant; 27 import org.joda.time.MutableDateTime; 28 import org.joda.time.ReadableInstant; 29 import org.joda.time.chrono.ISOChronology; 30 import org.joda.time.field.FieldUtils; 31 import org.joda.time.format.DateTimeFormatter; 32 import org.joda.time.format.ISODateTimeFormat; 33 34 54 public abstract class AbstractInstant implements ReadableInstant { 55 56 59 protected AbstractInstant() { 60 super(); 61 } 62 63 69 public DateTimeZone getZone() { 70 return getChronology().getZone(); 71 } 72 73 87 public int get(DateTimeFieldType type) { 88 if (type == null) { 89 throw new IllegalArgumentException ("The DateTimeFieldType must not be null"); 90 } 91 return type.getField(getChronology()).get(getMillis()); 92 } 93 94 101 public boolean isSupported(DateTimeFieldType type) { 102 if (type == null) { 103 return false; 104 } 105 return type.getField(getChronology()).isSupported(); 106 } 107 108 122 public int get(DateTimeField field) { 123 if (field == null) { 124 throw new IllegalArgumentException ("The DateTimeField must not be null"); 125 } 126 return field.get(getMillis()); 127 } 128 129 135 public Instant toInstant() { 136 return new Instant(getMillis()); 137 } 138 139 144 public DateTime toDateTime() { 145 return new DateTime(getMillis(), getZone()); 146 } 147 148 153 public DateTime toDateTimeISO() { 154 return new DateTime(getMillis(), ISOChronology.getInstance(getZone())); 155 } 156 157 163 public DateTime toDateTime(DateTimeZone zone) { 164 Chronology chrono = DateTimeUtils.getChronology(getChronology()); 165 chrono = chrono.withZone(zone); 166 return new DateTime(getMillis(), chrono); 167 } 168 169 175 public DateTime toDateTime(Chronology chronology) { 176 return new DateTime(getMillis(), chronology); 177 } 178 179 184 189 public MutableDateTime toMutableDateTime() { 190 return new MutableDateTime(getMillis(), getZone()); 191 } 192 193 198 public MutableDateTime toMutableDateTimeISO() { 199 return new MutableDateTime(getMillis(), ISOChronology.getInstance(getZone())); 200 } 201 202 208 public MutableDateTime toMutableDateTime(DateTimeZone zone) { 209 Chronology chrono = DateTimeUtils.getChronology(getChronology()); 210 chrono = chrono.withZone(zone); 211 return new MutableDateTime(getMillis(), chrono); 212 } 213 214 220 public MutableDateTime toMutableDateTime(Chronology chronology) { 221 return new MutableDateTime(getMillis(), chronology); 222 } 223 224 233 public Date toDate() { 234 return new Date (getMillis()); 235 } 236 237 251 public boolean equals(Object readableInstant) { 252 if (this == readableInstant) { 254 return true; 255 } 256 if (readableInstant instanceof ReadableInstant == false) { 257 return false; 258 } 259 ReadableInstant otherInstant = (ReadableInstant) readableInstant; 260 return 261 getMillis() == otherInstant.getMillis() && 262 FieldUtils.equals(getChronology(), otherInstant.getChronology()); 263 } 264 265 270 public int hashCode() { 271 return 273 ((int) (getMillis() ^ (getMillis() >>> 32))) + 274 (getChronology().hashCode()); 275 } 276 277 289 public int compareTo(Object instant) { 290 if (this == instant) { 291 return 0; 292 } 293 294 ReadableInstant otherInstant = (ReadableInstant) instant; 295 296 long otherMillis = otherInstant.getMillis(); 297 long thisMillis = getMillis(); 298 299 if (thisMillis == otherMillis) { 301 return 0; 302 } 303 if (thisMillis < otherMillis) { 304 return -1; 305 } else { 306 return 1; 307 } 308 } 309 310 318 public boolean isAfter(long instant) { 319 return (getMillis() > instant); 320 } 321 322 328 public boolean isAfterNow() { 329 return isAfter(DateTimeUtils.currentTimeMillis()); 330 } 331 332 339 public boolean isAfter(ReadableInstant instant) { 340 long instantMillis = DateTimeUtils.getInstantMillis(instant); 341 return isAfter(instantMillis); 342 } 343 344 352 public boolean isBefore(long instant) { 353 return (getMillis() < instant); 354 } 355 356 362 public boolean isBeforeNow() { 363 return isBefore(DateTimeUtils.currentTimeMillis()); 364 } 365 366 373 public boolean isBefore(ReadableInstant instant) { 374 long instantMillis = DateTimeUtils.getInstantMillis(instant); 375 return isBefore(instantMillis); 376 } 377 378 386 public boolean isEqual(long instant) { 387 return (getMillis() == instant); 388 } 389 390 396 public boolean isEqualNow() { 397 return isEqual(DateTimeUtils.currentTimeMillis()); 398 } 399 400 407 public boolean isEqual(ReadableInstant instant) { 408 long instantMillis = DateTimeUtils.getInstantMillis(instant); 409 return isEqual(instantMillis); 410 } 411 412 418 public String toString() { 419 return ISODateTimeFormat.dateTime().print(this); 420 } 421 422 430 public String toString(DateTimeFormatter formatter) { 431 if (formatter == null) { 432 return toString(); 433 } 434 return formatter.print(this); 435 } 436 437 } 438 | Popular Tags |