Shashi Shirke
Shashi Shirke

Reputation: 93

how to get last-modified date from header to String

I have got the Header values in Header Object. but I need "Last-Modified" into the string object for comparison. Please could you tell me how should I get the last header into the string.

HttpClient client = new DefaultHttpClient();
//HttpGet get = new HttpGet(url);
HttpHead method = new HttpHead(url);
HttpResponse response = client.execute(method);
Header[] s = response.getAllHeaders();

String sh = String.valueOf(s);
System.out.println("The value of sh:"+sh);

System.out.println("The header from the httpclient:");

for (int i = 0; i < s.length; i++) {
  Header hd = s[i];
  System.out.println("Header Name: "+hd.getName() + "       " + " Header Value: " +  hd.getValue());
}

String last-modified =   // here I need to convert this header(last-modified);

Upvotes: 1

Views: 6715

Answers (3)

warship
warship

Reputation: 11

private String getLastModifiedDate(HttpResponse response) {
    Header header = response.getFirstHeader("Date");
    if (header != null) {
        return header.getValue();
    }

    return "";
}

Upvotes: 0

Premek Brada
Premek Brada

Reputation: 11

In many circumstances, you get just one Last-Modified header, so you could simply use:

String lastModified = response.getHeader("last-modified");
if (lastModified != null) {    // in case the header isn't set
  // do something
}

For multiple values, the JavaDoc says: If a response header with the given name exists and contains multiple values, the value that was added first will be returned.

Upvotes: 1

michael667
michael667

Reputation: 3260

Try something like this:

Header[] s = response.getHeaders("last-modified");
String lastModified = s[0].getValue();   // ! There might be more than 1 header
                                         // ! or none at all

Upvotes: 0

Related Questions