/
DateTimeUtils.java
596 lines (560 loc) · 21.9 KB
/
DateTimeUtils.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
/*
* Copyright 2001-2013 Stephen Colebourne
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.joda.time;
import java.lang.reflect.Method;
import java.text.DateFormatSymbols;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Locale;
import java.util.Map;
import java.util.concurrent.atomic.AtomicReference;
import org.joda.time.chrono.ISOChronology;
/**
* DateTimeUtils provide public utility methods for the date-time library.
* <p>
* DateTimeUtils uses shared static variables which are declared as volatile
* for thread-safety. These can be changed during the lifetime of the application
* however doing so is generally a bad idea.
*
* @author Stephen Colebourne
* @since 1.0
*/
public class DateTimeUtils {
/** The singleton instance of the system millisecond provider. */
private static final SystemMillisProvider SYSTEM_MILLIS_PROVIDER = new SystemMillisProvider();
/** The millisecond provider currently in use. */
private static volatile MillisProvider cMillisProvider = SYSTEM_MILLIS_PROVIDER;
/**
* The default names.
* This is lazily initialized to reduce risks of race conditions at startup.
*/
private static final AtomicReference<Map<String, DateTimeZone>> cZoneNames =
new AtomicReference<Map<String,DateTimeZone>>();
/**
* Restrictive constructor
*/
protected DateTimeUtils() {
super();
}
//-----------------------------------------------------------------------
/**
* Gets the current time in milliseconds.
* <p>
* By default this returns <code>System.currentTimeMillis()</code>.
* This may be changed using other methods in this class.
*
* @return the current time in milliseconds from 1970-01-01T00:00:00Z
*/
public static final long currentTimeMillis() {
return cMillisProvider.getMillis();
}
/**
* Resets the current time to return the system time.
* <p>
* This method changes the behaviour of {@link #currentTimeMillis()}.
* Whenever the current time is queried, {@link System#currentTimeMillis()} is used.
*
* @throws SecurityException if the application does not have sufficient security rights
*/
public static final void setCurrentMillisSystem() throws SecurityException {
checkPermission();
cMillisProvider = SYSTEM_MILLIS_PROVIDER;
}
/**
* Sets the current time to return a fixed millisecond time.
* <p>
* This method changes the behaviour of {@link #currentTimeMillis()}.
* Whenever the current time is queried, the same millisecond time will be returned.
*
* @param fixedMillis the fixed millisecond time to use
* @throws SecurityException if the application does not have sufficient security rights
*/
public static final void setCurrentMillisFixed(long fixedMillis) throws SecurityException {
checkPermission();
cMillisProvider = new FixedMillisProvider(fixedMillis);
}
/**
* Sets the current time to return the system time plus an offset.
* <p>
* This method changes the behaviour of {@link #currentTimeMillis()}.
* Whenever the current time is queried, {@link System#currentTimeMillis()} is used
* and then offset by adding the millisecond value specified here.
*
* @param offsetMillis the fixed millisecond time to use
* @throws SecurityException if the application does not have sufficient security rights
*/
public static final void setCurrentMillisOffset(long offsetMillis) throws SecurityException {
checkPermission();
if (offsetMillis == 0) {
cMillisProvider = SYSTEM_MILLIS_PROVIDER;
} else {
cMillisProvider = new OffsetMillisProvider(offsetMillis);
}
}
/**
* Sets the provider of the current time to class specified.
* <p>
* This method changes the behaviour of {@link #currentTimeMillis()}.
* Whenever the current time is queried, the specified class will be called.
*
* @param millisProvider the provider of the current time to use, not null
* @throws SecurityException if the application does not have sufficient security rights
* @since 2.0
*/
public static final void setCurrentMillisProvider(MillisProvider millisProvider) throws SecurityException {
if (millisProvider == null) {
throw new IllegalArgumentException("The MillisProvider must not be null");
}
checkPermission();
cMillisProvider = millisProvider;
}
/**
* Checks whether the provider may be changed using permission 'CurrentTime.setProvider'.
*
* @throws SecurityException if the provider may not be changed
*/
private static void checkPermission() throws SecurityException {
SecurityManager sm = System.getSecurityManager();
if (sm != null) {
sm.checkPermission(new JodaTimePermission("CurrentTime.setProvider"));
}
}
//-----------------------------------------------------------------------
/**
* Gets the millisecond instant from the specified instant object handling null.
* <p>
* If the instant object is <code>null</code>, the {@link #currentTimeMillis()}
* will be returned. Otherwise, the millis from the object are returned.
*
* @param instant the instant to examine, null means now
* @return the time in milliseconds from 1970-01-01T00:00:00Z
*/
public static final long getInstantMillis(ReadableInstant instant) {
if (instant == null) {
return DateTimeUtils.currentTimeMillis();
}
return instant.getMillis();
}
//-----------------------------------------------------------------------
/**
* Gets the chronology from the specified instant object handling null.
* <p>
* If the instant object is <code>null</code>, or the instant's chronology is
* <code>null</code>, {@link ISOChronology#getInstance()} will be returned.
* Otherwise, the chronology from the object is returned.
*
* @param instant the instant to examine, null means ISO in the default zone
* @return the chronology, never null
*/
public static final Chronology getInstantChronology(ReadableInstant instant) {
if (instant == null) {
return ISOChronology.getInstance();
}
Chronology chrono = instant.getChronology();
if (chrono == null) {
return ISOChronology.getInstance();
}
return chrono;
}
//-----------------------------------------------------------------------
/**
* Gets the chronology from the specified instant based interval handling null.
* <p>
* The chronology is obtained from the start if that is not null, or from the
* end if the start is null. The result is additionally checked, and if still
* null then {@link ISOChronology#getInstance()} will be returned.
*
* @param start the instant to examine and use as the primary source of the chronology
* @param end the instant to examine and use as the secondary source of the chronology
* @return the chronology, never null
*/
public static final Chronology getIntervalChronology(ReadableInstant start, ReadableInstant end) {
Chronology chrono = null;
if (start != null) {
chrono = start.getChronology();
} else if (end != null) {
chrono = end.getChronology();
}
if (chrono == null) {
chrono = ISOChronology.getInstance();
}
return chrono;
}
//-----------------------------------------------------------------------
/**
* Gets the chronology from the specified interval object handling null.
* <p>
* If the interval object is <code>null</code>, or the interval's chronology is
* <code>null</code>, {@link ISOChronology#getInstance()} will be returned.
* Otherwise, the chronology from the object is returned.
*
* @param interval the interval to examine, null means ISO in the default zone
* @return the chronology, never null
*/
public static final Chronology getIntervalChronology(ReadableInterval interval) {
if (interval == null) {
return ISOChronology.getInstance();
}
Chronology chrono = interval.getChronology();
if (chrono == null) {
return ISOChronology.getInstance();
}
return chrono;
}
//-----------------------------------------------------------------------
/**
* Gets the interval handling null.
* <p>
* If the interval is <code>null</code>, an interval representing now
* to now in the {@link ISOChronology#getInstance() ISOChronology}
* will be returned. Otherwise, the interval specified is returned.
*
* @param interval the interval to use, null means now to now
* @return the interval, never null
* @since 1.1
*/
public static final ReadableInterval getReadableInterval(ReadableInterval interval) {
if (interval == null) {
long now = DateTimeUtils.currentTimeMillis();
interval = new Interval(now, now);
}
return interval;
}
//-----------------------------------------------------------------------
/**
* Gets the chronology handling null.
* <p>
* If the chronology is <code>null</code>, {@link ISOChronology#getInstance()}
* will be returned. Otherwise, the chronology is returned.
*
* @param chrono the chronology to use, null means ISO in the default zone
* @return the chronology, never null
*/
public static final Chronology getChronology(Chronology chrono) {
if (chrono == null) {
return ISOChronology.getInstance();
}
return chrono;
}
//-----------------------------------------------------------------------
/**
* Gets the zone handling null.
* <p>
* If the zone is <code>null</code>, {@link DateTimeZone#getDefault()}
* will be returned. Otherwise, the zone specified is returned.
*
* @param zone the time zone to use, null means the default zone
* @return the time zone, never null
*/
public static final DateTimeZone getZone(DateTimeZone zone) {
if (zone == null) {
return DateTimeZone.getDefault();
}
return zone;
}
//-----------------------------------------------------------------------
/**
* Gets the period type handling null.
* <p>
* If the zone is <code>null</code>, {@link PeriodType#standard()}
* will be returned. Otherwise, the type specified is returned.
*
* @param type the time zone to use, null means the standard type
* @return the type to use, never null
*/
public static final PeriodType getPeriodType(PeriodType type) {
if (type == null) {
return PeriodType.standard();
}
return type;
}
//-----------------------------------------------------------------------
/**
* Gets the millisecond duration from the specified duration object handling null.
* <p>
* If the duration object is <code>null</code>, zero will be returned.
* Otherwise, the millis from the object are returned.
*
* @param duration the duration to examine, null means zero
* @return the duration in milliseconds
*/
public static final long getDurationMillis(ReadableDuration duration) {
if (duration == null) {
return 0L;
}
return duration.getMillis();
}
//-----------------------------------------------------------------------
/**
* Checks whether the partial is contiguous.
* <p>
* A partial is contiguous if one field starts where another ends.
* <p>
* For example <code>LocalDate</code> is contiguous because DayOfMonth has
* the same range (Month) as the unit of the next field (MonthOfYear), and
* MonthOfYear has the same range (Year) as the unit of the next field (Year).
* <p>
* Similarly, <code>LocalTime</code> is contiguous, as it consists of
* MillisOfSecond, SecondOfMinute, MinuteOfHour and HourOfDay (note how
* the names of each field 'join up').
* <p>
* However, a Year/HourOfDay partial is not contiguous because the range
* field Day is not equal to the next field Year.
* Similarly, a DayOfWeek/DayOfMonth partial is not contiguous because
* the range Month is not equal to the next field Day.
*
* @param partial the partial to check
* @return true if the partial is contiguous
* @throws IllegalArgumentException if the partial is null
* @since 1.1
*/
public static final boolean isContiguous(ReadablePartial partial) {
if (partial == null) {
throw new IllegalArgumentException("Partial must not be null");
}
DurationFieldType lastType = null;
for (int i = 0; i < partial.size(); i++) {
DateTimeField loopField = partial.getField(i);
if (i > 0) {
if (loopField.getRangeDurationField() == null || loopField.getRangeDurationField().getType() != lastType) {
return false;
}
}
lastType = loopField.getDurationField().getType();
}
return true;
}
//-----------------------------------------------------------------------
/**
* Gets the {@link DateFormatSymbols} based on the given locale.
* <p>
* If JDK 6 or newer is being used, DateFormatSymbols.getInstance(locale) will
* be used in order to allow the use of locales defined as extensions.
* Otherwise, new DateFormatSymbols(locale) will be used.
* See JDK 6 {@link DateFormatSymbols} for further information.
*
* @param locale the {@link Locale} used to get the correct {@link DateFormatSymbols}
* @return the symbols
* @since 2.0
*/
public static final DateFormatSymbols getDateFormatSymbols(Locale locale) {
try {
Method method = DateFormatSymbols.class.getMethod("getInstance", new Class[] {Locale.class});
return (DateFormatSymbols) method.invoke(null, new Object[] {locale});
} catch (Exception ex) {
return new DateFormatSymbols(locale);
}
}
//-----------------------------------------------------------------------
/**
* Gets the default map of time zone names.
* <p>
* This can be changed by {@link #setDefaultTimeZoneNames}.
* <p>
* The default set of short time zone names is as follows:
* <ul>
* <li>UT - UTC
* <li>UTC - UTC
* <li>GMT - UTC
* <li>EST - America/New_York
* <li>EDT - America/New_York
* <li>CST - America/Chicago
* <li>CDT - America/Chicago
* <li>MST - America/Denver
* <li>MDT - America/Denver
* <li>PST - America/Los_Angeles
* <li>PDT - America/Los_Angeles
* </ul>
*
* @return the unmodifiable map of abbreviations to zones, not null
* @since 2.2
*/
public static final Map<String, DateTimeZone> getDefaultTimeZoneNames() {
Map<String, DateTimeZone> names = cZoneNames.get();
if (names == null) {
names = buildDefaultTimeZoneNames();
if (!cZoneNames.compareAndSet(null, names)) {
names = cZoneNames.get();
}
}
return names;
}
/**
* Sets the default map of time zone names.
* <p>
* The map is copied before storage.
*
* @param names the map of abbreviations to zones, not null
* @since 2.2
*/
public static final void setDefaultTimeZoneNames(Map<String, DateTimeZone> names) {
cZoneNames.set(Collections.unmodifiableMap(new HashMap<String, DateTimeZone>(names)));
}
private static Map<String, DateTimeZone> buildDefaultTimeZoneNames() {
// names from RFC-822 / JDK
// this is all very US-centric and dubious, but perhaps it will help some
Map<String, DateTimeZone> map = new LinkedHashMap<String, DateTimeZone>();
map.put("UT", DateTimeZone.UTC);
map.put("UTC", DateTimeZone.UTC);
map.put("GMT", DateTimeZone.UTC);
put(map, "EST", "America/New_York");
put(map, "EDT", "America/New_York");
put(map, "CST", "America/Chicago");
put(map, "CDT", "America/Chicago");
put(map, "MST", "America/Denver");
put(map, "MDT", "America/Denver");
put(map, "PST", "America/Los_Angeles");
put(map, "PDT", "America/Los_Angeles");
return Collections.unmodifiableMap(map);
}
private static void put(Map<String, DateTimeZone> map, String name, String id) {
try {
map.put(name, DateTimeZone.forID(id));
} catch (RuntimeException ex) {
// ignore
}
}
//-------------------------------------------------------------------------
/**
* Calculates the astronomical Julian Day for an instant.
* <p>
* The <a href="http://en.wikipedia.org/wiki/Julian_day">Julian day</a> is a well-known
* system of time measurement for scientific use by the astronomy community.
* It expresses the interval of time in days and fractions of a day since
* January 1, 4713 BC (Julian) Greenwich noon.
* <p>
* Each day starts at midday (not midnight) and time is expressed as a fraction.
* Thus the fraction 0.25 is 18:00. equal to one quarter of the day from midday to midday.
* <p>
* Note that this method has nothing to do with the day-of-year.
*
* @param epochMillis the epoch millis from 1970-01-01Z
* @return the astronomical Julian Day represented by the specified instant
* @since 2.2
*/
public static final double toJulianDay(long epochMillis) {
// useful links
// http://en.wikipedia.org/wiki/Julian_day#cite_note-13 - Wikipedia
// http://aa.usno.navy.mil/data/docs/JulianDate.php" - USNO
// http://users.zoominternet.net/~matto/Java/Julian%20Date%20Converter.htm - Julian Date Converter by Matt Oltersdorf
// http://ssd.jpl.nasa.gov/tc.cgi#top - CalTech
double epochDay = epochMillis / 86400000d;
return epochDay + 2440587.5d;
}
/**
* Calculates the astronomical Julian Day Number for an instant.
* <p>
* The {@link #toJulianDay(long)} method calculates the astronomical Julian Day
* with a fraction based on days starting at midday.
* This method calculates the variant where days start at midnight.
* JDN 0 is used for the date equivalent to Monday January 1, 4713 BC (Julian).
* Thus these days start 12 hours before those of the fractional Julian Day.
* <p>
* Note that this method has nothing to do with the day-of-year.
*
* @param epochMillis the epoch millis from 1970-01-01Z
* @return the astronomical Julian Day represented by the specified instant
* @since 2.2
*/
public static final long toJulianDayNumber(long epochMillis) {
return (long) Math.floor(toJulianDay(epochMillis) + 0.5d);
}
/**
* Creates a date-time from a Julian Day.
* <p>
* Returns the {@code DateTime} object equal to the specified Julian Day.
*
* @param julianDay the Julian Day
* @return the epoch millis from 1970-01-01Z
* @since 2.2
*/
public static final long fromJulianDay(double julianDay) {
double epochDay = julianDay - 2440587.5d;
return (long) (epochDay * 86400000d);
}
//-----------------------------------------------------------------------
/**
* A millisecond provider, allowing control of the system clock.
*
* @author Stephen Colebourne
* @since 2.0 (previously private)
*/
public static interface MillisProvider {
/**
* Gets the current time.
* <p>
* Implementations of this method must be thread-safe.
*
* @return the current time in milliseconds
*/
long getMillis();
}
/**
* System millis provider.
*/
static class SystemMillisProvider implements MillisProvider {
/**
* Gets the current time.
* @return the current time in millis
*/
public long getMillis() {
return System.currentTimeMillis();
}
}
/**
* Fixed millisecond provider.
*/
static class FixedMillisProvider implements MillisProvider {
/** The fixed millis value. */
private final long iMillis;
/**
* Constructor.
* @param fixedMillis the millis value
*/
FixedMillisProvider(long fixedMillis) {
iMillis = fixedMillis;
}
/**
* Gets the current time.
* @return the current time in millis
*/
public long getMillis() {
return iMillis;
}
}
/**
* Offset from system millis provider.
*/
static class OffsetMillisProvider implements MillisProvider {
/** The millis offset. */
private final long iMillis;
/**
* Constructor.
* @param offsetMillis the millis offset
*/
OffsetMillisProvider(long offsetMillis) {
iMillis = offsetMillis;
}
/**
* Gets the current time.
* @return the current time in millis
*/
public long getMillis() {
return System.currentTimeMillis() + iMillis;
}
}
}