In this post, we will see how to get last modified date of file in java. We can use java.io.File’s lastModified() method to get last modified date. This function returns time in milliseconds(long). We can convert this to required date format using SimpleDateFormat.
Java Program:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
package org.arpit.java2blog; import java.io.File; import java.text.SimpleDateFormat; public class GetLastModifiedTimeOfFileMain { public static void main(String[] args) { System.out.println("-----------------"); // Read the file File mp3File=new File("/Users/Arpit/Desktop/MySong.mp3"); System.out.println("Time inmilliseconds: "+mp3File.lastModified()); SimpleDateFormat sdf= new SimpleDateFormat("dd MMM yyyy HH:mm:sss"); String dateFormat=sdf.format(mp3File.lastModified()); System.out.println("Time in date format: "+dateFormat); System.out.println("-----------------"); } } |
1 2 3 4 5 6 |
----------------- Time in milliseconds: 1424326757000 Time in date format: 19 Feb 2015 11:49:017 ----------------- |
Was this post helpful?
Let us know if this post was helpful. Feedbacks are monitored on daily basis. Please do provide feedback as that\'s the only way to improve.