1 16 package org.joda.time; 17 18 import java.io.Serializable ; 19 20 import org.joda.time.base.AbstractInstant; 21 import org.joda.time.chrono.ISOChronology; 22 import org.joda.time.convert.ConverterManager; 23 import org.joda.time.convert.InstantConverter; 24 25 49 public final class Instant 50 extends AbstractInstant 51 implements ReadableInstant, Serializable { 52 53 54 private static final long serialVersionUID = 3299096530934209741L; 55 56 57 private final long iMillis; 58 59 63 public Instant() { 64 super(); 65 iMillis = DateTimeUtils.currentTimeMillis(); 66 } 67 68 73 public Instant(long instant) { 74 super(); 75 iMillis = instant; 76 } 77 78 87 public Instant(Object instant) { 88 super(); 89 InstantConverter converter = ConverterManager.getInstance().getInstantConverter(instant); 90 iMillis = converter.getInstantMillis(instant, ISOChronology.getInstanceUTC()); 91 } 92 93 99 public Instant toInstant() { 100 return this; 101 } 102 103 112 public Instant withMillis(long newMillis) { 113 return (newMillis == iMillis ? this : new Instant(newMillis)); 114 } 115 116 126 public Instant withDurationAdded(long durationToAdd, int scalar) { 127 if (durationToAdd == 0 || scalar == 0) { 128 return this; 129 } 130 long instant = getChronology().add(getMillis(), durationToAdd, scalar); 131 return withMillis(instant); 132 } 133 134 144 public Instant withDurationAdded(ReadableDuration durationToAdd, int scalar) { 145 if (durationToAdd == null || scalar == 0) { 146 return this; 147 } 148 return withDurationAdded(durationToAdd.getMillis(), scalar); 149 } 150 151 161 public Instant plus(long duration) { 162 return withDurationAdded(duration, 1); 163 } 164 165 174 public Instant plus(ReadableDuration duration) { 175 return withDurationAdded(duration, 1); 176 } 177 178 188 public Instant minus(long duration) { 189 return withDurationAdded(duration, -1); 190 } 191 192 201 public Instant minus(ReadableDuration duration) { 202 return withDurationAdded(duration, -1); 203 } 204 205 211 public long getMillis() { 212 return iMillis; 213 } 214 215 220 public Chronology getChronology() { 221 return ISOChronology.getInstanceUTC(); 222 } 223 224 240 public DateTime toDateTime() { 241 return new DateTime(getMillis(), ISOChronology.getInstance()); 242 } 243 244 261 public DateTime toDateTimeISO() { 262 return toDateTime(); 263 } 264 265 280 public MutableDateTime toMutableDateTime() { 281 return new MutableDateTime(getMillis(), ISOChronology.getInstance()); 282 } 283 284 301 public MutableDateTime toMutableDateTimeISO() { 302 return toMutableDateTime(); 303 } 304 305 } 306 | Popular Tags |