Class DateUtils


  • public class DateUtils
    extends java.lang.Object
    Date utilities.
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static java.util.Date fromSecondsSinceEpoch​(long time)
      Converts the specified Unix epoch time in seconds to a date object.
      static boolean isAfter​(java.util.Date date, java.util.Date reference, long maxClockSkewSeconds)
      Check if the specified date is after the specified reference, given the maximum accepted negative clock skew.
      static boolean isBefore​(java.util.Date date, java.util.Date reference, long maxClockSkewSeconds)
      Checks if the specified date is before the specified reference, given the maximum accepted positive clock skew.
      static boolean isWithin​(java.util.Date date, java.util.Date reference, long maxClockSkewSeconds)
      Checks if the specified date is within the specified reference, give or take the maximum accepted clock skew.
      static java.util.Date nowWithSecondsPrecision()
      Returns the current date, with the milliseconds removed.
      static long toSecondsSinceEpoch​(java.util.Date date)
      Converts the specified date object to a Unix epoch time in seconds.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Method Detail

      • nowWithSecondsPrecision

        public static java.util.Date nowWithSecondsPrecision()
        Returns the current date, with the milliseconds removed.
        Returns:
        The current date, with seconds precision.
      • toSecondsSinceEpoch

        public static long toSecondsSinceEpoch​(java.util.Date date)
        Converts the specified date object to a Unix epoch time in seconds.
        Parameters:
        date - The date. Must not be null.
        Returns:
        The Unix epoch time, in seconds.
      • fromSecondsSinceEpoch

        public static java.util.Date fromSecondsSinceEpoch​(long time)
        Converts the specified Unix epoch time in seconds to a date object.
        Parameters:
        time - The Unix epoch time, in seconds. Must not be negative.
        Returns:
        The date.
      • isAfter

        public static boolean isAfter​(java.util.Date date,
                                      java.util.Date reference,
                                      long maxClockSkewSeconds)
        Check if the specified date is after the specified reference, given the maximum accepted negative clock skew.

        Formula:

         return date + clock_skew > reference
         
        Example: Ensure a JWT expiration (exp) timestamp is after the current time, with a minute of acceptable clock skew.
         boolean valid = DateUtils.isAfter(exp, new Date(), 60);
         
        Parameters:
        date - The date to check. Must not be null.
        reference - The reference date (e.g. the current time). Must not be null.
        maxClockSkewSeconds - The maximum acceptable negative clock skew of the date value to check, in seconds.
        Returns:
        true if the date is before the reference, plus the maximum accepted clock skew, else false.
      • isBefore

        public static boolean isBefore​(java.util.Date date,
                                       java.util.Date reference,
                                       long maxClockSkewSeconds)
        Checks if the specified date is before the specified reference, given the maximum accepted positive clock skew.

        Formula:

         return date - clock_skew < reference
         
        Example: Ensure a JWT issued-at (iat) timestamp is before the current time, with a minute of acceptable clock skew.
         boolean valid = DateUtils.isBefore(iat, new Date(), 60);
         
        Parameters:
        date - The date to check. Must not be null.
        reference - The reference date (e.g. the current time). Must not be null.
        maxClockSkewSeconds - The maximum acceptable clock skew of the date value to check, in seconds.
        Returns:
        true if the date is before the reference, minus the maximum accepted clock skew, else false.
      • isWithin

        public static boolean isWithin​(java.util.Date date,
                                       java.util.Date reference,
                                       long maxClockSkewSeconds)
        Checks if the specified date is within the specified reference, give or take the maximum accepted clock skew.
        Parameters:
        date - The date to check. Must not be null.
        reference - The reference date (e.g. the current time). Must not be null.
        maxClockSkewSeconds - The maximum acceptable clock skew of the date value to check, in seconds.
        Returns:
        true if the date is within the reference, give or take the maximum accepted clock skew, else false.