Get the Last Modified date of an URL

前端 未结 3 1102
心在旅途
心在旅途 2020-12-15 05:08

I have three code. This is the first one in which I get the metadata information of any url and in that metadata I have LastModified date also. If I run this class then I ge

相关标签:
3条回答
  • 2020-12-15 05:16

    The last modified date should be in GMT (RFC 2822) so you should get get it like this:

    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
    Long dateTime = connection.getLastModified();
    connection.disconnect();
    ZonedDateTime urlLastModified = ZonedDateTime.ofInstant(Instant.ofEpochMilli(dateTime), ZoneId.of("GMT"));
    
    0 讨论(0)
  • 2020-12-15 05:29

    The first piece of code extracts the date from the metadata of the PDF file, while the two other ones get the information from the HTTP headers returned by the Web server. The first one is probably more accurate if you want to know when the document was created/modified.

    0 讨论(0)
  • 2020-12-15 05:35

    The best option is the third one - connection.getLastModified(), because it is the most easy-to-use method and has the highest level of abstraction. All the rest are on lower levels of abstraction: the first reads the raw response, and the second reads the raw header. The third reads the header and converts it to long.

    The difference between the outputs is due to the timezone. Using new Date() you use the VM default timezone. Prefer Calendar, or best - joda-time DateTime which support custom time zones.

    0 讨论(0)
提交回复
热议问题