title: how to check SSL certificate expiration date programmatically in Java
body: <p>I need to extract expiration date from SSL certificate on web site in Java,should support both 
  trusted and self-signed certificate,such as:
  1.trusted
  <a href="https://github.com" rel="noreferrer">https://github.com</a>
  2.self-signed
  <a href="https://mms.nw.ru/" rel="noreferrer">https://mms.nw.ru/</a></p>
  
  <p>I already copy some code as:</p>
  
  <pre><code>import java.net.URL;
  import java.security.SecureRandom;
  import java.security.cert.Certificate;
  import java.security.cert.CertificateException;
  import java.security.cert.X509Certificate;
  import javax.net.ssl.HostnameVerifier;
  import javax.net.ssl.HttpsURLConnection;
  import javax.net.ssl.KeyManager;
  import javax.net.ssl.SSLContext;
  import javax.net.ssl.SSLSession;
  import javax.net.ssl.TrustManager;
  import javax.net.ssl.X509TrustManager;
  
  public class SSLTest {
  
      public static void main(String [] args) throws Exception {
          // configure the SSLContext with a TrustManager
          SSLContext ctx = SSLContext.getInstance("TLS");
          ctx.init(new KeyManager[0], new TrustManager[] {new DefaultTrustManager()}, new SecureRandom());
          SSLContext.setDefault(ctx);
  
          URL url = new URL("https://github.com");//https://mms.nw.ru
          HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
          conn.setHostnameVerifier(new HostnameVerifier() {
              @Override
              public boolean verify(String arg0, SSLSession arg1) {
                  return true;
              }
          });
          System.out.println(conn.getResponseCode());
          Certificate[] certs = conn.getServerCertificates();
          for (Certificate cert :certs){
              System.out.println(cert.getType());
              System.out.println(cert);
          }
  
          conn.disconnect();
      }
  
      private static class DefaultTrustManager implements X509TrustManager {
  
          @Override
          public void checkClientTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {}
  
          @Override
          public void checkServerTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {}
  
          @Override
          public X509Certificate[] getAcceptedIssuers() {
              return null;
          }
      }
  }
  </code></pre>
  
  <p>The questions are:</p>
  
  <ol>
  <li><p>How to parse the expiration date from the certificate, in my code the toString() did output the date,but it is hard to parse.</p></li>
  <li><p>How to determine the certificate chain, eg, the github certificate with chains 3, how did i know which certificate to get the expiration date from?</p></li>
  </ol>