Do we want to to validate the expiration date of the trusted certificate also? To check if the certificate is valid, you could use something like this instead:
```java
    try {
        if (trusted != null) {
            trusted.checkValidity();
            usingSelfSigned = false;
        }
    } catch (CertificateException ce) {
        // certificate is expired or not yet valid, wrap exception for outer catch
        throw new IOException(ce);
    }
```
Note that if you do this, it would make sense to swap the boolean condition as suggested above.