Java file modified date


 

Java file modified date

In this section, you will learn how to find the modification date and time of the file.

In this section, you will learn how to find the modification date and time of the file.

Java file modified date

In this section, you will learn how to find the modification date and time of the file.

Description of code:

We have created an instance of File class and specify the file. Then, we have checked whether the specified file exists. It it exists then we have used lastModified() method that will return the modification date and time of the file. This return value is system dependent. The return value is of long type, so in order to display the date and time in a format, we have used SimpleDateFormat class.

exists()- This method File class checks whether the file or directory denoted by the pathname exists or not.

lastModified( )- this method of File class gives the last modification date and time of the specified file or folder name. This method shows the time in millisecond.

SimpleDateFormat- This class is used to formatting and parsing dates in a manner.

format()- This method of SimpleDateFormat class formats a date into a date/time string in a pattern.

Here is the code:

import java.io.*;
import java.util.*;
import java.text.*;

public class FileModifiedDate {
  public static void main(String[] argsthrows IOException {
    File file = new File("C:/file.txt");
    if (file.exists()) {
      long t = file.lastModified();
      Date d = new Date(t);
      SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
      System.out.println("Directory modification date and time : "
          + sdf.format(d));
    else {
      System.out.println("File not found!");
    }
  }
}

In the above code, we have used lastModified() method of File class to retrieve the last modification data and time of specified file. Through the above code, you can determine the modification date and time of any file or folder.

Output:

Directory modification date and time : 30-08-2010  12:21:52

Ads