What’s the Time Stamp of a File?

What’s the Time Stamp of a File?

JDK 1.1.x’s java.io.File 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.

Share the Post:
Heading photo, Metadata.

What is Metadata?

What is metadata? Well, It’s an odd concept to wrap your head around. Metadata is essentially the secondary layer of data that tracks details about the “regular” data. The regular

XDR solutions

The Benefits of Using XDR Solutions

Cybercriminals constantly adapt their strategies, developing newer, more powerful, and intelligent ways to attack your network. Since security professionals must innovate as well, more conventional endpoint detection solutions have evolved

AI is revolutionizing fraud detection

How AI is Revolutionizing Fraud Detection

Artificial intelligence – commonly known as AI – means a form of technology with multiple uses. As a result, it has become extremely valuable to a number of businesses across

AI innovation

Companies Leading AI Innovation in 2023

Artificial intelligence (AI) has been transforming industries and revolutionizing business operations. AI’s potential to enhance efficiency and productivity has become crucial to many businesses. As we move into 2023, several