Class DateUtils

java.lang.Object
net.solarnetwork.util.DateUtils

public final class DateUtils extends Object
Date and time utilities.
Since:
1.59
Version:
2.2
Author:
matt
  • Field Details

    • ISO_DATE_OPT_TIME_ALT

      public static final DateTimeFormatter 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

      public static final DateTimeFormatter ISO_DATE_OPT_TIME_ALT_UTC
      Date and time formatter based on ISO_DATE_OPT_TIME_ALT with a UTC time zone offset applied.
    • ISO_DATE_OPT_TIME_ALT_LOCAL

      public static final DateTimeFormatter ISO_DATE_OPT_TIME_ALT_LOCAL
      Date and time formatter based on ISO_DATE_OPT_TIME_ALT with the local system default time zone applied.
    • ISO_DATE_OPT_TIME

      public static final DateTimeFormatter 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

      public static final DateTimeFormatter ISO_DATE_OPT_TIME_UTC
      Date and time formatter based on ISO_DATE_OPT_TIME with a UTC time zone offset applied.
      Since:
      2.2
    • ISO_DATE_OPT_TIME_LOCAL

      public static final DateTimeFormatter ISO_DATE_OPT_TIME_LOCAL
      Date and time formatter based on ISO_DATE_OPT_TIME with the local system default time zone applied.
      Since:
      2.2
    • ISO_DATE_TIME_ALT

      public static final DateTimeFormatter 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

      public static final DateTimeFormatter ISO_DATE_TIME_ALT_UTC
      Date and time formatter based on ISO_DATE_TIME_ALT with a UTC time zone offset applied.
      Since:
      1.1
    • LOCAL_TIME

      public static final DateTimeFormatter LOCAL_TIME
      Local time formatted for standard local time values like HH:mm.
      Since:
      1.4
      See Also:
    • LOCAL_DATE

      public static final DateTimeFormatter 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

      public static final DateTimeFormatter 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

      public static final DateTimeFormatter DISPLAY_DATE_LONG_TIME_SHORT
      Format for a long date and short time, for display purposes.
      Since:
      1.5
    • ISO_LOCAL_TIME_OPT_MILLIS

      public static final DateTimeFormatter 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

      public static final DateTimeFormatter 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

      public static final DateTimeFormatter ISO_DATE_OPT_TIME_OPT_MILLIS_ALT_UTC
      Date and time formatter based on ISO_DATE_OPT_TIME_OPT_MILLIS_ALT with a UTC time zone offset applied.
      Since:
      2.0
    • ISO_DATE_OPT_TIME_OPT_MILLIS

      public static final DateTimeFormatter 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

      public static final DateTimeFormatter ISO_DATE_OPT_TIME_OPT_MILLIS_UTC
      Date and time formatter based on ISO_DATE_OPT_TIME_OPT_MILLIS with a UTC time zone offset applied.
      Since:
      2.1
    • RANGE_DELIMITER

      public static final Pattern RANGE_DELIMITER
      A range delimited pattern.
      Since:
      1.2
    • MINUTES_PER_DAY

      public static final int MINUTES_PER_DAY
      The number of standard minutes in a standard 24-hour day.
      See Also:
  • Constructor Details

    • DateUtils

      public DateUtils()
  • Method Details

    • parseIsoAltTimestamp

      public static ZonedDateTime parseIsoAltTimestamp(String value, ZoneId defaultZone)
      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 defaultZone will be used to get a final result.

      Parameters:
      value - the date time string to parse
      defaultZone - a default time zone to use if one is not available in value
      Returns:
      the parsed date, or null if it cannot be parsed for any reason
    • parseIsoTimestamp

      public static ZonedDateTime parseIsoTimestamp(String value, ZoneId defaultZone)
      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 defaultZone will be used to get a final result.

      Parameters:
      value - the date time string to parse
      defaultZone - a default time zone to use if one is not available in value
      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 defaultZone will be used to get a final result.

      Parameters:
      formatter - the formatter, which must support ZonedDateTime parsing
      value - the date time string to parse
      defaultZone - a default time zone to use if one is not available in value
      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 parse
      range - the range string to parse into a time range
      locale - the locale to parse the range as
      Returns:
      the range
      Throws:
      DateTimeException - if any parsing error occurs
      Since:
      1.2
    • parseMonthRange

      public static IntRange parseMonthRange(String range, Locale locale) throws DateTimeException
      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 range
      locale - 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

      public static IntRange parseDayOfMonthRange(String range, Locale locale) throws DateTimeException
      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 range
      locale - 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

      public static IntRange parseDayOfWeekRange(String range, Locale locale) throws DateTimeException
      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 range
      locale - 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

      public static IntRange parseMinuteOfDayRange(String range, Locale locale) throws DateTimeException
      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 range
      locale - 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 range
      locale - the locale to parse the range as
      fix24 - if true then the value 24 will be changed to 23: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 format
      range - the range to format into a string
      locale - the locale to format the range as, or null to use the system default
      style - the formatting style
      Returns:
      the range string
      Throws:
      DateTimeException - if any formatting error occurs
      Since:
      1.2
    • formatHoursMinutesSeconds

      public static String formatHoursMinutesSeconds(long milliseconds)
      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

      public static LocalTime parseLocalTime(String value)
      Parse a standard local time value, in HH:mm form.
      Parameters:
      value - the time value
      Returns:
      the LocalTime object
    • format

      public static String format(LocalTime value)
      Format a standard local time value, in HH:mm form.
      Parameters:
      value - the LocalTime to format
      Returns:
      the formatted value
    • parseLocalDate

      public static LocalDate parseLocalDate(String value)
      Parse a standard local date value, in yyyy-MM-dd form.
      Parameters:
      value - the date value
      Returns:
      the LocalDate object
    • format

      public static String format(LocalDate value)
      Format a standard local date value, in yyyy-MM-dd form.
      Parameters:
      value - the LocalDate to format
      Returns:
      the formatted value
    • formatForLocalDisplay

      public static String formatForLocalDisplay(Instant timestamp)
      Format an instant for display in the local (sytem) time zone.
      Parameters:
      timestamp - the instant
      Returns:
      the formatted date
      Since:
      1.5