Class DateUtil
java.lang.Object
me.josephsf.jobportaljosephsfeir.util.DateUtil
Utility class for date and time operations in the Job Portal system.
This class provides static methods for common date-related operations such as formatting, parsing, comparing, and manipulating dates and times. It uses Java's modern date-time API (java.time package) for all operations.
The class is final with a private constructor to prevent instantiation, as it only contains static utility methods.
- Since:
- 2025-05-15
-
Method Summary
Modifier and TypeMethodDescriptionstatic LocalDateAdds a specified number of days to a date.static LocalDateAdds a specified number of months to a date.static LocalDateAdds a specified number of years to a date.static longdaysBetween(LocalDate startDate, LocalDate endDate) Calculates the number of days between two dates.static StringformatDate(LocalDate date) Formats a LocalDate to a string using the standard date format.static StringformatDateTime(LocalDateTime dateTime) Formats a LocalDateTime to a string using the standard date-time format.static LocalDateGets the current date.static LocalDateTimeGets the current date and time.static LocalDateTimegetDateTimeAgo(int days) Gets a date-time that was a specified number of days ago.static LocalDategetDaysAgo(int days) Gets a date that was a specified number of days ago.static booleanChecks if a date is in the future.static booleanChecks if a date is in the past.static LocalDateParses a string into a LocalDate using the standard date format.static LocalDateTimeparseDateTime(String dateTimeString) Parses a string into a LocalDateTime using the standard date-time format.static longyearsBetween(LocalDate startDate, LocalDate endDate) Calculates the number of years between two dates.
-
Method Details
-
formatDate
Formats a LocalDate to a string using the standard date format.- Parameters:
date- The LocalDate to format- Returns:
- The formatted date string, or null if the input is null
-
formatDateTime
Formats a LocalDateTime to a string using the standard date-time format.- Parameters:
dateTime- The LocalDateTime to format- Returns:
- The formatted date-time string, or null if the input is null
-
parseDate
Parses a string into a LocalDate using the standard date format.- Parameters:
dateString- The string to parse- Returns:
- The parsed LocalDate, or null if the input is null or empty
- Throws:
DateTimeParseException- if the text cannot be parsed
-
parseDateTime
Parses a string into a LocalDateTime using the standard date-time format.- Parameters:
dateTimeString- The string to parse- Returns:
- The parsed LocalDateTime, or null if the input is null or empty
- Throws:
DateTimeParseException- if the text cannot be parsed
-
getCurrentDate
Gets the current date.- Returns:
- The current date as a LocalDate
-
getCurrentDateTime
Gets the current date and time.- Returns:
- The current date and time as a LocalDateTime
-
daysBetween
Calculates the number of days between two dates.- Parameters:
startDate- The start dateendDate- The end date- Returns:
- The number of days between the dates, or 0 if either date is null
-
yearsBetween
Calculates the number of years between two dates.- Parameters:
startDate- The start dateendDate- The end date- Returns:
- The number of years between the dates, or 0 if either date is null
-
isPast
Checks if a date is in the past.- Parameters:
date- The date to check- Returns:
- true if the date is in the past, false if it's today, in the future, or null
-
isFuture
Checks if a date is in the future.- Parameters:
date- The date to check- Returns:
- true if the date is in the future, false if it's today, in the past, or null
-
addDays
Adds a specified number of days to a date.- Parameters:
date- The base datedays- The number of days to add- Returns:
- The resulting date, or null if the input date is null
-
addMonths
Adds a specified number of months to a date.- Parameters:
date- The base datemonths- The number of months to add- Returns:
- The resulting date, or null if the input date is null
-
addYears
Adds a specified number of years to a date.- Parameters:
date- The base dateyears- The number of years to add- Returns:
- The resulting date, or null if the input date is null
-
getDaysAgo
Gets a date that was a specified number of days ago.- Parameters:
days- The number of days ago- Returns:
- The date that occurred the specified number of days before the current date
-
getDateTimeAgo
Gets a date-time that was a specified number of days ago.- Parameters:
days- The number of days ago- Returns:
- The date-time that occurred the specified number of days before the current date-time
-