Class DateUtils

java.lang.Object
com.nimbusds.jwt.util.DateUtils

public class DateUtils extends Object
Date utilities.
  • Method Summary

    Modifier and Type
    Method
    Description
    static Date
    Converts the specified seconds since the Unix epoch to a Date.
    static boolean
    isAfter(Date date, Date reference, long maxClockSkewSeconds)
    Check if the specified Date is after the specified reference, given the maximum accepted negative clock skew.
    static boolean
    isBefore(Date date, Date reference, long maxClockSkewSeconds)
    Checks if the specified Date is before the specified reference, given the maximum accepted positive clock skew.
    static boolean
    isWithin(Date date, Date reference, long maxClockSkewSeconds)
    Checks if the specified Date is within the specified reference, give or take the maximum accepted clock skew.
    static Date
    Returns the current Date, with the milliseconds removed.
    static long
    Converts the specified Date to seconds since the Unix epoch.

    Methods inherited from class java.lang.Object

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

    • nowWithSecondsPrecision

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

      public static long toSecondsSinceEpoch(Date date)
      Converts the specified Date to seconds since the Unix epoch.
      Parameters:
      date - The Date. Must not be null.
      Returns:
      The seconds since the Unix epoch.
    • fromSecondsSinceEpoch

      public static Date fromSecondsSinceEpoch(long time)
      Converts the specified seconds since the Unix epoch to a Date.
      Parameters:
      time - The seconds since the Unix epoch. Must not be negative.
      Returns:
      The Date.
    • isAfter

      public static boolean isAfter(Date date, 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(Date date, 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(Date date, 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.