public class DateTimeUtils extends Object
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.
Modifier and Type | Class and Description |
---|---|
static interface |
DateTimeUtils.MillisProvider
A millisecond provider, allowing control of the system clock.
|
Modifier and Type | Field and Description |
---|---|
static DateTimeUtils.MillisProvider |
SYSTEM_MILLIS_PROVIDER
The singleton instance of the system millisecond provider.
|
Modifier | Constructor and Description |
---|---|
protected |
DateTimeUtils()
Restrictive constructor
|
Modifier and Type | Method and Description |
---|---|
static long |
currentTimeMillis()
Gets the current time in milliseconds.
|
static long |
fromJulianDay(double julianDay)
Creates a date-time from a Julian Day.
|
static Chronology |
getChronology(Chronology chrono)
Gets the chronology handling null.
|
static DateFormatSymbols |
getDateFormatSymbols(Locale locale)
Gets the
DateFormatSymbols based on the given locale. |
static Map<String,DateTimeZone> |
getDefaultTimeZoneNames()
Gets the default map of time zone names.
|
static long |
getDurationMillis(ReadableDuration duration)
Gets the millisecond duration from the specified duration object handling null.
|
static Chronology |
getInstantChronology(ReadableInstant instant)
Gets the chronology from the specified instant object handling null.
|
static long |
getInstantMillis(ReadableInstant instant)
Gets the millisecond instant from the specified instant object handling null.
|
static Chronology |
getIntervalChronology(ReadableInstant start,
ReadableInstant end)
Gets the chronology from the specified instant based interval handling null.
|
static Chronology |
getIntervalChronology(ReadableInterval interval)
Gets the chronology from the specified interval object handling null.
|
static PeriodType |
getPeriodType(PeriodType type)
Gets the period type handling null.
|
static ReadableInterval |
getReadableInterval(ReadableInterval interval)
Gets the interval handling null.
|
static DateTimeZone |
getZone(DateTimeZone zone)
Gets the zone handling null.
|
static boolean |
isContiguous(ReadablePartial partial)
Checks whether the partial is contiguous.
|
static void |
setCurrentMillisFixed(long fixedMillis)
Sets the current time to return a fixed millisecond time.
|
static void |
setCurrentMillisOffset(long offsetMillis)
Sets the current time to return the system time plus an offset.
|
static void |
setCurrentMillisProvider(DateTimeUtils.MillisProvider millisProvider)
Sets the provider of the current time to class specified.
|
static void |
setCurrentMillisSystem()
Resets the current time to return the system time.
|
static void |
setDefaultTimeZoneNames(Map<String,DateTimeZone> names)
Sets the default map of time zone names.
|
static double |
toJulianDay(long epochMillis)
Calculates the astronomical Julian Day for an instant.
|
static long |
toJulianDayNumber(long epochMillis)
Calculates the astronomical Julian Day Number for an instant.
|
public static final DateTimeUtils.MillisProvider SYSTEM_MILLIS_PROVIDER
public static final long currentTimeMillis()
By default this returns System.currentTimeMillis()
.
This may be changed using other methods in this class.
public static final void setCurrentMillisSystem() throws SecurityException
This method changes the behaviour of currentTimeMillis()
.
Whenever the current time is queried, System.currentTimeMillis()
is used.
SecurityException
- if the application does not have sufficient security rightspublic static final void setCurrentMillisFixed(long fixedMillis) throws SecurityException
This method changes the behaviour of currentTimeMillis()
.
Whenever the current time is queried, the same millisecond time will be returned.
fixedMillis
- the fixed millisecond time to useSecurityException
- if the application does not have sufficient security rightspublic static final void setCurrentMillisOffset(long offsetMillis) throws SecurityException
This method changes the behaviour of currentTimeMillis()
.
Whenever the current time is queried, System.currentTimeMillis()
is used
and then offset by adding the millisecond value specified here.
offsetMillis
- the fixed millisecond time to useSecurityException
- if the application does not have sufficient security rightspublic static final void setCurrentMillisProvider(DateTimeUtils.MillisProvider millisProvider) throws SecurityException
This method changes the behaviour of currentTimeMillis()
.
Whenever the current time is queried, the specified class will be called.
millisProvider
- the provider of the current time to use, not nullSecurityException
- if the application does not have sufficient security rightspublic static final long getInstantMillis(ReadableInstant instant)
If the instant object is null
, the currentTimeMillis()
will be returned. Otherwise, the millis from the object are returned.
instant
- the instant to examine, null means nowpublic static final Chronology getInstantChronology(ReadableInstant instant)
If the instant object is null
, or the instant's chronology is
null
, ISOChronology.getInstance()
will be returned.
Otherwise, the chronology from the object is returned.
instant
- the instant to examine, null means ISO in the default zonepublic static final Chronology getIntervalChronology(ReadableInstant start, ReadableInstant end)
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 ISOChronology.getInstance()
will be returned.
start
- the instant to examine and use as the primary source of the chronologyend
- the instant to examine and use as the secondary source of the chronologypublic static final Chronology getIntervalChronology(ReadableInterval interval)
If the interval object is null
, or the interval's chronology is
null
, ISOChronology.getInstance()
will be returned.
Otherwise, the chronology from the object is returned.
interval
- the interval to examine, null means ISO in the default zonepublic static final ReadableInterval getReadableInterval(ReadableInterval interval)
If the interval is null
, an interval representing now
to now in the ISOChronology
will be returned. Otherwise, the interval specified is returned.
interval
- the interval to use, null means now to nowpublic static final Chronology getChronology(Chronology chrono)
If the chronology is null
, ISOChronology.getInstance()
will be returned. Otherwise, the chronology is returned.
chrono
- the chronology to use, null means ISO in the default zonepublic static final DateTimeZone getZone(DateTimeZone zone)
If the zone is null
, DateTimeZone.getDefault()
will be returned. Otherwise, the zone specified is returned.
zone
- the time zone to use, null means the default zonepublic static final PeriodType getPeriodType(PeriodType type)
If the zone is null
, PeriodType.standard()
will be returned. Otherwise, the type specified is returned.
type
- the time zone to use, null means the standard typepublic static final long getDurationMillis(ReadableDuration duration)
If the duration object is null
, zero will be returned.
Otherwise, the millis from the object are returned.
duration
- the duration to examine, null means zeropublic static final boolean isContiguous(ReadablePartial partial)
A partial is contiguous if one field starts where another ends.
For example LocalDate
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).
Similarly, LocalTime
is contiguous, as it consists of
MillisOfSecond, SecondOfMinute, MinuteOfHour and HourOfDay (note how
the names of each field 'join up').
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.
partial
- the partial to checkIllegalArgumentException
- if the partial is nullpublic static final DateFormatSymbols getDateFormatSymbols(Locale locale)
DateFormatSymbols
based on the given locale.
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 DateFormatSymbols
for further information.
locale
- the Locale
used to get the correct DateFormatSymbols
public static final Map<String,DateTimeZone> getDefaultTimeZoneNames()
This can be changed by setDefaultTimeZoneNames(java.util.Map<java.lang.String, org.joda.time.DateTimeZone>)
.
The default set of short time zone names is as follows:
public static final void setDefaultTimeZoneNames(Map<String,DateTimeZone> names)
The map is copied before storage.
names
- the map of abbreviations to zones, not nullpublic static final double toJulianDay(long epochMillis)
The Julian day 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.
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.
Note that this method has nothing to do with the day-of-year.
epochMillis
- the epoch millis from 1970-01-01Zpublic static final long toJulianDayNumber(long epochMillis)
The 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.
Note that this method has nothing to do with the day-of-year.
epochMillis
- the epoch millis from 1970-01-01Zpublic static final long fromJulianDay(double julianDay)
Returns the DateTime
object equal to the specified Julian Day.
julianDay
- the Julian DayCopyright © 2002–2024 Joda.org. All rights reserved.