How to get file last modified date in java

In this section , you are going to learn how to get the file last modification date and time using java io API

file.lastModified() method return the long value which denotes the time when the file was last modified. Here we are converting that long value into date and converting it into proper date and time format.Refer the below example.


import java.text.SimpleDateFormat;

public class LastModifiedDateExample {
    public static void main(String[] args) {
        File file = new File("c:\\test\\myfile.txt");
        SimpleDateFormat sdf = new SimpleDateFormat("MM-dd-yyyy HH:mm:ss");
        System.out.println("Modified Date :- " + sdf.format(file.lastModified()));

Note:Program output is vary base on your input file last modification date.


Modified Date :- 06-05-2012 15:05:11