Class 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 Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionlong
format
(ZonedDateTime zonedDateTime) Formats the given ZonedDateTime using the default date formatter.Formats the given Date using the default date formatter.from
(ZonedDateTime dateTime) Converts the given ZonedDateTime to an Instant.Converts the given Date to an XMLGregorianCalender, based on the system default ZoneId.Converts the given Date to an XMLGregorianCalender, based on the given ZoneId.from
(GregorianCalendar gregorianCalendar) Converts the given GregorianCalendar to an Instant.from
(XMLGregorianCalendar xmlGregorianCalendar) Converts the given XMLGregorianCalendar to an Instant.get
(Instant from, Instant to, ChronoUnit unit) This method generates a list of InstantRange objects, each representing a period within the given range.This method generates a list of InstantRange objects, each representing a day within the given range.This method generates a list of InstantRange objects, each representing an hour within the given range.This method generates a list of InstantRange objects, each representing a month within the given range.This method generates a list of InstantRange objects, each representing a week within the given range.now()
parseToUTC
(CharSequence date) Attempts to parse the given date string into a ZonedDateTime.parseToUTC
(CharSequence date, UTC_FORMATTER formatter) Attempts to parse the given date string into a ZonedDateTime, using the given formatter.
-
Field Details
-
DEFAULT_DATE_FORMAT
- See Also:
-
-
Constructor Details
-
Dates
public Dates()
-
-
Method Details
-
getMonths
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
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
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
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
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 isChronoUnit.MINUTES
, then each period will be one minute long.- Returns:
- A list of InstantRange objects, each representing a period within the given range.
-
from
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
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
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
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
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
- Returns:
- The current time, truncated to milliseconds, in UTC.
-
nowTruncatedToSeconds
- Returns:
- The current time, truncated to seconds, in UTC.
-
nowInstant
- Returns:
- The current time, truncated to milliseconds, in UTC.
-
parseToUTC
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
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
- Returns:
- The default date formatter.
-
minimumZonedDateTime
- Returns:
- The minimum ZonedDateTime that can be represented.
-
maximumZonedDateTime
- Returns:
- The maximum ZonedDateTime that can be represented.
-
currentTimeMillis
public long currentTimeMillis()- Returns:
- The current time in milliseconds.
-
format
Formats the given ZonedDateTime using the default date formatter.- Parameters:
zonedDateTime
- The ZonedDateTime to format.- Returns:
- The formatted date string.
-
format
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.
-