Class DateTimeUtils

java.lang.Object
host.anzo.commons.utils.DateTimeUtils

public class DateTimeUtils extends Object
Utility class for handling date and time operations. This class provides methods for converting between different date and time formats, calculating time spans, and manipulating date and time values.
Since:
9/20/2018
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final LocalDateTime
    The maximum SQL date represented as a LocalDateTime.
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static long
    getCurrentDayHourTime(long timeInMillis, int hour)
    Gets the epoch time for the specified hour of the current day.
    static long
    getCurrentDayPassedTime(long timeInMillis, @NotNull TimeUnit timeUnit)
    Calculates the time passed from the start of the current day in the specified time unit.
    static long
    Gets the epoch time for the start of the current month.
    static long
    getCurrentMonthStartTime(long timeInMillis)
    Gets the epoch time for the start of the current month.
    static @NotNull String
    getFormattedDateTime(@NotNull LocalDateTime localDateTime)
    Formats the given LocalDateTime into a string using the defined date format.
    static @NotNull String
    Returns a string representation of the international time for the given epoch milliseconds.
    static @NotNull String
    getInternationalTime(long millis)
    Returns a string representation of the international time for the given epoch milliseconds.
    static LocalDate
    getLocalDate(long epochTime)
    Converts an epoch time to a LocalDate.
    Gets the current LocalDateTime based on the system's current time.
    getLocalDateTime(long epochTime)
    Converts an epoch time to a LocalDateTime.
    static long
    Gets the epoch time for a specified hour the next day.
    static long
    getNextDayHourTime(int hour, int minute)
    Gets the epoch time for a specified hour and minute the next day.
    static long
    getNextDayHourTime(long timeInMillis, int hour)
    Gets the epoch time for a specified hour the next day.
    static long
    getNextDayHourTime(long timeInMillis, int hour, int minute)
    Gets the epoch time for a specified hour and minute the next day.
    static @NotNull LocalDateTime
    getNextDayHourTime(@NotNull LocalDateTime dateTime, int hour, int minute)
    Gets the LocalDateTime for a specified hour and minute the next day.
    static long
    Gets the epoch time for the start of the next day.
    static long
    getNextDayStartTime(long timeInMillis)
    Gets the epoch time for the start of the next day.
    static long
    getNextDayStartTime(long timeInMillis, DayOfWeek dayOfWeek)
    Gets the epoch time for the start of the next specified day of the week.
    static long
    Gets the epoch time for the next hour from the current time.
    static long
    getNextHourTime(int hour, int minute)
    Gets the epoch time for the next specified hour and minute.
    static long
    getNextHourTime(long timeInMillis)
    Gets the epoch time for the next hour from the current time.
    static long
    getNextHourTime(long timeInMillis, int hour)
    Gets the epoch time for the next specified hour.
    static long
    getNextHourTime(long timeInMillis, int hour, int minute)
    Gets the epoch time for the next specified hour and minute.
    getNextHourTime(@NotNull LocalDateTime dateTime, int hour, int minute)
    Gets the LocalDateTime for the next specified hour and minute.
    static long
    Gets the epoch time for the start of the next month.
    static long
    getNextMonthStartTime(long timeInMillis)
    Gets the epoch time for the start of the next month.
    static long
    getTimeTo(@NotNull LocalDateTime dateTime, @NotNull TimeUnit timeUnit)
    Gets the time in milliseconds to a specified date time in the specified time unit.
    static long
    Gets the time in milliseconds to the next day start.
    static long
    getTimeToNextDayStart(long timeInMillis)
    Gets the time in milliseconds to the next day start.
    static long
    getTimeToNextDayStart(long timeInMillis, DayOfWeek dayOfWeek)
    Gets the time in milliseconds to the next specified day of the week start.
    static long
    Gets the time in milliseconds to the next specified day of the week start.
    static long
    getTimeToNextHour(int hour, int minute, @NotNull TimeUnit timeUnit)
    Gets the time in milliseconds to the next specified hour and minute.
    getZonedDateTime(long epochTime)
    Converts an epoch time to a ZonedDateTime.
    static boolean
    isSameDay(long date1, long date2)
    Checks if two epoch dates fall on the same day.
    static DayOfWeek
    parseDay(int day)
    Parses an integer representing a day of the week and returns the corresponding DayOfWeek enum value.
    static int
    parseTimeSpan(@NotNull String timeSpan, TimeUnit unit)
    Parses a time span string in the format "HH:mm:ss" and converts it to the specified time unit.
    static long
    toEpochMillis(@NotNull LocalDateTime localDateTime)
    Converts a LocalDateTime to epoch milliseconds.
    static String
    toTimeSpan(long seconds)
    Converts a number of seconds into a time span string in the format "HH:mm:ss".

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • MAX_SQL_DATE

      public static final LocalDateTime MAX_SQL_DATE
      The maximum SQL date represented as a LocalDateTime.
  • Constructor Details

    • DateTimeUtils

      public DateTimeUtils()
  • Method Details

    • toEpochMillis

      public static long toEpochMillis(@NotNull @NotNull LocalDateTime localDateTime)
      Converts a LocalDateTime to epoch milliseconds.
      Parameters:
      localDateTime - the LocalDateTime to convert
      Returns:
      the epoch milliseconds representation of the given LocalDateTime
    • getInternationalTime

      @NotNull public static @NotNull String getInternationalTime(long millis)
      Returns a string representation of the international time for the given epoch milliseconds.
      Parameters:
      millis - the epoch milliseconds to convert
      Returns:
      a formatted string containing the time in system, EDT, and CET time zones
    • getInternationalTime

      @NotNull public static @NotNull String getInternationalTime()
      Returns a string representation of the international time for the given epoch milliseconds.
      Returns:
      a formatted string containing the time in system, EDT, and CET time zones
    • getFormattedDateTime

      @NotNull public static @NotNull String getFormattedDateTime(@NotNull @NotNull LocalDateTime localDateTime)
      Formats the given LocalDateTime into a string using the defined date format.
      Parameters:
      localDateTime - the LocalDateTime to format
      Returns:
      a formatted string representation of the given LocalDateTime
    • isSameDay

      public static boolean isSameDay(long date1, long date2)
      Checks if two epoch dates fall on the same day.
      Parameters:
      date1 - the first date in epoch format
      date2 - the second date in epoch format
      Returns:
      true if date1 is the same day as date2, false otherwise
    • parseTimeSpan

      public static int parseTimeSpan(@NotNull @NotNull String timeSpan, TimeUnit unit)
      Parses a time span string in the format "HH:mm:ss" and converts it to the specified time unit.
      Parameters:
      timeSpan - the time span string to parse
      unit - the time unit to convert to
      Returns:
      the time span in the specified time unit, or -1 if the format is invalid
    • toTimeSpan

      public static String toTimeSpan(long seconds)
      Converts a number of seconds into a time span string in the format "HH:mm:ss".
      Parameters:
      seconds - the number of seconds to convert
      Returns:
      a formatted string representation of the time span
    • parseDay

      public static DayOfWeek parseDay(int day)
      Parses an integer representing a day of the week and returns the corresponding DayOfWeek enum value.
      Parameters:
      day - the day of the week (SUNDAY=0, MONDAY=6)
      Returns:
      the corresponding DayOfWeek enum value
    • getLocalDateTime

      public static LocalDateTime getLocalDateTime()
      Gets the current LocalDateTime based on the system's current time.
      Returns:
      the current LocalDateTime
    • getLocalDateTime

      public static LocalDateTime getLocalDateTime(long epochTime)
      Converts an epoch time to a LocalDateTime.
      Parameters:
      epochTime - the epoch time to convert
      Returns:
      the corresponding LocalDateTime
    • getZonedDateTime

      public static ZonedDateTime getZonedDateTime(long epochTime)
      Converts an epoch time to a ZonedDateTime.
      Parameters:
      epochTime - the epoch time to convert
      Returns:
      the corresponding ZonedDateTime
    • getLocalDate

      public static LocalDate getLocalDate(long epochTime)
      Converts an epoch time to a LocalDate.
      Parameters:
      epochTime - the epoch time to convert
      Returns:
      the corresponding LocalDate
    • getCurrentDayPassedTime

      public static long getCurrentDayPassedTime(long timeInMillis, @NotNull @NotNull TimeUnit timeUnit)
      Calculates the time passed from the start of the current day in the specified time unit.
      Parameters:
      timeInMillis - the epoch time stamp
      timeUnit - the time unit to convert to
      Returns:
      the time passed from the start of the current day in the specified time unit
    • getCurrentDayHourTime

      public static long getCurrentDayHourTime(long timeInMillis, int hour)
      Gets the epoch time for the specified hour of the current day.
      Parameters:
      timeInMillis - the epoch time stamp
      hour - the specified hour
      Returns:
      the epoch time at the specified hour of the current day
    • getCurrentMonthStartTime

      public static long getCurrentMonthStartTime(long timeInMillis)
      Gets the epoch time for the start of the current month.
      Parameters:
      timeInMillis - the epoch time stamp
      Returns:
      the epoch time for the start of the current month
    • getCurrentMonthStartTime

      public static long getCurrentMonthStartTime()
      Gets the epoch time for the start of the current month.
      Returns:
      the epoch time for the start of the current month
    • getNextHourTime

      public static long getNextHourTime(long timeInMillis, int hour, int minute)
      Gets the epoch time for the next specified hour and minute.
      Parameters:
      timeInMillis - the epoch time stamp
      hour - the specified hour
      minute - the specified minute
      Returns:
      the epoch time for the next specified hour and minute
    • getNextHourTime

      public static long getNextHourTime(int hour, int minute)
      Gets the epoch time for the next specified hour and minute.
      Parameters:
      hour - the specified hour
      minute - the specified minute
      Returns:
      the epoch time for the next specified hour and minute
    • getNextHourTime

      public static LocalDateTime getNextHourTime(@NotNull @NotNull LocalDateTime dateTime, int hour, int minute)
      Gets the LocalDateTime for the next specified hour and minute.
      Parameters:
      dateTime - the current LocalDateTime
      hour - the specified hour
      minute - the specified minute
      Returns:
      the LocalDateTime for the next specified hour and minute
    • getNextHourTime

      public static long getNextHourTime(long timeInMillis, int hour)
      Gets the epoch time for the next specified hour.
      Parameters:
      timeInMillis - the epoch time stamp
      hour - the specified hour
      Returns:
      the epoch time for the next specified hour
    • getNextHourTime

      public static long getNextHourTime(long timeInMillis)
      Gets the epoch time for the next hour from the current time.
      Parameters:
      timeInMillis - the epoch time stamp
      Returns:
      the epoch time for the next hour from the current time
    • getNextHourTime

      public static long getNextHourTime()
      Gets the epoch time for the next hour from the current time.
      Returns:
      the epoch time for the next hour from the current time
    • getNextDayStartTime

      public static long getNextDayStartTime(long timeInMillis, DayOfWeek dayOfWeek)
      Gets the epoch time for the start of the next specified day of the week.
      Parameters:
      timeInMillis - the epoch time stamp
      dayOfWeek - the specified day of the week
      Returns:
      the epoch time for the start of the next specified day of the week
    • getNextDayStartTime

      public static long getNextDayStartTime(long timeInMillis)
      Gets the epoch time for the start of the next day.
      Parameters:
      timeInMillis - the epoch time stamp
      Returns:
      the epoch time for the start of the next day
    • getNextDayStartTime

      public static long getNextDayStartTime()
      Gets the epoch time for the start of the next day.
      Returns:
      the epoch time for the start of the next day
    • getNextDayHourTime

      public static long getNextDayHourTime(long timeInMillis, int hour)
      Gets the epoch time for a specified hour the next day.
      Parameters:
      timeInMillis - the epoch time stamp
      hour - the specified hour
      Returns:
      the epoch time for the specified hour the next day
    • getNextDayHourTime

      public static long getNextDayHourTime(int hour)
      Gets the epoch time for a specified hour the next day.
      Parameters:
      hour - the specified hour
      Returns:
      the epoch time for the specified hour the next day
    • getNextDayHourTime

      public static long getNextDayHourTime(long timeInMillis, int hour, int minute)
      Gets the epoch time for a specified hour and minute the next day.
      Parameters:
      timeInMillis - the epoch time stamp
      hour - the specified hour
      minute - the specified minute
      Returns:
      the epoch time for the specified hour and minute the next day
    • getNextDayHourTime

      public static long getNextDayHourTime(int hour, int minute)
      Gets the epoch time for a specified hour and minute the next day.
      Parameters:
      hour - the specified hour
      minute - the specified minute
      Returns:
      the epoch time for the specified hour and minute the next day
    • getNextDayHourTime

      @NotNull public static @NotNull LocalDateTime getNextDayHourTime(@NotNull @NotNull LocalDateTime dateTime, int hour, int minute)
      Gets the LocalDateTime for a specified hour and minute the next day.
      Parameters:
      dateTime - the LocalDateTime to calculate from
      hour - the specified hour
      minute - the specified minute
      Returns:
      the LocalDateTime for the specified hour and minute the next day
    • getNextMonthStartTime

      public static long getNextMonthStartTime(long timeInMillis)
      Gets the epoch time for the start of the next month.
      Parameters:
      timeInMillis - the epoch time stamp
      Returns:
      the epoch time for the start of the next month
    • getNextMonthStartTime

      public static long getNextMonthStartTime()
      Gets the epoch time for the start of the next month.
      Returns:
      the epoch time for the start of the next month
    • getTimeToNextDayStart

      public static long getTimeToNextDayStart(long timeInMillis, DayOfWeek dayOfWeek)
      Gets the time in milliseconds to the next specified day of the week start.
      Parameters:
      timeInMillis - the epoch time stamp
      dayOfWeek - the specified day of the week
      Returns:
      the time in milliseconds to the next specified day of the week start
    • getTimeToNextDayStart

      public static long getTimeToNextDayStart(DayOfWeek dayOfWeek)
      Gets the time in milliseconds to the next specified day of the week start.
      Parameters:
      dayOfWeek - the specified day of the week
      Returns:
      the time in milliseconds to the next specified day of the week start
    • getTimeToNextDayStart

      public static long getTimeToNextDayStart(long timeInMillis)
      Gets the time in milliseconds to the next day start.
      Parameters:
      timeInMillis - the epoch time stamp
      Returns:
      the time in milliseconds to the next day start
    • getTimeToNextDayStart

      public static long getTimeToNextDayStart()
      Gets the time in milliseconds to the next day start.
      Returns:
      the time in milliseconds to the next day start
    • getTimeTo

      public static long getTimeTo(@NotNull @NotNull LocalDateTime dateTime, @NotNull @NotNull TimeUnit timeUnit)
      Gets the time in milliseconds to a specified date time in the specified time unit.
      Parameters:
      dateTime - the date time to calculate time to
      timeUnit - the time unit to convert to
      Returns:
      the time to the specified date time in the specified time unit
    • getTimeToNextHour

      public static long getTimeToNextHour(int hour, int minute, @NotNull @NotNull TimeUnit timeUnit)
      Gets the time in milliseconds to the next specified hour and minute.
      Parameters:
      hour - the specified hour
      minute - the specified minute
      timeUnit - the time unit to convert to
      Returns:
      the time to the next specified hour and minute in the specified time unit