001/* 002 * The contents of this file are subject to the terms of the Common Development and 003 * Distribution License (the License). You may not use this file except in compliance with the 004 * License. 005 * 006 * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the 007 * specific language governing permission and limitations under the License. 008 * 009 * When distributing Covered Software, include this CDDL Header Notice in each file and include 010 * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL 011 * Header, with the fields enclosed by brackets [] replaced by your own identifying 012 * information: "Portions copyright [year] [name of copyright owner]". 013 * 014 * Copyright 2013-2015 ForgeRock AS. 015 */ 016 017package org.forgerock.json.jose.utils; 018 019import java.util.Date; 020 021import org.forgerock.json.jose.exceptions.JwtRuntimeException; 022 023/** 024 * This class provides utility methods for converting Java Date objects into and from IntDates. 025 * <p> 026 * Where an IntDate is a JSON numeric value representing the number of seconds from 1970-01-01T0:0:0Z UTC until the 027 * specified UTC date/time. 028 * 029 * @see <a href="http://tools.ietf.org/html/draft-jones-json-web-token-10#section-2">IntDate</a> 030 * 031 * @since 2.0.0 032 */ 033public final class IntDate { 034 035 /** 036 * Private constructor. 037 */ 038 private IntDate() { 039 } 040 041 /** 042 * Converts a Java Date object into an IntDate. 043 * <p> 044 * <strong>Note:</strong> Precision is lost in this conversion as the milliseconds are dropped as a part of the 045 * conversion. 046 * 047 * @param date The Java Date to convert. 048 * @return The IntDate representation of the Java Date. 049 */ 050 public static long toIntDate(Date date) { 051 if (date == null) { 052 throw new JwtRuntimeException("Null date cannot be converted to IntDate"); 053 } 054 return date.getTime() / 1000L; 055 } 056 057 /** 058 * Converts an IntDate into a Java Date object. 059 * <p> 060 * <strong>Note:</strong> Milliseconds are set to zero on the converted Java Date as an IntDate does not hold 061 * millisecond information. 062 * 063 * @param intDate The IntDate to convert. 064 * @return The Java Date representation of the IntDate. 065 */ 066 public static Date fromIntDate(long intDate) { 067 return new Date(intDate * 1000L); 068 } 069}