Reputation: 67
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){
if(x.contains("Last-Modified:")){
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);
break;
}
}
Upvotes: 2
Views: 891
Reputation: 31299
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
.
Upvotes: 3