Class DateUtils
- Since:
- 1.59
- Version:
- 2.2
- Author:
- matt
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final DateTimeFormatterFormat for a long date and short time, for display purposes.static final DateTimeFormatterDate and time formatter using the ISO 8601 style but with an optional time component.static final DateTimeFormatterDate and time formatter using the ISO 8601 style but with an optional time component and a space character for the date/time separator instead of T.static final DateTimeFormatterDate and time formatter based onISO_DATE_OPT_TIME_ALTwith the local system default time zone applied.static final DateTimeFormatterDate and time formatter based onISO_DATE_OPT_TIME_ALTwith a UTC time zone offset applied.static final DateTimeFormatterDate and time formatter based onISO_DATE_OPT_TIMEwith the local system default time zone applied.static final DateTimeFormatterDate and time formatter using the ISO 8601 style but with an optional time component using an optional milliseconds of second component.static final DateTimeFormatterDate and time formatter using the ISO 8601 style but with an optional time component using an optional milliseconds of second component, and a space character for the date/time separator instead of T.static final DateTimeFormatterDate and time formatter based onISO_DATE_OPT_TIME_OPT_MILLIS_ALTwith a UTC time zone offset applied.static final DateTimeFormatterDate and time formatter based onISO_DATE_OPT_TIME_OPT_MILLISwith a UTC time zone offset applied.static final DateTimeFormatterDate and time formatter based onISO_DATE_OPT_TIMEwith a UTC time zone offset applied.static final DateTimeFormatterDate and time formatter using the ISO 8601 style but with a space character for the date/time separator instead of T.static final DateTimeFormatterDate and time formatter based onISO_DATE_TIME_ALTwith a UTC time zone offset applied.static final DateTimeFormatterDate and time formatter using the ISO 8601 style for local time with optional seconds, but with an optional milliseconds of second fraction component (instead of nanoseconds of second).static final DateTimeFormatterLocal time formatted for standard local time values like yyyy-MM-dd.static final DateTimeFormatterLocal time formatted for standard local time values like HH:mm.static final DateTimeFormatterLocal time formatted for standard local time values like HH:mm:ss.static final intThe number of standard minutes in a standard 24-hour day.static final PatternA range delimited pattern. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic StringFormat a standard local date value, inyyyy-MM-ddform.static StringFormat a standard local time value, inHH:mmform.static StringformatForLocalDisplay(Instant timestamp) Format an instant for display in the local (sytem) time zone.static StringformatHoursMinutesSeconds(long milliseconds) Get a formatted "x days hh:mm:ss" style string from a millisecond duration.static StringformatRange(ChronoField field, IntRange range, Locale locale, TextStyle style) Format a time range.static IntRangeparseDayOfMonthRange(String range, Locale locale) Parse a day of month range.static IntRangeparseDayOfWeekRange(String range, Locale locale) Parse a day of week range.static ZonedDateTimeparseIsoAltTimestamp(String value, ZoneId defaultZone) Parse an ISO-8601 alternate timestamp.static ZonedDateTimeparseIsoTimestamp(String value, ZoneId defaultZone) Parse an ISO-8601 timestamp.static ZonedDateTimeparseIsoTimestamp(DateTimeFormatter formatter, String value, ZoneId defaultZone) Parse an ISO-8601 alternate timestamp using a given formatter.static LocalDateparseLocalDate(String value) Parse a standard local date value, inyyyy-MM-ddform.static LocalTimeparseLocalTime(String value) Parse a standard local time value, inHH:mmform.static IntRangeparseMinuteOfDayRange(String range, Locale locale) Parse a hour of day range.static IntRangeparseMinuteOfDayRange(String range, Locale locale, boolean fix24) Parse a minute of day range.static IntRangeparseMonthRange(String range, Locale locale) Parse a month range.static IntRangeparseRange(TemporalField field, String range, Locale locale) Parse a time range.
-
Field Details
-
ISO_DATE_OPT_TIME_ALT
Date and time formatter using the ISO 8601 style but with an optional time component and a space character for the date/time separator instead of T.This supports patterns like:
- 2020-02-01 20:12:34+12:00
- 2020-02-01 20:12:34
- 2020-02-01 20:12
- 2020-02-01+12:00
- 2020-02-01
Note that parsing the date + zone form like 2020-02-01+12:00 requires using a
TemporalAccessor, like:TemporalAccessor ta = DateUtils.ISO_DATE_OPT_TIME_ALT.parse("2020-02-01+12:00"); ZonedDateTime ts = LocalDate.from(ta).atStartOfDay(ZoneId.from(ta)); -
ISO_DATE_OPT_TIME_ALT_UTC
Date and time formatter based onISO_DATE_OPT_TIME_ALTwith a UTC time zone offset applied. -
ISO_DATE_OPT_TIME_ALT_LOCAL
Date and time formatter based onISO_DATE_OPT_TIME_ALTwith the local system default time zone applied. -
ISO_DATE_OPT_TIME
Date and time formatter using the ISO 8601 style but with an optional time component.This supports patterns like:
- 2020-02-01T20:12:34+12:00
- 2020-02-01T20:12:34
- 2020-02-01T20:12
- 2020-02-01+12:00
- 2020-02-01
Note that parsing the date + zone form like 2020-02-01+12:00 requires using a
TemporalAccessor, like:TemporalAccessor ta = DateUtils.ISO_DATE_OPT_TIME.parse("2020-02-01+12:00"); ZonedDateTime ts = LocalDate.from(ta).atStartOfDay(ZoneId.from(ta));- Since:
- 2.2
-
ISO_DATE_OPT_TIME_UTC
Date and time formatter based onISO_DATE_OPT_TIMEwith a UTC time zone offset applied.- Since:
- 2.2
-
ISO_DATE_OPT_TIME_LOCAL
Date and time formatter based onISO_DATE_OPT_TIMEwith the local system default time zone applied.- Since:
- 2.2
-
ISO_DATE_TIME_ALT
Date and time formatter using the ISO 8601 style but with a space character for the date/time separator instead of T.This supports patterns like:
- 2020-02-01 20:12:34+12:00
- 2020-02-01 20:12:34
- 2020-02-01 20:12
- Since:
- 1.1
-
ISO_DATE_TIME_ALT_UTC
Date and time formatter based onISO_DATE_TIME_ALTwith a UTC time zone offset applied.- Since:
- 1.1
-
LOCAL_TIME
Local time formatted for standard local time values like HH:mm.- Since:
- 1.4
- See Also:
-
LOCAL_DATE
Local time formatted for standard local time values like yyyy-MM-dd.This is just an alias for
DateTimeFormatter.ISO_LOCAL_DATE.- Since:
- 2.0
-
LOCAL_TIME_FULL
Local time formatted for standard local time values like HH:mm:ss.This is just an alias for
DateTimeFormatter.ISO_LOCAL_TIME.- Since:
- 2.0
- See Also:
-
DISPLAY_DATE_LONG_TIME_SHORT
Format for a long date and short time, for display purposes.- Since:
- 1.5
-
ISO_LOCAL_TIME_OPT_MILLIS
Date and time formatter using the ISO 8601 style for local time with optional seconds, but with an optional milliseconds of second fraction component (instead of nanoseconds of second).This supports patterns like:
- 20:12:34.567
- 20:12:34
- 20:12
- Since:
- 2.0
-
ISO_DATE_OPT_TIME_OPT_MILLIS_ALT
Date and time formatter using the ISO 8601 style but with an optional time component using an optional milliseconds of second component, and a space character for the date/time separator instead of T.This supports patterns like:
- 2020-02-01 20:12:34.567
- 2020-02-01 20:12:34
- 2020-02-01 20:12
- 2020-02-01
- Since:
- 2.0
-
ISO_DATE_OPT_TIME_OPT_MILLIS_ALT_UTC
Date and time formatter based onISO_DATE_OPT_TIME_OPT_MILLIS_ALTwith a UTC time zone offset applied.- Since:
- 2.0
-
ISO_DATE_OPT_TIME_OPT_MILLIS
Date and time formatter using the ISO 8601 style but with an optional time component using an optional milliseconds of second component.This supports patterns like:
- 2020-02-01T20:12:34.567
- 2020-02-01T20:12:34
- 2020-02-01T20:12
- 2020-02-01
- Since:
- 2.1
-
ISO_DATE_OPT_TIME_OPT_MILLIS_UTC
Date and time formatter based onISO_DATE_OPT_TIME_OPT_MILLISwith a UTC time zone offset applied.- Since:
- 2.1
-
RANGE_DELIMITER
A range delimited pattern.- Since:
- 1.2
-
MINUTES_PER_DAY
public static final int MINUTES_PER_DAYThe number of standard minutes in a standard 24-hour day.- See Also:
-
-
Constructor Details
-
DateUtils
public DateUtils()
-
-
Method Details
-
parseIsoAltTimestamp
Parse an ISO-8601 alternate timestamp.This method handles input values that both include or omit a time zone offset. If a time zone offset is not provided in the input, then
defaultZonewill be used to get a final result.- Parameters:
value- the date time string to parsedefaultZone- a default time zone to use if one is not available invalue- Returns:
- the parsed date, or null if it cannot be parsed for any reason
-
parseIsoTimestamp
Parse an ISO-8601 timestamp.This method handles input values that both include or omit a time zone offset. If a time zone offset is not provided in the input, then
defaultZonewill be used to get a final result.- Parameters:
value- the date time string to parsedefaultZone- a default time zone to use if one is not available invalue- Returns:
- the parsed date, or null if it cannot be parsed for any reason
- Since:
- 2.2
-
parseIsoTimestamp
public static ZonedDateTime parseIsoTimestamp(DateTimeFormatter formatter, String value, ZoneId defaultZone) Parse an ISO-8601 alternate timestamp using a given formatter.This method handles input values that both include or omit a time zone offset. If a time zone offset is not provided in the input, then
defaultZonewill be used to get a final result.- Parameters:
formatter- the formatter, which must supportZonedDateTimeparsingvalue- the date time string to parsedefaultZone- a default time zone to use if one is not available invalue- Returns:
- the parsed date, or null if it cannot be parsed for any reason
- Since:
- 2.2
-
parseRange
public static IntRange parseRange(TemporalField field, String range, Locale locale) throws DateTimeException Parse a time range.The range can be specified using names, abbreviations, or numbers. The range of allowed numbers varies by field.
- Parameters:
field- the time field to parserange- the range string to parse into a time rangelocale- the locale to parse the range as- Returns:
- the range
- Throws:
DateTimeException- if any parsing error occurs- Since:
- 1.2
-
parseMonthRange
Parse a month range.The range can be specified using month names, abbreviations, or numbers. Months are numbered from 1-12.
- Parameters:
range- the range string to parse into a month rangelocale- the locale to parse the range as- Returns:
- the range, with a minimum of 1 and maximum of 12
- Throws:
DateTimeException- if any parsing error occurs- Since:
- 1.2
-
parseDayOfMonthRange
Parse a day of month range.The range can be specified using day numbers. Days are numbered from 1-31.
- Parameters:
range- the range string to parse into a day of month rangelocale- the locale to parse the range as- Returns:
- the range, with a minimum of 1 and maximum of 31
- Throws:
DateTimeException- if any parsing error occurs- Since:
- 1.2
-
parseDayOfWeekRange
Parse a day of week range.The range can be specified using weekday names, abbreviations, or numbers. Weekdays are numbered from 1-7 with Monday starting at 1.
- Parameters:
range- the range string to parse into a day of week rangelocale- the locale to parse the range as- Returns:
- the range, with a minimum of 1 and maximum of 7
- Throws:
DateTimeException- if any parsing error occurs- Since:
- 1.2
-
parseMinuteOfDayRange
Parse a hour of day range.The range can be specified using numbers. Hours of 0 - 24 are allowed.
- Parameters:
range- the range string to parse into a day of week rangelocale- the locale to parse the range as- Returns:
- the range, with a minimum of 1 and maximum of 24
- Throws:
DateTimeException- if any parsing error occurs- Since:
- 1.2
-
parseMinuteOfDayRange
public static IntRange parseMinuteOfDayRange(String range, Locale locale, boolean fix24) throws DateTimeException Parse a minute of day range.The range can be specified using ISO local time strings or numbers. For strings, the format HH:MM is used.
For numbers, the values are assumed to be whole hours and values between 0 - 24 are allowed.
- Parameters:
range- the range string to parse into a day of week rangelocale- the locale to parse the range asfix24- if true then the value 24 will be changed to23:59, otherwise 24 will be left as-is- Returns:
- the range, with a minimum of 0 and maximum of 1439
- Throws:
DateTimeException- if any parsing error occurs- Since:
- 1.2
-
formatRange
public static String formatRange(ChronoField field, IntRange range, Locale locale, TextStyle style) throws DateTimeException Format a time range.The range can be specified using names, abbreviations, or numbers. The range of allowed numbers varies by field.
- Parameters:
field- the time field to formatrange- the range to format into a stringlocale- the locale to format the range as, or null to use the system defaultstyle- the formatting style- Returns:
- the range string
- Throws:
DateTimeException- if any formatting error occurs- Since:
- 1.2
-
formatHoursMinutesSeconds
Get a formatted "x days hh:mm:ss" style string from a millisecond duration.The output pattern is like: Dd HH:MM:SS where D is the number of whole days, HH the number of hours, MM the number of minutes, and SS the number of seconds.
The returned string will be shortened to exclude the day and hour components if those values are 0.
Examples of output are:
3d 12:34:56 23:45:43 34:56 00:01
- Parameters:
milliseconds- the millisecond duration- Returns:
- the formatted string
-
parseLocalTime
Parse a standard local time value, inHH:mmform.- Parameters:
value- the time value- Returns:
- the LocalTime object
-
format
Format a standard local time value, inHH:mmform.- Parameters:
value- the LocalTime to format- Returns:
- the formatted value
-
parseLocalDate
Parse a standard local date value, inyyyy-MM-ddform.- Parameters:
value- the date value- Returns:
- the LocalDate object
-
format
Format a standard local date value, inyyyy-MM-ddform.- Parameters:
value- the LocalDate to format- Returns:
- the formatted value
-
formatForLocalDisplay
Format an instant for display in the local (sytem) time zone.- Parameters:
timestamp- the instant- Returns:
- the formatted date
- Since:
- 1.5
-