public final class DateTimeToolsNG
extends java.lang.Object
Date, Calendar,
and related objects, with a focus on time zone handling.
Warning:
Some methods may rely on the client's time zone settings. Ensure consistency
between the client's time zone and the data's time zone to avoid unexpected results.
| Modifier and Type | Method and Description |
|---|---|
static int |
countDays(java.util.Date from,
java.util.Date till)
Counts the conceptual days between two dates, including the start and end dates, ignoring time.
|
static int |
countDays(TimespanI ts)
Counts the conceptual days covered by the given timespan, ignoring time.
|
static long |
coveredTimespanInFullMinutes(java.util.Date from,
java.util.Date till)
Returns the timespan between two dates in full minutes, rounded down.
|
static long |
coveredTimespanInMillis(java.util.Calendar from,
java.util.Calendar till)
Returns the timespan between two calendars in milliseconds.
|
static long |
coveredTimespanInMillis(java.util.Date from,
java.util.Date till)
Returns the timespan between two dates in milliseconds.
|
static long |
coveredTimespanToDays(java.util.Date from,
java.util.Date till)
Returns the timespan between two dates in abstract days, rounded up.
|
static long |
coveredTimespanToDays(java.util.Date from,
java.util.Date till,
boolean roundUp)
Returns the timespan between two dates in abstract days.
|
static long |
coveredTimespanToDays(java.time.Duration duration)
Returns the timespan between two dates in abstract days, rounded up.
|
static long |
coveredTimespanToDays(java.time.Duration duration,
boolean roundUp)
Returns the timespan between two dates in abstract days.
|
static long |
coveredTimespanToDays(TimespanI ts)
Returns the timespan between the start and end dates in days, rounded up.
|
static long |
coveredTimespanToDays(TimespanI ts,
boolean roundUp)
Returns the timespan between the start and end dates in days.
|
static java.time.Duration |
createDuration(java.util.Calendar from,
java.util.Calendar till)
Calculates the duration between two
Calendar objects. |
static java.time.Duration |
createDuration(java.util.Date from,
java.util.Date till)
Calculates the duration between two
Date objects. |
static java.time.LocalDate |
createLocalDate(java.util.Calendar calendar)
Converts a
Calendar object to a LocalDate. |
static java.time.LocalDate |
createLocalDate(java.util.Date date)
Converts a
Date object to a LocalDate. |
static java.time.LocalDateTime |
createLocalDateTime(java.util.Calendar calendar)
Converts a
Calendar object to a LocalDateTime. |
static java.time.Period |
createPeriod(java.util.Calendar from,
java.util.Calendar till)
Calculates the period between two
Calendar objects. |
static java.time.Period |
createPeriod(java.util.Date from,
java.util.Date till)
Calculates the period between two
Date objects. |
static java.lang.String |
formatPeriod(java.time.Period period,
boolean showDays) |
static java.lang.String |
formatPeriod(java.time.Period period,
boolean showDays,
boolean hideZeroes) |
static int |
getNumberOfMonths(java.util.Calendar from,
java.util.Calendar till)
Returns the number of conceptual months between two calendars, rounded up.
|
static int |
getNumberOfMonths(java.util.Date from,
java.util.Date till)
Returns the number of conceptual months between two dates, rounded up.
|
static int |
getNumberOfMonths(java.time.Period period)
Returns the number of conceptual months covered by a period, rounded up.
|
static java.time.ZoneId |
getZoneBerlin()
Returns the
ZoneId for Europe/Berlin. |
static java.time.ZoneId |
getZoneLuxembourg()
Returns the
ZoneId for Europe/Luxembourg. |
static java.util.Date |
toDate(java.time.LocalDateTime localDateTime,
java.time.ZoneId zoneId)
Converts a
LocalDateTime to a Date in the specified time zone. |
static java.util.Date |
toDate(java.time.LocalDate localDate,
java.time.ZoneId zoneId)
Converts a
LocalDate to a Date at the start of the day (midnight)
in the specified time zone. |
static java.util.Date |
toDate(java.time.ZonedDateTime zonedDateTime)
Converts a
ZonedDateTime to a Date. |
static java.util.Date |
toDateOfSystem(java.time.LocalDate localDate)
Converts a
LocalDate to a Date at the start of the day (midnight)
in the system's default time zone. |
static java.util.Date |
toDateOfSystem(java.time.LocalDateTime localDateTime)
Converts a
LocalDateTime to a Date in the system's default time zone. |
static java.time.LocalDateTime |
toLocalDateTime(java.util.Date date,
java.time.ZoneId zoneId)
Converts a
Date object to a LocalDateTime at the given zone. |
static java.time.LocalDateTime |
toLocalDateTimeOfSystem(java.util.Date date)
Converts a
Date to a LocalDateTime at the default time zone of the system. |
static java.time.ZoneOffset |
toZoneOffset(java.time.ZoneId zoneId,
java.util.Date atDate)
Gets the
ZoneOffset that was valid in the given ZoneId at the provided Date. |
static java.time.ZoneOffset |
toZoneOffset(java.time.ZoneId zoneId,
java.time.Instant atInstant)
Gets the
ZoneOffset that was valid in the given ZoneId at the provided Instant. |
static java.time.ZoneOffset |
toZoneOffset(java.time.ZoneId zoneId,
java.time.LocalDateTime atDateTime)
Gets the
ZoneOffset that was valid in the given ZoneId at the
provided LocalDateTime. |
public static java.time.ZoneId getZoneBerlin()
ZoneId for Europe/Berlin.ZoneId for Europe/Berlinpublic static java.time.ZoneId getZoneLuxembourg()
ZoneId for Europe/Luxembourg.ZoneId for Europe/Luxembourgpublic static java.time.ZoneOffset toZoneOffset(java.time.ZoneId zoneId,
java.time.LocalDateTime atDateTime)
ZoneOffset that was valid in the given ZoneId at the
provided LocalDateTime.
It is generally recommended to use toZoneOffset(ZoneId, Instant)
instead, as the lookup with an Instant is guaranteed to be unique.
zoneId - the ZoneId to checkatDateTime - the LocalDateTime at which the offset is requestedZoneOffset valid at the given LocalDateTime in the given zone,
or null if either parameter is null.for rules regarding the interpretation
of non-unique or non-existing {@code LocalDateTime}s (e.g., during DST transitions)public static java.time.ZoneOffset toZoneOffset(java.time.ZoneId zoneId,
java.time.Instant atInstant)
ZoneOffset that was valid in the given ZoneId at the provided Instant.zoneId - the ZoneId to checkatInstant - the Instant to check (e.g., via Date.toInstant())ZoneOffset that was in effect at the given Instant in the given zone,
or null if either parameter is null.public static java.time.ZoneOffset toZoneOffset(java.time.ZoneId zoneId,
java.util.Date atDate)
ZoneOffset that was valid in the given ZoneId at the provided Date.
This method converts the Date to an Instant and delegates to
toZoneOffset(ZoneId, Instant).
zoneId - the ZoneId to checkatDate - the Date to checkZoneOffset that was in effect at the given Date in the given zone,
or null if either parameter is null.toZoneOffset(ZoneId, Instant)public static java.time.LocalDateTime toLocalDateTime(java.util.Date date,
java.time.ZoneId zoneId)
Date object to a LocalDateTime at the given zone.date - the date to convertzoneId - the ZoneId at which the date will be interpretedLocalDateTime, or null if either parameter is null.public static java.time.LocalDateTime toLocalDateTimeOfSystem(java.util.Date date)
Date to a LocalDateTime at the default time zone of the system.date - the date to convertLocalDateTime, or null if the date is null.public static java.util.Date toDateOfSystem(java.time.LocalDate localDate)
LocalDate to a Date at the start of the day (midnight)
in the system's default time zone.
This method is a temporary workaround for persisting LocalDate values as Date objects
in the database. It may be removed once the backend is updated to handle java.time types
natively.
localDate - the LocalDate to convert, may be nullDate, or null if the input LocalDate is nullpublic static java.util.Date toDateOfSystem(java.time.LocalDateTime localDateTime)
LocalDateTime to a Date in the system's default time zone.
This method is a temporary workaround for persisting LocalDateTime values as Date objects
in the database. It may be removed once the backend is updated to handle java.time types
natively.
localDateTime - the LocalDateTime to convert, may be nullDate, or null if the input LocalDateTime is nullpublic static java.util.Date toDate(java.time.LocalDate localDate,
java.time.ZoneId zoneId)
LocalDate to a Date at the start of the day (midnight)
in the specified time zone.
This method is a temporary workaround for persisting LocalDate values as Date objects
in the database. It may be removed once the backend is updated to handle java.time types
natively.
localDate - the LocalDate to convert, may be nullzoneId - the ZoneId to use for the conversionDate, or null if the input LocalDate is nullpublic static java.util.Date toDate(java.time.LocalDateTime localDateTime,
java.time.ZoneId zoneId)
LocalDateTime to a Date in the specified time zone.
This method is a temporary workaround for persisting LocalDateTime values as Date objects
in the database. It may be removed once the backend is updated to handle java.time types
natively.
localDateTime - the LocalDateTime to convert, may be nullzoneId - the ZoneId to use for the conversionDate, or null if the input LocalDateTime is nullpublic static java.util.Date toDate(java.time.ZonedDateTime zonedDateTime)
ZonedDateTime to a Date.
This method is a temporary workaround for persisting ZonedDateTime values as Date objects
in the database. It may be removed once the backend is updated to handle java.time types
natively.
zonedDateTime - the ZonedDateTime to convert, may be nullDate, or null if the input ZonedDateTime is nullpublic static java.time.LocalDateTime createLocalDateTime(java.util.Calendar calendar)
Calendar object to a LocalDateTime.calendar - the calendar to convertLocalDateTime, or null if the input calendar is nullpublic static java.time.Duration createDuration(java.util.Date from,
java.util.Date till)
Date objects.from - the start datetill - the end datejava.lang.IllegalArgumentException - if either date is nullpublic static java.time.Duration createDuration(java.util.Calendar from,
java.util.Calendar till)
Calendar objects.from - the start calendartill - the end calendarjava.lang.IllegalArgumentException - if either calendar is nullpublic static java.time.LocalDate createLocalDate(java.util.Date date)
Date object to a LocalDate.date - the date to convertLocalDate, or null if the input date is nullpublic static java.time.LocalDate createLocalDate(java.util.Calendar calendar)
Calendar object to a LocalDate.calendar - the calendar to convertLocalDate, or null if the input calendar is nullpublic static java.time.Period createPeriod(java.util.Date from,
java.util.Date till)
Date objects.from - the start datetill - the end datejava.lang.IllegalArgumentException - if either date is nullpublic static java.time.Period createPeriod(java.util.Calendar from,
java.util.Calendar till)
Calendar objects.from - the start calendartill - the end calendarjava.lang.IllegalArgumentException - if either calendar is nullpublic static long coveredTimespanToDays(TimespanI ts)
ts - the timespan to count the days injava.lang.IllegalArgumentException - if the timespan is nullpublic static long coveredTimespanToDays(TimespanI ts, boolean roundUp)
ts - the timespan to count the days inroundUp - if true, the number of days is rounded upjava.lang.IllegalArgumentException - if the timespan is nullpublic static long coveredTimespanToDays(java.util.Date from,
java.util.Date till)
from - the start datetill - the end datejava.lang.IllegalArgumentException - if either date is nullpublic static long coveredTimespanToDays(java.util.Date from,
java.util.Date till,
boolean roundUp)
from - the start datetill - the end dateroundUp - if true, the number of days is rounded upjava.lang.IllegalArgumentException - if either date is nullpublic static long coveredTimespanToDays(java.time.Duration duration)
duration - the duration to convert to dayspublic static long coveredTimespanToDays(java.time.Duration duration,
boolean roundUp)
duration - the duration to convert to daysroundUp - if true, the number of days is rounded up if the remainder is at least one hour.public static int countDays(TimespanI ts)
ts - the timespan to count the days injava.lang.IllegalArgumentException - if the timespan is nullpublic static int countDays(java.util.Date from,
java.util.Date till)
from - the start datetill - the end datejava.lang.IllegalArgumentException - if either date is nullpublic static long coveredTimespanInFullMinutes(java.util.Date from,
java.util.Date till)
from - the start datetill - the end datejava.lang.IllegalArgumentException - if either date is nullpublic static long coveredTimespanInMillis(java.util.Date from,
java.util.Date till)
from - the start datetill - the end datejava.lang.IllegalArgumentException - if either date is nullpublic static long coveredTimespanInMillis(java.util.Calendar from,
java.util.Calendar till)
from - the start calendartill - the end calendarjava.lang.IllegalArgumentException - if either calendar is nullpublic static int getNumberOfMonths(java.util.Date from,
java.util.Date till)
from - the start datetill - the end datejava.lang.IllegalArgumentException - if either date is nullpublic static int getNumberOfMonths(java.util.Calendar from,
java.util.Calendar till)
from - the start calendartill - the end calendarjava.lang.IllegalArgumentException - if either calendar is nullpublic static int getNumberOfMonths(java.time.Period period)
period - the period to inspectjava.lang.IllegalArgumentException - if the period is nullpublic static java.lang.String formatPeriod(java.time.Period period,
boolean showDays)
public static java.lang.String formatPeriod(java.time.Period period,
boolean showDays,
boolean hideZeroes)
Copyright © 2000-2025 OAshi S.à r.l. All Rights Reserved.