Package host.anzo.commons.utils
Class DateTimeUtils
java.lang.Object
host.anzo.commons.utils.DateTimeUtils
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
FieldsModifier and TypeFieldDescriptionstatic final LocalDateTimeThe maximum SQL date represented as a LocalDateTime. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic longgetCurrentDayHourTime(long timeInMillis, int hour) Gets the epoch time for the specified hour of the current day.static longgetCurrentDayPassedTime(long timeInMillis, @NotNull TimeUnit timeUnit) Calculates the time passed from the start of the current day in the specified time unit.static longGets the epoch time for the start of the current month.static longgetCurrentMonthStartTime(long timeInMillis) Gets the epoch time for the start of the current month.static @NotNull StringgetFormattedDateTime(@NotNull LocalDateTime localDateTime) Formats the given LocalDateTime into a string using the defined date format.static @NotNull StringReturns a string representation of the international time for the given epoch milliseconds.static @NotNull StringgetInternationalTime(long millis) Returns a string representation of the international time for the given epoch milliseconds.static LocalDategetLocalDate(long epochTime) Converts an epoch time to a LocalDate.static LocalDateTimeGets the current LocalDateTime based on the system's current time.static LocalDateTimegetLocalDateTime(long epochTime) Converts an epoch time to a LocalDateTime.static longgetNextDayHourTime(int hour) Gets the epoch time for a specified hour the next day.static longgetNextDayHourTime(int hour, int minute) Gets the epoch time for a specified hour and minute the next day.static longgetNextDayHourTime(long timeInMillis, int hour) Gets the epoch time for a specified hour the next day.static longgetNextDayHourTime(long timeInMillis, int hour, int minute) Gets the epoch time for a specified hour and minute the next day.static @NotNull LocalDateTimegetNextDayHourTime(@NotNull LocalDateTime dateTime, int hour, int minute) Gets the LocalDateTime for a specified hour and minute the next day.static longGets the epoch time for the start of the next day.static longgetNextDayStartTime(long timeInMillis) Gets the epoch time for the start of the next day.static longgetNextDayStartTime(long timeInMillis, DayOfWeek dayOfWeek) Gets the epoch time for the start of the next specified day of the week.static longGets the epoch time for the next hour from the current time.static longgetNextHourTime(int hour, int minute) Gets the epoch time for the next specified hour and minute.static longgetNextHourTime(long timeInMillis) Gets the epoch time for the next hour from the current time.static longgetNextHourTime(long timeInMillis, int hour) Gets the epoch time for the next specified hour.static longgetNextHourTime(long timeInMillis, int hour, int minute) Gets the epoch time for the next specified hour and minute.static LocalDateTimegetNextHourTime(@NotNull LocalDateTime dateTime, int hour, int minute) Gets the LocalDateTime for the next specified hour and minute.static longGets the epoch time for the start of the next month.static longgetNextMonthStartTime(long timeInMillis) Gets the epoch time for the start of the next month.static longgetTimeTo(@NotNull LocalDateTime dateTime, @NotNull TimeUnit timeUnit) Gets the time in milliseconds to a specified date time in the specified time unit.static longGets the time in milliseconds to the next day start.static longgetTimeToNextDayStart(long timeInMillis) Gets the time in milliseconds to the next day start.static longgetTimeToNextDayStart(long timeInMillis, DayOfWeek dayOfWeek) Gets the time in milliseconds to the next specified day of the week start.static longgetTimeToNextDayStart(DayOfWeek dayOfWeek) Gets the time in milliseconds to the next specified day of the week start.static longgetTimeToNextHour(int hour, int minute, @NotNull TimeUnit timeUnit) Gets the time in milliseconds to the next specified hour and minute.static ZonedDateTimegetZonedDateTime(long epochTime) Converts an epoch time to a ZonedDateTime.static booleanisSameDay(long date1, long date2) Checks if two epoch dates fall on the same day.static DayOfWeekparseDay(int day) Parses an integer representing a day of the week and returns the corresponding DayOfWeek enum value.static intparseTimeSpan(@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 longtoEpochMillis(@NotNull LocalDateTime localDateTime) Converts a LocalDateTime to epoch milliseconds.static StringtoTimeSpan(long seconds) Converts a number of seconds into a time span string in the format "HH:mm:ss".
-
Field Details
-
MAX_SQL_DATE
The maximum SQL date represented as a LocalDateTime.
-
-
Constructor Details
-
DateTimeUtils
public DateTimeUtils()
-
-
Method Details
-
toEpochMillis
Converts a LocalDateTime to epoch milliseconds.- Parameters:
localDateTime- the LocalDateTime to convert- Returns:
- the epoch milliseconds representation of the given LocalDateTime
-
getInternationalTime
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
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 formatdate2- the second date in epoch format- Returns:
trueif date1 is the same day as date2,falseotherwise
-
parseTimeSpan
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 parseunit- the time unit to convert to- Returns:
- the time span in the specified time unit, or -1 if the format is invalid
-
toTimeSpan
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
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
Gets the current LocalDateTime based on the system's current time.- Returns:
- the current LocalDateTime
-
getLocalDateTime
Converts an epoch time to a LocalDateTime.- Parameters:
epochTime- the epoch time to convert- Returns:
- the corresponding LocalDateTime
-
getZonedDateTime
Converts an epoch time to a ZonedDateTime.- Parameters:
epochTime- the epoch time to convert- Returns:
- the corresponding ZonedDateTime
-
getLocalDate
Converts an epoch time to a LocalDate.- Parameters:
epochTime- the epoch time to convert- Returns:
- the corresponding LocalDate
-
getCurrentDayPassedTime
Calculates the time passed from the start of the current day in the specified time unit.- Parameters:
timeInMillis- the epoch time stamptimeUnit- 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 stamphour- 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 stamphour- the specified hourminute- 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 hourminute- 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 LocalDateTimehour- the specified hourminute- 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 stamphour- 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
Gets the epoch time for the start of the next specified day of the week.- Parameters:
timeInMillis- the epoch time stampdayOfWeek- 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 stamphour- 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 stamphour- the specified hourminute- 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 hourminute- 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 fromhour- the specified hourminute- 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
Gets the time in milliseconds to the next specified day of the week start.- Parameters:
timeInMillis- the epoch time stampdayOfWeek- the specified day of the week- Returns:
- the time in milliseconds to the next specified day of the week start
-
getTimeToNextDayStart
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 totimeUnit- the time unit to convert to- Returns:
- the time to the specified date time in the specified time unit
-
getTimeToNextHour
Gets the time in milliseconds to the next specified hour and minute.- Parameters:
hour- the specified hourminute- the specified minutetimeUnit- the time unit to convert to- Returns:
- the time to the next specified hour and minute in the specified time unit
-