How do I get the last modification time of a Java resource?

Aaron Digulla picture Aaron Digulla · Jan 13, 2010 · Viewed 11.9k times · Source

Can someone please tell me a reliable way to get the last modification time of a Java resource? The resource can be a file or an entry in a JAR.

Answer

jarnbjo picture jarnbjo · Jan 13, 2010

If you with "resource" mean something reachable through Class#getResource or ClassLoader#getResource, you can get the last modified time stamp through the URLConnection:

URL url = Test.class.getResource("/org/jdom/Attribute.class");
System.out.println(new Date(url.openConnection().getLastModified()));

Be aware that getLastModified() returns 0 if last modified is unknown, which unfortunately is impossible to distinguish from a real timestamp reading "January 1st, 1970, 0:00 UTC".