public static String toPEMformat(X509Certificate certificate){
  StringWriter sw=new StringWriter();
  PEMWriter pemWriter=new PEMWriter(sw);
  try {
    pemWriter.writeObject(certificate);
    pemWriter.close();
    return sw.toString();
  }
 catch (  IOException x) {
    throw new RuntimeException("Failed to serialize certificate",x);
  }
}

public static String toPEMformat ( X509Certificate certificate ) { StringWriter sw = new StringWriter ( ) ; PEMWriter pemWriter = new PEMWriter ( sw ) ; try { pemWriter . writeObject ( certificate ) ; pemWriter . close ( ) ; return sw . toString ( ) ; } catch ( IOException x ) { throw new RuntimeException ( "Failed to serialize certificate" , x ) ; } }
serialize certificate in pem format
