Class Dates

java.lang.Object
com.persequor.extension.util.Dates

@Singleton public class Dates extends Object
Utility class for handling dates.

Especially useful for ensuring that classes which are time based are testable, as the time can be mocked.

Also ensures that date formats are consistent and time truncation is consistent.

To use, inject this class into your class, either as a field or as a constructor parameter.

  • Field Details

  • Constructor Details

    • Dates

      public Dates()
  • Method Details

    • getMonths

      public List<InstantRange> getMonths(Instant from, Instant to)
      This method generates a list of InstantRange objects, each representing a month within the given range. The range is divided into months, starting from the 'from' Instant and ending at the 'to' Instant. Each InstantRange object in the list represents a month, with the start and end Instants of that month.

      Each month starts on the time of day that the 'from' Instant has.

      Parameters:
      from - The start of the range. This is the Instant from which the first month starts.
      to - The end of the range. This is the Instant at which the last month ends.
      Returns:
      A list of InstantRange objects, each representing a month within the given range.
    • getWeeks

      public List<InstantRange> getWeeks(Instant from, Instant to)
      This method generates a list of InstantRange objects, each representing a week within the given range. The range is divided into weeks, starting from the 'from' Instant and ending at the 'to' Instant. Each InstantRange object in the list represents a week, with the start and end Instants of that week.

      Each week starts on the time of day that the 'from' Instant has.

      Parameters:
      from - The start of the range. This is the Instant from which the first week starts.
      to - The end of the range. This is the Instant at which the last week ends.
      Returns:
      A list of InstantRange objects, each representing a week within the given range.
    • getDays

      public List<InstantRange> getDays(Instant from, Instant to)
      This method generates a list of InstantRange objects, each representing a day within the given range. The range is divided into days, starting from the 'from' Instant and ending at the 'to' Instant. Each InstantRange object in the list represents a day, with the start and end Instants of that day.

      Each day starts on the time of day that the 'from' Instant has.

      Parameters:
      from - The start of the range. This is the Instant from which the first day starts.
      to - The end of the range. This is the Instant at which the last day ends.
      Returns:
      A list of InstantRange objects, each representing a day within the given range.
    • getHours

      public List<InstantRange> getHours(Instant from, Instant to)
      This method generates a list of InstantRange objects, each representing an hour within the given range. The range is divided into hours, starting from the 'from' Instant and ending at the 'to' Instant. Each InstantRange object in the list represents an hour, with the start and end Instants of that hour.

      Each hour starts on the time of day that the 'from' Instant has.

      Parameters:
      from - The start of the range. This is the Instant from which the first hour starts.
      to - The end of the range. This is the Instant at which the last hour ends.
      Returns:
      A list of InstantRange objects, each representing an hour within the given range.
    • get

      public List<InstantRange> get(Instant from, Instant to, ChronoUnit unit)
      This method generates a list of InstantRange objects, each representing a period within the given range. The range is divided into periods, starting from the 'from' Instant and ending at the 'to' Instant. Each InstantRange object in the list represents a period, with the start and end Instants of that period.

      Each period starts on the time of day that the 'from' Instant has.

      Parameters:
      from - The start of the range. This is the Instant from which the first minute starts.
      to - The end of the range. This is the Instant at which the last minute ends.
      unit - The unit of the period. For example, if the unit is ChronoUnit.MINUTES, then each period will be one minute long.
      Returns:
      A list of InstantRange objects, each representing a period within the given range.
    • from

      public Instant from(GregorianCalendar gregorianCalendar)
      Converts the given GregorianCalendar to an Instant.

      Useful for ensuring a class is testable, as the returned Instant can be mocked.

      Parameters:
      gregorianCalendar - The GregorianCalendar to convert.
      Returns:
      The Instant representing the same time as the given GregorianCalendar.
    • from

      public Instant from(ZonedDateTime dateTime)
      Converts the given ZonedDateTime to an Instant.

      Useful for ensuring a class is testable, as the returned Instant can be mocked.

      Parameters:
      dateTime - The ZonedDateTime to convert.
      Returns:
      The Instant representing the same time as the given ZonedDateTime.
    • from

      public Instant from(XMLGregorianCalendar xmlGregorianCalendar)
      Converts the given XMLGregorianCalendar to an Instant.

      Useful for ensuring a class is testable, as the returned Instant can be mocked.

      Parameters:
      xmlGregorianCalendar - The XMLGregorianCalendar to convert.
      Returns:
      The Instant representing the same time as the given XMLGregorianCalendar.
    • from

      public XMLGregorianCalendar from(Date date, ZoneId zoneId)
      Converts the given Date to an XMLGregorianCalender, based on the given ZoneId.

      Useful for ensuring a class is testable, as the returned XMLGregorianCalender can be mocked.

      Parameters:
      date - The Date to convert.
      Returns:
      The XMLGregorianCalender representing the same time as the given Date.
    • from

      public XMLGregorianCalendar from(Date date)
      Converts the given Date to an XMLGregorianCalender, based on the system default ZoneId.

      Useful for ensuring a class is testable, as the returned XMLGregorianCalender can be mocked.

      Parameters:
      date - The Date to convert.
      Returns:
      The XMLGregorianCalender representing the same time as the given Date.
    • now

      public ZonedDateTime now()
      Returns:
      The current time, truncated to milliseconds, in UTC.
    • nowTruncatedToSeconds

      public ZonedDateTime nowTruncatedToSeconds()
      Returns:
      The current time, truncated to seconds, in UTC.
    • nowInstant

      public Instant nowInstant()
      Returns:
      The current time, truncated to milliseconds, in UTC.
    • parseToUTC

      public ZonedDateTime parseToUTC(CharSequence date)
      Attempts to parse the given date string into a ZonedDateTime.

      Then converts the ZonedDateTime to UTC, does not change the actual point in time that is represented.

      Parameters:
      date - The date string to parse.
      Returns:
      The parsed ZonedDateTime.
    • parseToUTC

      public ZonedDateTime parseToUTC(CharSequence date, UTC_FORMATTER formatter)
      Attempts to parse the given date string into a ZonedDateTime, using the given formatter.

      Then converts the ZonedDateTime to UTC, does not change the actual point in time that is represented.

      Parameters:
      date - The date string to parse.
      formatter - The formatter to use when parsing the date string.
      Returns:
      The parsed ZonedDateTime.
    • getDateFormatter

      public DateTimeFormatter getDateFormatter()
      Returns:
      The default date formatter.
    • minimumZonedDateTime

      public ZonedDateTime minimumZonedDateTime()
      Returns:
      The minimum ZonedDateTime that can be represented.
    • maximumZonedDateTime

      public ZonedDateTime maximumZonedDateTime()
      Returns:
      The maximum ZonedDateTime that can be represented.
    • currentTimeMillis

      public long currentTimeMillis()
      Returns:
      The current time in milliseconds.
    • format

      public String format(ZonedDateTime zonedDateTime)
      Formats the given ZonedDateTime using the default date formatter.
      Parameters:
      zonedDateTime - The ZonedDateTime to format.
      Returns:
      The formatted date string.
    • format

      public String format(Date date)
      Formats the given Date using the default date formatter.

      Assumes the Date is in UTC.

      Parameters:
      date - The Instant to format.
      Returns:
      The formatted date string.