001package com.nimbusds.jose.util; 002 003 004import java.util.Date; 005 006 007/** 008 * Date utilities. Use {@link com.nimbusds.jwt.util.DateUtils} instead. 009 */ 010@Deprecated 011public class DateUtils { 012 013 014 /** 015 * Converts the specified date object to a Unix epoch time in seconds. 016 * 017 * @param date The date. Must not be {@code null}. 018 * 019 * @return The Unix epoch time, in seconds. 020 */ 021 public static long toSecondsSinceEpoch(final Date date) { 022 023 return date.getTime() / 1000L; 024 } 025 026 027 /** 028 * Converts the specified Unix epoch time in seconds to a date object. 029 * 030 * @param time The Unix epoch time, in seconds. Must not be negative. 031 * 032 * @return The date. 033 */ 034 public static Date fromSecondsSinceEpoch(final long time) { 035 036 return new Date(time * 1000L); 037 } 038 039 040 /** 041 * Check if the specified date is after the specified reference, given 042 * the maximum accepted negative clock skew. 043 * 044 * <p>Formula: 045 * 046 * <pre> 047 * return date + clock_skew > reference 048 * </pre> 049 * 050 * Example: Ensure a JWT expiration (exp) timestamp is after the 051 * current time, with a minute of acceptable clock skew. 052 * 053 * <pre> 054 * boolean valid = DateUtils.isAfter(exp, new Date(), 60); 055 * </pre> 056 * 057 * @param date The date to check. Must not be 058 * {@code null}. 059 * @param reference The reference date (e.g. the current 060 * time). Must not be {@code null}. 061 * @param maxClockSkewSeconds The maximum acceptable negative clock 062 * skew of the date value to check, in 063 * seconds. 064 * 065 * @return {@code true} if the date is before the reference, plus the 066 * maximum accepted clock skew, else {@code false}. 067 */ 068 public static boolean isAfter(final Date date, 069 final Date reference, 070 final long maxClockSkewSeconds) { 071 072 return new Date(date.getTime() + maxClockSkewSeconds*1000L).after(reference); 073 } 074 075 076 /** 077 * Checks if the specified date is before the specified reference, 078 * given the maximum accepted positive clock skew. 079 * 080 * <p>Formula: 081 * 082 * <pre> 083 * return date - clock_skew < reference 084 * </pre> 085 * 086 * Example: Ensure a JWT issued-at (iat) timestamp is before the 087 * current time, with a minute of acceptable clock skew. 088 * 089 * <pre> 090 * boolean valid = DateUtils.isBefore(iat, new Date(), 60); 091 * </pre> 092 * 093 * @param date The date to check. Must not be 094 * {@code null}. 095 * @param reference The reference date (e.g. the current 096 * time). Must not be {@code null}. 097 * @param maxClockSkewSeconds The maximum acceptable clock skew of the 098 * date value to check, in seconds. 099 * 100 * @return {@code true} if the date is before the reference, minus the 101 * maximum accepted clock skew, else {@code false}. 102 */ 103 public static boolean isBefore(final Date date, 104 final Date reference, 105 final long maxClockSkewSeconds) { 106 107 return new Date(date.getTime() - maxClockSkewSeconds*1000L).before(reference); 108 } 109 110 111 /** 112 * Prevents instantiation. 113 */ 114 private DateUtils() { } 115}