private Long getExpiration(URLConnection connection, long baseTime) {

        DateFormat PATTERN_RFC1123 = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss zzz"); //, Locale.US

        String cacheControl = connection.getHeaderField("Cache-Control");
        if (cacheControl != null) {
            java.util.StringTokenizer tok = new java.util.StringTokenizer(cacheControl, ",");
            while(tok.hasMoreTokens()) {
                String token = tok.nextToken().trim().toLowerCase();
                if ("must-revalidate".equals(token)) {
                    return new Long(0);
                }
                else if (token.startsWith("max-age")) {
                    int eqIdx = token.indexOf('=');
                    if (eqIdx != -1) {
                        String value = token.substring(eqIdx+1).trim();
                        int seconds;
                        try {
                            seconds = Integer.parseInt(value);
                            return new Long(baseTime + seconds * 1000);
                        }
                        catch(NumberFormatException nfe) {
                            System.err.println("getExpiration(): Bad Cache-Control max-age value: " + value);
                            // ignore
                        }
                    }
                }
            }
        }

        String expires = connection.getHeaderField("Expires");
        if (expires != null) {
            try {
                synchronized(PATTERN_RFC1123) {
                    java.util.Date expDate = PATTERN_RFC1123.parse(expires);
                    return new Long(expDate.getTime());
                }
            }
            catch(java.text.ParseException pe) {
                int seconds;
                try {
                    seconds = Integer.parseInt(expires);
                    return new Long(baseTime + seconds * 1000);
                }
                catch(NumberFormatException nfe) {
                    System.err.println("getExpiration(): Bad Expires header value: " + expires);
                }
            }
        }
        return null;
    }