java files

File.lastModified() to Date Java Code With Examples

Table of Contents

Program

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Calendar;
public class FileLastModifiedDate
{
    public static void main(String[] args)
    {	       
	File file = new File("C:\\LICENSE-2.0.txt");
        SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy HH:mm");
	System.out.println("Last Modified Date: " + sdf.format(file.lastModified()));
 

         File file = new File("C:\\technicalkeeda\\java.txt");
        SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
        System.out.println("Last Modified Date:- " + sdf.format(file.lastModified()));
 
        Calendar calendar = Calendar.getInstance();
        file.setLastModified(calendar.getTimeInMillis());
 
        System.out.println("Latest Modified Date:- " + sdf.format(file.lastModified()));
}

}

Output

      Last Modified Date: 01/30/2021 11:05

      Last Modified Date:- 03/01/2018 13:05:55
 
      Latest Modified Date:- 04/01/2018 18:06:30

Description

public boolean setLastModified​(long time)

Sets the last-modified time of the file or directory named by this abstract pathname.
All platforms support file-modification times to the nearest second, but some provide more precision
The argument will be truncated to fit the supported precision
If the operation succeeds and no intervening operations on the file take place,
then the next invocation of the lastModified() method will return the (possibly truncated)
time argument that was passed to this method.