Class DateUtil

java.lang.Object
me.josephsf.jobportaljosephsfeir.util.DateUtil

public final class DateUtil extends Object
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 Details

    • formatDate

      public static String formatDate(LocalDate date)
      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

      public static String formatDateTime(LocalDateTime dateTime)
      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

      public static LocalDate parseDate(String dateString)
      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

      public static LocalDateTime parseDateTime(String dateTimeString)
      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

      public static LocalDate getCurrentDate()
      Gets the current date.
      Returns:
      The current date as a LocalDate
    • getCurrentDateTime

      public static LocalDateTime getCurrentDateTime()
      Gets the current date and time.
      Returns:
      The current date and time as a LocalDateTime
    • daysBetween

      public static long daysBetween(LocalDate startDate, LocalDate endDate)
      Calculates the number of days between two dates.
      Parameters:
      startDate - The start date
      endDate - The end date
      Returns:
      The number of days between the dates, or 0 if either date is null
    • yearsBetween

      public static long yearsBetween(LocalDate startDate, LocalDate endDate)
      Calculates the number of years between two dates.
      Parameters:
      startDate - The start date
      endDate - The end date
      Returns:
      The number of years between the dates, or 0 if either date is null
    • isPast

      public static boolean isPast(LocalDate date)
      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

      public static boolean isFuture(LocalDate date)
      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

      public static LocalDate addDays(LocalDate date, long days)
      Adds a specified number of days to a date.
      Parameters:
      date - The base date
      days - The number of days to add
      Returns:
      The resulting date, or null if the input date is null
    • addMonths

      public static LocalDate addMonths(LocalDate date, long months)
      Adds a specified number of months to a date.
      Parameters:
      date - The base date
      months - The number of months to add
      Returns:
      The resulting date, or null if the input date is null
    • addYears

      public static LocalDate addYears(LocalDate date, long years)
      Adds a specified number of years to a date.
      Parameters:
      date - The base date
      years - The number of years to add
      Returns:
      The resulting date, or null if the input date is null
    • getDaysAgo

      public static LocalDate getDaysAgo(int days)
      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

      public static LocalDateTime getDateTimeAgo(int days)
      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