Categories
file java

get the last modified date of a file in Java

In this post, we will learn about get the last modified date of a file in Java

It returns the value in long format,

we need to use SimpleDateFormat to convert into date format

Syntax
file.lastModified();
Example

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

public class LastModifedDate {

	public static void main(String[] args) {
		try {
			File file = new File("C:\\Project.log");
		
			if (file.exists()) {
				long lastModified = file.lastModified();

				SimpleDateFormat sdf = new SimpleDateFormat("MM-dd-yyyy HH:mm:ss");
				System.out.println(sdf.format(lastModified));

			} else {
				System.err.println("File path is not correct ");
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
Github

https://github.com/rkumar9090/BeginnersBug/blob/master/BegineersBug/src/com/geeks/example/files/LastModifedDate.java

Related Articles

how to read a file from java with example

Leave a Reply

Your email address will not be published. Required fields are marked *