How to get last modified date of file in java

In this post, we will see how to get last modified date of file in java. We can use'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: 

package org.arpit.java2blog;

import java.text.SimpleDateFormat;

public class GetLastModifiedTimeOfFileMain {

	public static void main(String[] args)
		// 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);

When you run above program, you will get below output:
Time in milliseconds: 1424326757000
Time in date format: 19 Feb 2015 11:49:017

Written by Arpit:

If you have read the post and liked it. Please connect with me on Facebook | Twitter | Google Plus


Java tutorial for beginners Copyright © 2012