What’s the Time Stamp of a File?

What’s the Time Stamp of a File?

JDK 1.1.x’s class has a lastModified():long method. You can use the value returned by lastModified method to create an instance of java.util.Date class and, thus, get a hold of date/time of last modifications for a file.Here is some sample code that prints the last modification of files in current user directory:

 public void printCurDirContent() { String path = "."; File f = new File(path); String[] s = f.list(); for(int i=0; i

JDK's documentation mentions that the return value of lastModified() method of the File class is "system-dependent and should only be used to compare with other values returned by last modified". So if you use to compare time/date stamps of files, make sure that you execute the method on your files on the same system.

As of JDK 1.2, a new method, setLastModified(:long):boolean, has been added to java.util.File class. You can use this method to change the last modification time of a file. Here is some code showing how:

 public static void touch(String filePath) { long timeNow = System.currentTimeMillis(); File f = new File(filePath); boolean touched = f.setLastModified(timeNow); if (!touched) { System.err.println("Failed to touch: "+ filePath); } } 

Note that the return value of lastModified method and the parameter value of setLastModified(...) method are times measured in milliseconds since January 1, 1970 00:00:00 GMT.

See also  5 Benefits of Using Time Tracking Software

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist