Package org.elasticsearch.common.time
Class DateUtils
java.lang.Object
org.elasticsearch.common.time.DateUtils
public class DateUtils
extends java.lang.Object
-
Field Summary
Fields Modifier and Type Field Description static java.util.Map<java.lang.String,java.lang.String>
DEPRECATED_LONG_TIMEZONES
static java.util.Set<java.lang.String>
DEPRECATED_SHORT_TZ_IDS
static java.time.Instant
MAX_NANOSECOND_INSTANT
The maximum nanosecond resolution date we can properly handle. -
Constructor Summary
Constructors Constructor Description DateUtils()
-
Method Summary
Modifier and Type Method Description static java.time.Instant
clampToNanosRange(java.time.Instant instant)
Returns an instant that is with valid nanosecond resolution.static java.time.ZoneId
dateTimeZoneToZoneId(org.joda.time.DateTimeZone timeZone)
static java.time.ZonedDateTime
nowWithMillisResolution()
Returns the current UTC date-time with milliseconds precision.static java.time.ZonedDateTime
nowWithMillisResolution(java.time.Clock clock)
static java.time.ZoneId
of(java.lang.String zoneId)
static long
roundFloor(long utcMillis, long unitMillis)
Rounds the given utc milliseconds sicne the epoch down to the next unit millis Note: This does not check for correctness of the result, as this only works with units smaller or equal than a day In order to ensure the performance of this methods, there are no guards or checks in itstatic long
roundMonthOfYear(long utcMillis)
Round down to the beginning of the month of the year of the specified timestatic long
roundQuarterOfYear(long utcMillis)
Round down to the beginning of the quarter of the year of the specified timestatic long
roundWeekOfWeekYear(long utcMillis)
Round down to the beginning of the week based on week year of the specified timestatic long
roundYear(long utcMillis)
Round down to the beginning of the year of the specified timestatic java.time.Instant
toInstant(long nanoSecondsSinceEpoch)
convert a long value to a java time instant the long value resembles the nanoseconds since the epochstatic long
toLong(java.time.Instant instant)
convert a java time instant to a long value which is stored in lucene the long value resembles the nanoseconds since the epochstatic long
toMilliSeconds(long nanoSecondsSinceEpoch)
Convert a nanosecond timestamp in millisecondsstatic long
toNanoSeconds(long milliSecondsSinceEpoch)
Convert a nanosecond timestamp in millisecondsstatic org.joda.time.DateTimeZone
zoneIdToDateTimeZone(java.time.ZoneId zoneId)
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Field Details
-
DEPRECATED_SHORT_TZ_IDS
public static final java.util.Set<java.lang.String> DEPRECATED_SHORT_TZ_IDS -
DEPRECATED_LONG_TIMEZONES
public static final java.util.Map<java.lang.String,java.lang.String> DEPRECATED_LONG_TIMEZONES -
MAX_NANOSECOND_INSTANT
public static final java.time.Instant MAX_NANOSECOND_INSTANTThe maximum nanosecond resolution date we can properly handle.
-
-
Constructor Details
-
DateUtils
public DateUtils()
-
-
Method Details
-
zoneIdToDateTimeZone
public static org.joda.time.DateTimeZone zoneIdToDateTimeZone(java.time.ZoneId zoneId) -
dateTimeZoneToZoneId
public static java.time.ZoneId dateTimeZoneToZoneId(org.joda.time.DateTimeZone timeZone) -
of
public static java.time.ZoneId of(java.lang.String zoneId) -
toLong
public static long toLong(java.time.Instant instant)convert a java time instant to a long value which is stored in lucene the long value resembles the nanoseconds since the epoch- Parameters:
instant
- the instant to convert- Returns:
- the nano seconds and seconds as a single long
-
clampToNanosRange
public static java.time.Instant clampToNanosRange(java.time.Instant instant)Returns an instant that is with valid nanosecond resolution. If the parameter is before the valid nanosecond range then this returns the minimum Instant valid for nanosecond resultion. If the parameter is after the valid nanosecond range then this returns the maximum Instant valid for nanosecond resolution.Useful for checking if all values for the field are within some range, even if the range's endpoints are not valid nanosecond resolution.
-
toInstant
public static java.time.Instant toInstant(long nanoSecondsSinceEpoch)convert a long value to a java time instant the long value resembles the nanoseconds since the epoch- Parameters:
nanoSecondsSinceEpoch
- the nanoseconds since the epoch- Returns:
- the instant resembling the specified date
-
toNanoSeconds
public static long toNanoSeconds(long milliSecondsSinceEpoch)Convert a nanosecond timestamp in milliseconds- Parameters:
milliSecondsSinceEpoch
- the millisecond since the epoch- Returns:
- the nanoseconds since the epoch
-
toMilliSeconds
public static long toMilliSeconds(long nanoSecondsSinceEpoch)Convert a nanosecond timestamp in milliseconds- Parameters:
nanoSecondsSinceEpoch
- the nanoseconds since the epoch- Returns:
- the milliseconds since the epoch
-
roundFloor
public static long roundFloor(long utcMillis, long unitMillis)Rounds the given utc milliseconds sicne the epoch down to the next unit millis Note: This does not check for correctness of the result, as this only works with units smaller or equal than a day In order to ensure the performance of this methods, there are no guards or checks in it- Parameters:
utcMillis
- the milliseconds since the epochunitMillis
- the unit to round to- Returns:
- the rounded milliseconds since the epoch
-
roundQuarterOfYear
public static long roundQuarterOfYear(long utcMillis)Round down to the beginning of the quarter of the year of the specified time- Parameters:
utcMillis
- the milliseconds since the epoch- Returns:
- The milliseconds since the epoch rounded down to the quarter of the year
-
roundMonthOfYear
public static long roundMonthOfYear(long utcMillis)Round down to the beginning of the month of the year of the specified time- Parameters:
utcMillis
- the milliseconds since the epoch- Returns:
- The milliseconds since the epoch rounded down to the month of the year
-
roundYear
public static long roundYear(long utcMillis)Round down to the beginning of the year of the specified time- Parameters:
utcMillis
- the milliseconds since the epoch- Returns:
- The milliseconds since the epoch rounded down to the beginning of the year
-
roundWeekOfWeekYear
public static long roundWeekOfWeekYear(long utcMillis)Round down to the beginning of the week based on week year of the specified time- Parameters:
utcMillis
- the milliseconds since the epoch- Returns:
- The milliseconds since the epoch rounded down to the beginning of the week based on week year
-
nowWithMillisResolution
public static java.time.ZonedDateTime nowWithMillisResolution()Returns the current UTC date-time with milliseconds precision. In Java 9+ (as opposed to Java 8) theClock
implementation uses system's best clock implementation (which could mean that the precision of the clock can be milliseconds, microseconds or nanoseconds), whereas in Java 8System.currentTimeMillis()
is always used. To account for these differences, this method defines a newClock
which will offer a value forZonedDateTime.now()
set to always have milliseconds precision.- Returns:
ZonedDateTime
instance for the current date-time with milliseconds precision in UTC
-
nowWithMillisResolution
public static java.time.ZonedDateTime nowWithMillisResolution(java.time.Clock clock)
-