public class DateUtils extends java.lang.Object
Modifier and Type | Class and Description |
---|---|
static class |
DateUtils.CurrentDateFactory
A factory returning the current date.
|
static class |
DateUtils.FixedDateFactory
A factory returning a fixed date.
|
Modifier and Type | Field and Description |
---|---|
static long |
MILLIS_PER_DAY
The number of milliseconds per day
|
static long |
MILLIS_PER_HOUR
The number of milliseconds per hour
|
static long |
MILLIS_PER_MINUTE
The number of milliseconds per minute
|
static long |
MILLIS_PER_SECOND
The number of milliseconds per second
|
static long |
NANOS_PER_SECOND
The number of nanoseconds per second
|
Constructor and Description |
---|
DateUtils() |
Modifier and Type | Method and Description |
---|---|
static java.util.Date |
addDaysToDate(java.util.Date date,
int days)
Create a new date and add
days to it. |
static java.util.Date |
createDate(int day,
int month,
int year)
Create a date.
|
static java.time.ZonedDateTime |
createZonedDateTimeForTimestamp(long timestamp)
Creates a new
ZonedDateTime for the given timestamp. |
static long |
daysToMilliseconds(int days)
Converts the given number of days into milliseconds.
|
static long |
diff(java.util.Date earlier,
java.util.Date later,
java.util.concurrent.TimeUnit timeUnit)
Returns the difference between two dates in the given time unit.
|
static long |
diffDays(java.util.Date earlier,
java.util.Date later)
Returns the difference between two dates in days.
|
static java.lang.String |
formatDate(long timestamp,
java.lang.String pattern)
Takes a
timestamp in milliseconds and returns formatted string
representation of date using given pattern. |
static java.lang.String |
formatDurationHumanReadable(java.time.Duration duration)
Formats a duration in a human-readable way (e.g.
|
static java.lang.String |
formatTimestampToDate(long timestamp)
Formats the given timestamp as date YYYY-MM-DD
|
static java.util.Date |
getDateBefore(int days)
Retrieves the date which is exactly the given number of days before now.
|
static java.util.List<java.util.Date> |
getDatePointsInTimespan(java.util.Date startDate,
java.util.Date endDate,
int daysBetweenDatePoints,
boolean includeEndDate)
Get an ordered list of dates that includes
startDate , date
points in between start and end date and the endDate if
includeEndDate is true. |
static java.util.Date |
getEarliest(java.util.Collection<java.util.Date> dates)
Returns the earliest date in a collection of dates
|
static java.util.Date |
getLatest(java.util.Collection<java.util.Date> dates)
Returns the latest date in a collection of dates
|
static java.util.Date |
getNow()
Returns the current time as a
Date object. |
static java.util.Date |
getNowWithoutTime()
getNow() truncated to the begin of day. |
static java.lang.Long |
getTimeOfDate(java.util.Date date)
Get the timestamp of a date.
|
static java.lang.String |
getUiFormattedDateString(long milliseconds)
Takes a
timestamp in milliseconds, creates a date based on this
timestamp and returns the UI formatted String representation of this date. |
static long |
hoursToMilliseconds(int hours)
Converts the given number of hours into milliseconds.
|
static java.util.Date |
incrementByOneDay(java.util.Date date)
Returns a new Date that is one day later than the given date.
|
static java.util.Date |
max(java.util.Date d1,
java.util.Date d2)
Returns the later of two dates or null, if one of the dates is null.
|
static java.util.Date |
min(java.util.Date d1,
java.util.Date d2)
Returns the earlier of two dates, or null, if one of the dates is null
|
static long |
minutesToMilliseconds(int minutes)
Converts the given number of minutes into milliseconds.
|
static double |
nanosToSeconds(long nanos)
Converts nano seconds to seconds.
|
static java.util.Date |
subtractDaysFromDate(java.util.Date date,
int days)
Create a new date and subtracts
days from it. |
static IFactory<java.util.Date,NeverThrownRuntimeException> |
testcode_getNowFactory()
Returns the factory that affects the notion of "now" in
getNow() . |
static void |
testcode_resetNowFactory()
Reset the now factory to the default one.
|
static void |
testcode_setFixedDate(java.util.Date date)
Sets the given fixed date for "now" in
getNow() . |
static void |
testcode_setNowFactory(IFactory<java.util.Date,NeverThrownRuntimeException> nowFactory)
Sets the factory that affects the notion of "now" in
getNow() . |
static java.util.Date |
truncateToBeginOfDay(java.util.Date date)
Returns a normalized version of the given date.
|
public static final long MILLIS_PER_SECOND
public static final long NANOS_PER_SECOND
public static final long MILLIS_PER_MINUTE
public static final long MILLIS_PER_HOUR
public static final long MILLIS_PER_DAY
public DateUtils()
public static java.util.Date getLatest(java.util.Collection<java.util.Date> dates)
public static java.util.Date getEarliest(java.util.Collection<java.util.Date> dates)
public static java.util.Date min(java.util.Date d1, java.util.Date d2)
public static java.util.Date max(java.util.Date d1, java.util.Date d2)
public static java.util.Date getDateBefore(int days)
public static java.util.Date getNow()
Date
object. This is preferred to
directly calling the Date.Date()
constructor, as this method provides
a central entry point that allows the notion of time to be tweaked (e.g. for
testing, when we want a certain date to be returned).
The behavior of this method can be affected either by calling the
testcode_setNowFactory(IFactory)
method, or by providing a fixed
date in format "yyyyMMddHHmmss" as the system property
"org.conqat.lib.commons.date.now".public static java.util.Date getNowWithoutTime()
getNow()
truncated to the begin of day.public static IFactory<java.util.Date,NeverThrownRuntimeException> testcode_getNowFactory()
getNow()
.
This should be only called from test code!public static void testcode_setNowFactory(IFactory<java.util.Date,NeverThrownRuntimeException> nowFactory)
getNow()
. This
should be only called from test code!public static void testcode_setFixedDate(java.util.Date date)
getNow()
. This should be only
called from test code! It is absolutely necessary to call the reset method
afterwards in order to prevent test interactions over the statically shared
nowFactory
.public static void testcode_resetNowFactory()
public static java.util.Date incrementByOneDay(java.util.Date date)
public static java.util.Date truncateToBeginOfDay(java.util.Date date)
public static java.lang.String formatTimestampToDate(long timestamp)
public static long daysToMilliseconds(int days)
public static long hoursToMilliseconds(int hours)
public static long minutesToMilliseconds(int minutes)
public static long diff(java.util.Date earlier, java.util.Date later, java.util.concurrent.TimeUnit timeUnit)
public static long diffDays(java.util.Date earlier, java.util.Date later)
public static java.util.Date addDaysToDate(java.util.Date date, int days)
days
to it.public static java.util.Date subtractDaysFromDate(java.util.Date date, int days)
days
from it.public static java.util.List<java.util.Date> getDatePointsInTimespan(java.util.Date startDate, java.util.Date endDate, int daysBetweenDatePoints, boolean includeEndDate)
startDate
, date
points in between start and end date and the endDate
if
includeEndDate
is true.public static java.lang.Long getTimeOfDate(java.util.Date date)
date
is null.public static java.util.Date createDate(int day, int month, int year)
month
- zero-based indexpublic static java.lang.String getUiFormattedDateString(long milliseconds)
timestamp
in milliseconds, creates a date based on this
timestamp and returns the UI formatted String representation of this date.
This used a fixed English locale.public static java.lang.String formatDate(long timestamp, java.lang.String pattern)
timestamp
in milliseconds and returns formatted string
representation of date using given pattern. The pattern format is the one
used in Java DateTimeFormatter
. Uses English locale.public static java.lang.String formatDurationHumanReadable(java.time.Duration duration)
public static double nanosToSeconds(long nanos)
public static java.time.ZonedDateTime createZonedDateTimeForTimestamp(long timestamp)
ZonedDateTime
for the given timestamp.