Java Certificate Gets the start date of the validity period of the given certificate.

PreviousNext

Gets the start date of the validity period of the given certificate.

Parameter:

  • certificate the given certificate

Return:

the start date of the validity period, or null if the certificate isn't an X.509 certificate

//package com.java2s;
import java.security.cert.Certificate;

import java.security.cert.X509Certificate;

import java.util.Date;

public class Main {
    /**//   w  w   w  . d  e  m   o2   s .   c  o m  
     * Gets the start date of the validity period of the given certificate.
     * 
     * @param certificate the given certificate
     * 
     * @return the start date of the validity period, or <code>null</code> if the certificate isn't an X.509 certificate
     * @throws IllegalArgumentException if the certificate is <code>null</code>
     */
    public static Date getStartDate(Certificate certificate) {
        checkCertificate(certificate);

        if (X509Certificate.class.isAssignableFrom(certificate.getClass()))
            return ((X509Certificate) certificate).getNotBefore();

        return null;
    }

    /**
     * Checks that the given certificate is not <code>null</code>.
     * 
     * @param certificate the given certificate
     * @throws IllegalArgumentException if certificate is <code>null</code>
     */
    public static void checkCertificate(Certificate certificate) {
        if (certificate == null)
            throw new IllegalArgumentException("certificate must not be null!");
    }
}
PreviousNext

Related