1 16 package org.joda.time; 17 18 import org.joda.time.chrono.ISOChronology; 19 20 28 public class DateTimeUtils { 29 30 31 private static final SystemMillisProvider SYSTEM_MILLIS_PROVIDER = new SystemMillisProvider(); 32 33 34 private static MillisProvider cMillisProvider = SYSTEM_MILLIS_PROVIDER; 35 36 39 protected DateTimeUtils() { 40 super(); 41 } 42 43 52 public static final long currentTimeMillis() { 53 return cMillisProvider.getMillis(); 54 } 55 56 64 public static final void setCurrentMillisSystem() throws SecurityException { 65 checkPermission(); 66 cMillisProvider = SYSTEM_MILLIS_PROVIDER; 67 } 68 69 78 public static final void setCurrentMillisFixed(long fixedMillis) throws SecurityException { 79 checkPermission(); 80 cMillisProvider = new FixedMillisProvider(fixedMillis); 81 } 82 83 93 public static final void setCurrentMillisOffset(long offsetMillis) throws SecurityException { 94 checkPermission(); 95 if (offsetMillis == 0) { 96 cMillisProvider = SYSTEM_MILLIS_PROVIDER; 97 } else { 98 cMillisProvider = new OffsetMillisProvider(offsetMillis); 99 } 100 } 101 102 107 private static void checkPermission() throws SecurityException { 108 SecurityManager sm = System.getSecurityManager(); 109 if (sm != null) { 110 sm.checkPermission(new JodaTimePermission("CurrentTime.setProvider")); 111 } 112 } 113 114 124 public static final long getInstantMillis(ReadableInstant instant) { 125 if (instant == null) { 126 return DateTimeUtils.currentTimeMillis(); 127 } 128 return instant.getMillis(); 129 } 130 131 142 public static final Chronology getInstantChronology(ReadableInstant instant) { 143 if (instant == null) { 144 return ISOChronology.getInstance(); 145 } 146 Chronology chrono = instant.getChronology(); 147 if (chrono == null) { 148 return ISOChronology.getInstance(); 149 } 150 return chrono; 151 } 152 153 165 public static final Chronology getIntervalChronology(ReadableInstant start, ReadableInstant end) { 166 Chronology chrono = null; 167 if (start != null) { 168 chrono = start.getChronology(); 169 } else if (end != null) { 170 chrono = end.getChronology(); 171 } 172 if (chrono == null) { 173 chrono = ISOChronology.getInstance(); 174 } 175 return chrono; 176 } 177 178 189 public static final Chronology getIntervalChronology(ReadableInterval interval) { 190 if (interval == null) { 191 return ISOChronology.getInstance(); 192 } 193 Chronology chrono = interval.getChronology(); 194 if (chrono == null) { 195 return ISOChronology.getInstance(); 196 } 197 return chrono; 198 } 199 200 212 public static final ReadableInterval getReadableInterval(ReadableInterval interval) { 213 if (interval == null) { 214 long now = DateTimeUtils.currentTimeMillis(); 215 interval = new Interval(now, now); 216 } 217 return interval; 218 } 219 220 230 public static final Chronology getChronology(Chronology chrono) { 231 if (chrono == null) { 232 return ISOChronology.getInstance(); 233 } 234 return chrono; 235 } 236 237 247 public static final DateTimeZone getZone(DateTimeZone zone) { 248 if (zone == null) { 249 return DateTimeZone.getDefault(); 250 } 251 return zone; 252 } 253 254 264 public static final PeriodType getPeriodType(PeriodType type) { 265 if (type == null) { 266 return PeriodType.standard(); 267 } 268 return type; 269 } 270 271 281 public static final long getDurationMillis(ReadableDuration duration) { 282 if (duration == null) { 283 return 0L; 284 } 285 return duration.getMillis(); 286 } 287 288 312 public static final boolean isContiguous(ReadablePartial partial) { 313 if (partial == null) { 314 throw new IllegalArgumentException ("Partial must not be null"); 315 } 316 DurationFieldType lastType = null; 317 for (int i = 0; i < partial.size(); i++) { 318 DateTimeField loopField = partial.getField(i); 319 if (i > 0) { 320 if (loopField.getRangeDurationField().getType() != lastType) { 321 return false; 322 } 323 } 324 lastType = loopField.getDurationField().getType(); 325 } 326 return true; 327 } 328 329 333 abstract static class MillisProvider { 334 338 abstract long getMillis(); 339 } 340 341 344 static class SystemMillisProvider extends MillisProvider { 345 349 long getMillis() { 350 return System.currentTimeMillis(); 351 } 352 } 353 354 357 static class FixedMillisProvider extends MillisProvider { 358 359 private final long iMillis; 360 361 365 FixedMillisProvider(long fixedMillis) { 366 iMillis = fixedMillis; 367 } 368 369 373 long getMillis() { 374 return iMillis; 375 } 376 } 377 378 381 static class OffsetMillisProvider extends MillisProvider { 382 383 private final long iMillis; 384 385 389 OffsetMillisProvider(long offsetMillis) { 390 iMillis = offsetMillis; 391 } 392 393 397 long getMillis() { 398 return System.currentTimeMillis() + iMillis; 399 } 400 } 401 402 } 403 | Popular Tags |