Determine File Creation Date Java

Determine File Creation Date Java

In this sample java class you will see how to determine file creation date using Java program. We will use JDK 7 java.nio pakcage to determine creation date:

  • Sample Java Program:
package com.javahonk;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.attribute.BasicFileAttributes;

public class DetermineFileCreationDate {
	
	public static void main(String[] args) {
		
		try {
			Path file = Paths.get("C:\\JavaHonk\\Log4jXML.log");;
			BasicFileAttributes basicFileAttributes = Files.readAttributes(file, BasicFileAttributes.class);
			System.out.println("File Creation Time: " + basicFileAttributes.creationTime().toString());
			System.out.println("File Last Access Time: " + basicFileAttributes.lastAccessTime());
			System.out.println("File Last Modified Time: " + basicFileAttributes.lastModifiedTime());
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}
  • Output:

Determine File Creation Date Java

  • For more information about java.nio package please refer this oracle link

Leave a Reply

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