moalbait moalbait - 7 months ago 73
Java Question

Last Modified date in URL

I'm writing client socket code in java and i found this piece of code which is supposed to read a line and check last modified date. I am confused why does it need to subtract the length by 21 in the modDateArr? and is there any other way to do this.

while((x = br.readLine()) != null){
modDateArr = new char[x.length()-21];
x.getChars(20, x.length()-1, modDateArr, 0);
// create mod date string from last mod info
modDate = new String(modDateArr);


Yes there is a better way: use URL and URLConnection:

URL url = new URL("http://blablah/foo");
URLConnection connection = url.openConnection();
Date lastModified = new Date(connection.getLastModified());

Note that the Last-Modified header looks like this:

Last-Modified: Wed, 15 Nov 1995 04:58:08 GMT

If you remove the first 21 characters from that line like your code does, you get this: 15 Nov 1995 04:58:08 GMT.