How to get the file last modified date in Java
Written on
January 10, 2010 at 8:24 pm by
mkyong
You can get the last modified date from a file with lastModified(), it will return a long type, you have to do some date formatting before display it.
Example
This is an example to get the last modified date from a file named “c:\\spy.log”, and format the date with SimpleDateFormat.
package com.mkyong.io; import java.io.File; import java.text.SimpleDateFormat; import java.util.Date; public class FileApp{ public static void main (String args[]) { SimpleDateFormat sdf = new SimpleDateFormat("d/M/yyyy hh:mm:ss"); File file = new File("c:\\spy.log"); if(file.exists()){ long time = file.lastModified(); System.out.println("Time before format : " + time); System.out.println("File name : " + file.getName()); System.out.println("Last modification date and time : " + sdf.format(new Date(time))); } } }
Output
Time before format : 1262227149328 File name : spy.log Last modification date and time : 31/12/2009 10:39:09

