Package org.ametys.core.util
Class DateUtils
java.lang.Object
org.ametys.core.util.DateUtils
Helper for converting dates from the old (
Date
) to the new (java.time
) JDK
Special thanks to http://stackoverflow.com/questions/21242110/convert-java-util-date-to-java-time-localdate#answer-27378709
which inspired this code
See also http://stackoverflow.com/questions/19431234/converting-between-java-time-localdatetime-and-java-util-date-
Field Summary
Modifier and TypeFieldDescriptionstatic final String
The ISO date-time pattern that formats or parses a date-time with an offset -
Method Summary
Modifier and TypeMethodDescriptionstatic Calendar
asCalendar
(LocalDate localDate) static Calendar
asCalendar
(ZonedDateTime zonedDateTime) Converts thisZonedDateTime
object to aCalendar
, setting the time zone to UTC.static Date
static Date
static Date
asDate
(ZonedDateTime zonedDateTime) Converts thisZonedDateTime
object to aDate
.static Instant
static LocalDate
asLocalDate
(Calendar calendar) static LocalDate
asLocalDate
(Date date) static LocalDate
asLocalDate
(Date date, ZoneId zone) static ZonedDateTime
asZonedDateTime
(long epochMilli) Converts this epoch time to aZonedDateTime
.static ZonedDateTime
asZonedDateTime
(long epochMilli, ZoneId zone) Converts this epoch time to aZonedDateTime
.static ZonedDateTime
asZonedDateTime
(LocalDate localDate, ZoneId zone) Converts aLocalDate
to aZonedDateTime
static ZonedDateTime
asZonedDateTime
(Calendar calendar) Converts thisCalendar
object to aZonedDateTime
.static ZonedDateTime
asZonedDateTime
(Date date) Converts thisDate
object to aZonedDateTime
, at UTC.static ZonedDateTime
asZonedDateTime
(Date date, ZoneId zone) Converts thisDate
object to aZonedDateTime
.static String
dateToString
(Date value) static String
epochMilliToString
(long epochMilli) Converts this epoch time to aString
using the ISO date formatterstatic String
formatDuration
(long duration) Format a duration for logsstatic String
formatDuration
(Duration duration) Format a duration for logsstatic DateTimeFormatter
Get the ISO date-time formatter that formats or parses a date-time with an offset, such as '2011-12-03T10:15:30.000+01:00'.static String
localDateToString
(LocalDate localDate) static Date
Parses a String into aDate
, using ISO 8601 format.static LocalDate
parseLocalDate
(String localDateAsString) Parses a String into aLocalDate
, using ISO date formatter.static ZonedDateTime
parseZonedDateTime
(String zonedDateTimeAsString) Parses a String into aZonedDateTime
, using ISO date formatter.static String
zonedDateTimeToString
(ZonedDateTime zonedDateTime) static String
zonedDateTimeToString
(ZonedDateTime zonedDateTime, ZoneId zoneId, String pattern) Converts aZonedDateTime
object toString
using the given pattern for formatting, in the given zone, so as to format the instant in another zone
For instance, if provided pattern is 'uuuu-MM-dd'T'HH:mm:ss.SSSXXX': for UTC zone, the zone date time corresponding to '2011-12-03T11:15:30+01:00' will be formatted as '2011-12-03T10:15:30Z' for +02:00 zone, the zone date time corresponding to '2011-12-03T11:15:30+01:00' will be formatted as '2011-12-03T12:15:30+02:00'
-
Field Details
-
ISO_OFFSET_DATE_TIME_PATTERN
The ISO date-time pattern that formats or parses a date-time with an offset- See Also:
-
-
Method Details
-
asInstant
- Parameters:
date
- The date object- Returns:
- an instant representing the same point on the time-line as this
Date
object
-
asZonedDateTime
Converts thisDate
object to aZonedDateTime
, at UTC.- Parameters:
date
- The date object- Returns:
- the
ZonedDateTime
formed from thisDate
-
asZonedDateTime
Converts thisDate
object to aZonedDateTime
.- Parameters:
date
- The date objectzone
- The zone. Ifnull
, UTC is used- Returns:
- the
ZonedDateTime
formed from thisDate
-
asZonedDateTime
Converts thisCalendar
object to aZonedDateTime
.- Parameters:
calendar
- the calendar- Returns:
- the
ZonedDateTime
formed from thisCalendar
-
asZonedDateTime
Converts this epoch time to aZonedDateTime
.- Parameters:
epochMilli
- the number of milliseconds from 1970-01-01T00:00:00Z- Returns:
- the
ZonedDateTime
formed from this epoch time
-
asZonedDateTime
Converts this epoch time to aZonedDateTime
.- Parameters:
epochMilli
- the number of milliseconds from 1970-01-01T00:00:00Zzone
- the time-zone- Returns:
- the
ZonedDateTime
formed from this epoch time
-
asZonedDateTime
Converts aLocalDate
to aZonedDateTime
- Parameters:
localDate
- the local datezone
- The time zone. Ifnull
, UTC is used- Returns:
- the
ZonedDateTime
formed from this local date
-
asLocalDate
-
asLocalDate
-
asLocalDate
Converts thisCalendar
object to aLocalDate
.
Warning: this conversion looses the Calendar's time components.- Parameters:
calendar
- the calendar- Returns:
- the
LocalDate
object
-
asDate
-
asDate
-
asDate
Converts thisZonedDateTime
object to aDate
.- Parameters:
zonedDateTime
- The local date time object- Returns:
- the
Date
part of thisZonedDateTime
-
asCalendar
Converts thisZonedDateTime
object to aCalendar
, setting the time zone to UTC.- Parameters:
zonedDateTime
- the zoned date time.- Returns:
- the converted
Calendar
object.
-
asCalendar
- Parameters:
localDate
- the local date- Returns:
- the
Calendar
object
-
formatDuration
Format a duration for logs- Parameters:
duration
- duration to log- Returns:
- a string representing the duration
-
formatDuration
Format a duration for logs- Parameters:
duration
- miliseconds representing the duration- Returns:
- a string representing the duration
-
getISODateTimeFormatter
Get the ISO date-time formatter that formats or parses a date-time with an offset, such as '2011-12-03T10:15:30.000+01:00'. This formatter is similar toDateTimeFormatter.ISO_OFFSET_DATE_TIME
but force 3-digits milliseconds.- Returns:
- ISO date-time formatter
-
dateToString
- Parameters:
value
- the value to convert- Returns:
- the date as a
String
-
epochMilliToString
Converts this epoch time to aString
using the ISO date formatter- Parameters:
epochMilli
- the number of milliseconds from 1970-01-01T00:00:00Z- Returns:
- the epoch time to a
String
-
zonedDateTimeToString
- Parameters:
zonedDateTime
- the zoned date time- Returns:
- the zoned date time as a
String
-
zonedDateTimeToString
public static String zonedDateTimeToString(ZonedDateTime zonedDateTime, ZoneId zoneId, String pattern) throws IllegalArgumentException Converts aZonedDateTime
object toString
using the given pattern for formatting, in the given zone, so as to format the instant in another zone
For instance, if provided pattern is 'uuuu-MM-dd'T'HH:mm:ss.SSSXXX':- for UTC zone, the zone date time corresponding to '2011-12-03T11:15:30+01:00' will be formatted as '2011-12-03T10:15:30Z'
- for +02:00 zone, the zone date time corresponding to '2011-12-03T11:15:30+01:00' will be formatted as '2011-12-03T12:15:30+02:00'
- Parameters:
zonedDateTime
- the zoned date timezoneId
- the target zonepattern
- the pattern for formatting- Returns:
- the zoned date time as a
String
, in the given zone - Throws:
IllegalArgumentException
- if the pattern is invalid
-
localDateToString
- Parameters:
localDate
- the local date- Returns:
- the local date as a
String
-
parse
Parses a String into aDate
, using ISO 8601 format.- Parameters:
value
- an ISO 8601 formatted String.- Returns:
- the corresponding Date, or null if the input is null.
-
parseZonedDateTime
Parses a String into aZonedDateTime
, using ISO date formatter.- Parameters:
zonedDateTimeAsString
- the zoned date time as string- Returns:
- the
ZonedDateTime
object or null if the input is null.
-
parseLocalDate
Parses a String into aLocalDate
, using ISO date formatter.- Parameters:
localDateAsString
- the local date as string- Returns:
- the
LocalDate
object or null if the input is null.
-