views:

80

answers:

3

Any Java API in client side can check its modified date?

+1  A: 

You can use the lastModified method in java.io.File to find out the last time a file was modified.

Laurence Gonsalves
Sorry I mean in client side. The question is revised.
developer.cyrus
+2  A: 

Client side means what exactly? Are you getting the file from the server via HttpClient or something similar? If so, you need to look at the HTTP headers (Last-Modified in particular). Please clarify.

ChssPly76
Sorry I mean in client side. The question is revised.
developer.cyrus
A: 

You can use HttpURLConnection to check the Last-Modified value on a page, assuming the server returns one.

This request uses the HTTP HEAD method to return only the headers for the resource:

URL url = new URL(
    "http://en.wikipedia.org/wiki/Main_Page");
HttpURLConnection httpConnection = (HttpURLConnection) url
    .openConnection();
httpConnection.setRequestMethod("HEAD");
httpConnection.connect();
long lastModified = httpConnection.getLastModified();
if (lastModified != 0) {
  System.out.println(new Date(lastModified));
} else {
  System.out.println("Last-Modified not returned");
}
httpConnection.disconnect();

// TODO: error handling

HttpURLConnection is adequate for some things, but if you want a more rounded API, have a look at Apache HttpComponents.

McDowell