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
FieldsModifier and TypeFieldDescriptionstatic final StringThe ISO date-time pattern that formats or parses a date-time with an offset -
Method Summary
Modifier and TypeMethodDescriptionstatic CalendarasCalendar(LocalDate localDate) static CalendarasCalendar(ZonedDateTime zonedDateTime) Converts thisZonedDateTimeobject to aCalendar, setting the time zone to UTC.static Datestatic Datestatic DateasDate(ZonedDateTime zonedDateTime) Converts thisZonedDateTimeobject to aDate.static Instantstatic LocalDateasLocalDate(Instant instant) static LocalDateasLocalDate(Instant instant, ZoneId zone) static LocalDateasLocalDate(Calendar calendar) static LocalDateasLocalDate(Date date) static LocalDateasLocalDate(Date date, ZoneId zone) static ZonedDateTimeasZonedDateTime(long epochMilli) Converts this epoch time to aZonedDateTime.static ZonedDateTimeasZonedDateTime(long epochMilli, ZoneId zone) Converts this epoch time to aZonedDateTime.static ZonedDateTimeasZonedDateTime(Instant instant) Converts anInstantto aZonedDateTimestatic ZonedDateTimeasZonedDateTime(Instant instant, ZoneId zone) Converts anInstantto aZonedDateTimestatic ZonedDateTimeasZonedDateTime(LocalDate localDate, ZoneId zone) Converts aLocalDateto aZonedDateTimestatic ZonedDateTimeasZonedDateTime(Calendar calendar) Converts thisCalendarobject to aZonedDateTime.static ZonedDateTimeasZonedDateTime(Date date) Converts thisDateobject to aZonedDateTime, at UTC.static ZonedDateTimeasZonedDateTime(Date date, ZoneId zone) Converts thisDateobject to aZonedDateTime.static StringdateToString(Date value) static StringepochMilliToString(long epochMilli) Converts this epoch time to aStringusing the ISO date formatterstatic StringformatDuration(long duration) Format a duration for logsstatic StringformatDuration(Duration duration) Format a duration for logsstatic DateTimeFormatterGet 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 booleanisAtMidnight(String zonedDateTimeAsString, String zoneId) Determines if a date is at midnight (00:00) for the given time-zonestatic StringlocalDateToString(LocalDate localDate) static StringlocalDateToString(LocalDate localDate, String pattern) static DateParses a String into aDate, using ISO 8601 format.static LocalDateparseLocalDate(String localDateAsString) Parses a String into aLocalDate, using ISO local date formatter.static LocalDateparseLocalDate(String localDateAsString, String pattern) Parses a String into aLocalDate, using ISO local date formatter.static LocalDateparseLocalDate(String localDateAsString, Optional<DateTimeFormatter> formatter) Parses a String into aLocalDate, using the given formatter.static ZonedDateTimeparseZonedDateTime(String zonedDateTimeAsString) Parses a String into aZonedDateTime, using ISO date time formatter.static ZonedDateTimeparseZonedDateTime(String zonedDateTimeAsString, String pattern) Parses a String into aZonedDateTime, using ISO date time formatter.static ZonedDateTimeparseZonedDateTime(String zonedDateTimeAsString, Optional<DateTimeFormatter> formatter) Parses a String into aZonedDateTime, using the given formatter.static StringzonedDateTimeToString(ZonedDateTime zonedDateTime) static StringzonedDateTimeToString(ZonedDateTime zonedDateTime, ZoneId zoneId) Converts aZonedDateTimeobject toStringin the given zone, so as to format the instant in another zone, using the ISO date time formatter with pattern 'uuuu-MM-dd'T'HH:mm:ss.SSSXXX'.static StringzonedDateTimeToString(ZonedDateTime zonedDateTime, ZoneId zoneId, String pattern) Converts aZonedDateTimeobject toStringusing 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
Dateobject
-
asZonedDateTime
Converts thisDateobject to aZonedDateTime, at UTC.- Parameters:
date- The date object- Returns:
- the
ZonedDateTimeformed from thisDate
-
asZonedDateTime
Converts thisDateobject to aZonedDateTime.- Parameters:
date- The date objectzone- The zone. Ifnull, UTC is used- Returns:
- the
ZonedDateTimeformed from thisDate
-
asZonedDateTime
Converts thisCalendarobject to aZonedDateTime.- Parameters:
calendar- the calendar- Returns:
- the
ZonedDateTimeformed from thisCalendar
-
asZonedDateTime
Converts this epoch time to aZonedDateTime.- Parameters:
epochMilli- the number of milliseconds from 1970-01-01T00:00:00Z- Returns:
- the
ZonedDateTimeformed 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
ZonedDateTimeformed from this epoch time
-
asZonedDateTime
Converts aLocalDateto aZonedDateTime- Parameters:
localDate- the local datezone- The time zone. Ifnull, UTC is used- Returns:
- the
ZonedDateTimeformed from this local date
-
asZonedDateTime
Converts anInstantto aZonedDateTime- Parameters:
instant- the instant- Returns:
- the
ZonedDateTimeformed from this local date
-
asZonedDateTime
Converts anInstantto aZonedDateTime- Parameters:
instant- the instantzone- The time zone. Ifnull, UTC is used- Returns:
- the
ZonedDateTimeformed from this local date
-
asLocalDate
-
asLocalDate
-
asLocalDate
Converts thisCalendarobject to aLocalDate.
Warning: this conversion looses the Calendar's time components.- Parameters:
calendar- the calendar- Returns:
- the
LocalDateobject
-
asLocalDate
- Parameters:
instant- the instant- Returns:
- the
LocalDateformed from this local date
-
asLocalDate
- Parameters:
instant- the instantzone- The time zone. Ifnull, UTC is used- Returns:
- the
LocalDateformed from this local date
-
asDate
-
asDate
-
asDate
Converts thisZonedDateTimeobject to aDate.- Parameters:
zonedDateTime- The local date time object- Returns:
- the
Datepart of thisZonedDateTime
-
asCalendar
Converts thisZonedDateTimeobject to aCalendar, setting the time zone to UTC.- Parameters:
zonedDateTime- the zoned date time.- Returns:
- the converted
Calendarobject.
-
asCalendar
- Parameters:
localDate- the local date- Returns:
- the
Calendarobject
-
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_TIMEbut 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 aStringusing the ISO date formatter- Parameters:
epochMilli- the number of milliseconds from 1970-01-01T00:00:00Z- Returns:
- the epoch time to a
String
-
zonedDateTimeToString
Converts aZonedDateTimeobject toStringin the given zone, so as to format the instant in another zone, using the ISO date time formatter with pattern 'uuuu-MM-dd'T'HH:mm:ss.SSSXXX'. For instance:- 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 zone- Returns:
- the zoned date time as a
String, in the given zone
-
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 aZonedDateTimeobject toStringusing 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
-
localDateToString
public static String localDateToString(LocalDate localDate, String pattern) throws IllegalArgumentException - Parameters:
localDate- the local datepattern- the pattern for formatting- Returns:
- the local date as a
String - Throws:
IllegalArgumentException- if the pattern is invalid
-
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 time formatter.- Parameters:
zonedDateTimeAsString- the zoned date time as string- Returns:
- the
ZonedDateTimeobject or null if the input is null.
-
parseZonedDateTime
public static ZonedDateTime parseZonedDateTime(String zonedDateTimeAsString, String pattern) throws IllegalArgumentException Parses a String into aZonedDateTime, using ISO date time formatter.- Parameters:
zonedDateTimeAsString- the zoned date time as stringpattern- the pattern to use to parse the given date- Returns:
- the
ZonedDateTimeobject or null if the input is null. - Throws:
IllegalArgumentException- if the pattern is invalid
-
parseZonedDateTime
public static ZonedDateTime parseZonedDateTime(String zonedDateTimeAsString, Optional<DateTimeFormatter> formatter) Parses a String into aZonedDateTime, using the given formatter. If no formatter, the ISO date time formatter is used- Parameters:
zonedDateTimeAsString- the zoned date time as stringformatter- the date time formatter- Returns:
- the
ZonedDateTimeobject or null if the input is null.
-
parseLocalDate
Parses a String into aLocalDate, using ISO local date formatter.- Parameters:
localDateAsString- the local date as string- Returns:
- the
LocalDateobject or null if the input is null.
-
parseLocalDate
public static LocalDate parseLocalDate(String localDateAsString, String pattern) throws IllegalArgumentException Parses a String into aLocalDate, using ISO local date formatter.- Parameters:
localDateAsString- the local date as stringpattern- the pattern to use to parse the given date- Returns:
- the
LocalDateobject or null if the input is null. - Throws:
IllegalArgumentException- if the pattern is invalid
-
parseLocalDate
public static LocalDate parseLocalDate(String localDateAsString, Optional<DateTimeFormatter> formatter) Parses a String into aLocalDate, using the given formatter. If no formatter, the ISO local date formatter is used- Parameters:
localDateAsString- the local date as stringformatter- the date time formatter- Returns:
- the
LocalDateobject or null if the input is null.
-
isAtMidnight
Determines if a date is at midnight (00:00) for the given time-zone- Parameters:
zonedDateTimeAsString- the zoned date time as stringzoneId- the time-zone id. If empty the system default time-zone will be used.- Returns:
- true if the date is at midnight
-