Java Program to Get Last Modification Date of a File


Last Modification Date of a File refers to the last date on which the file data is edited. We have various functions in java to find the last modification date of a file. In this section, we will be discussing different approaches of implementing a java program to get the last modification date of a file.

File is a collection of information which may contain data such as text information, images, audio, videos, program code. These can be accessed by any software applications to perform actions like read, write, update, delete etc.

We will now look in to each function that we will be using in our further examples below.

Creating a file class object −

File file = new File("file_path");

exists() − This method checks whether the file exists or not and returns boolean value.

File file = new File("/example.txt");
if (file.exists()) {
  System.out.println("File exists.");
}

Creating a path class object

Path path = Paths.get("path_string");

Create a SimpleDateFormat class object

SimpleDateFormat formatter = new SimpleDateFormat("pattern");

lastModified() − This method is used to return the time of last modified file.It is present in java.io package. It takes file object as input.

File file = new File("path/to/my/file.txt");
long lastModified = file.lastModified();

toMillis() − This method converts FileTime object to milliseconds. It is present in java.nio.file.attribute package.

Path file = Path.of("path/to/my/file.txt");
FileTime lastModifiedTime = Files.getLastModifiedTime(file);
long millis = lastModifiedTime.toMillis();

getLastModifiedTime() − This method is used to return the time of last modified file by taking path object as parameter.

Path file = Path.of("path/to/my/file.txt");
FileTime lastModifiedTime = Files.getLastModifiedTime(file);

format() − This methods converts date to string using specified format. It accepts date as a parameter and returns string. This function is called on SimpleDateFormat object.

Date now = new Date();
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String formattedDate = formatter.format(now);

Now, we will be into different code implementations in java for finding the last modified date of a file.

Approach 1: Using java.io package

In this approach, we will be implementing java code to get last modified date of a file using inbuilt File class and in java.io package.

Steps

  • Create a file object using File class.

  • Check if file exists using exists() method and if the file exists get last modified time using lastModified() method.

  • Create date object using Date class using last modified time.

  • Create SimpleDateFormat object and format the date.

  • Print the date.

  • If file does not exist,print File not found.

Example

In this example, we created a file object using file class constructor and checked whether if the file exists using ‘exists()’ method and if the file exists then we used the lastModified() method on fileobject which returns last modified time in milliseconds. Then we use the Date constructor and pass the lastModified value which we stored in ‘lastModified’ variable to the date constructor and get the date object. We have then created a SimpleDateFormat object using SimpleDateFormat constructor by passing any specific date format as a parameter (in this case it is "dd/MM/yyyy HH:mm:ss" )and create the format object. Now, using the SimpleDateFormat object we will use format() function and pass date object to get the formatted date as output.

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Main {
   public static void main(String[] args) {
      File file = new File("C:/example.txt");
      if (file.exists()) {
         long lastModified = file.lastModified();
         Date date = new Date(lastModified);
         SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
         String formattedDate = formatter.format(date);
         System.out.println("Last modified date of file: " + formattedDate);
      } else {
         System.out.println("File not found.");
      }
   }
}

Output

Last modified date of file: 28/02/2023 16:32:45

Approach 2: Using java.nio package

In this approach, we will be implementing java code to get last modified date of a file using inbuilt Path class and in java.nio package.

Steps

  • Import all the required java libraries.

  • Create a path object using Path class.

  • Use the Files class use the getLastModifiedTime() function and pass the path object created and find the last modified time.

  • Create date object using Date class using last modified time.

  • Create SimpleDateFormat object and format the date.

  • Print the date.

  • If file does not exist, print File not found.

Example

In this example, we created a path object using path class constructor and then we used the lastModified() method on path object which returns last modified time in milliseconds . Then we use the Date constructor and pass the lastModified value which we stored in ‘lastModified’ variable to the date constructor and get the date object. We have then created a SimpleDateFormat object using SimpleDateFormat constructor by passing any specific date format as a parameter (in this case it is "dd/MM/yyyy HH:mm:ss" )and create the format object. Now, using the SimpleDateFormat object we will use format() function and pass date object to get the formatted date as output.

import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.attribute.FileTime;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.io.IOException;

public class Main {
   public static void main(String[] args) {
      Path file = Path.of("path/to/my/file.txt");
      try {
         FileTime lastModifiedTime = Files.getLastModifiedTime(file);
         Date date = new Date(lastModifiedTime.toMillis());
         SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
         String formattedDate = formatter.format(date);
         System.out.println("Last modified date: " + formattedDate);
      } catch (IOException e) {
         System.out.println("File not found.");
      }
   }
}

Output

Last modified date of file: 28/02/2023 16:32:45

Thus, in this article we have discussed different approaches to get the last modification of a file using Java Programming Language.

Updated on: 16-Aug-2023

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements