Package com.nimbusds.jwt.util
Class DateUtils
java.lang.Object
com.nimbusds.jwt.util.DateUtils
Date utilities.
-
Method Summary
Modifier and TypeMethodDescriptionstatic Date
fromSecondsSinceEpoch
(long time) Converts the specified seconds since the Unix epoch to aDate
.static boolean
Check if the specifiedDate
is after the specified reference, given the maximum accepted negative clock skew.static boolean
Checks if the specifiedDate
is before the specified reference, given the maximum accepted positive clock skew.static boolean
Checks if the specifiedDate
is within the specified reference, give or take the maximum accepted clock skew.static Date
Returns the currentDate
, with the milliseconds removed.static long
toSecondsSinceEpoch
(Date date) Converts the specifiedDate
to seconds since the Unix epoch.
-
Method Details
-
nowWithSecondsPrecision
Returns the currentDate
, with the milliseconds removed.- Returns:
- The current
Date
, with seconds precision.
-
toSecondsSinceEpoch
Converts the specifiedDate
to seconds since the Unix epoch.- Parameters:
date
- TheDate
. Must not benull
.- Returns:
- The seconds since the Unix epoch.
-
fromSecondsSinceEpoch
Converts the specified seconds since the Unix epoch to aDate
.- Parameters:
time
- The seconds since the Unix epoch. Must not be negative.- Returns:
- The
Date
.
-
isAfter
Check if the specifiedDate
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
- TheDate
to check. Must not benull
.reference
- The referenceDate
(e.g. the current time). Must not benull
.maxClockSkewSeconds
- The maximum acceptable negative clock skew of the date value to check, in seconds.- Returns:
true
if theDate
is before the reference, plus the maximum accepted clock skew, elsefalse
.
-
isBefore
Checks if the specifiedDate
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
- TheDate
to check. Must not benull
.reference
- The referenceDate
(e.g. the current time). Must not benull
.maxClockSkewSeconds
- The maximum acceptable clock skew of the date value to check, in seconds.- Returns:
true
if theDate
is before the reference, minus the maximum accepted clock skew, elsefalse
.
-
isWithin
Checks if the specifiedDate
is within the specified reference, give or take the maximum accepted clock skew.- Parameters:
date
- TheDate
to check. Must not benull
.reference
- The referenceDate
(e.g. the current time). Must not benull
.maxClockSkewSeconds
- The maximum acceptable clock skew of the date value to check, in seconds.- Returns:
true
if theDate
is within the reference, give or take the maximum accepted clock skew, elsefalse
.
-