To get creation time of file in java we can use the java.nio.file.Files API’s readAttributes() method.
Example : Get Creation Time of File in Java
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 28 |
package com.kscodes.sampleproject; 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; import java.nio.file.attribute.FileTime; public class FileCreationTime { public static void main(String[] args) { Path path = Paths.get("C:\\kscodes\\work\\test.txt"); try { BasicFileAttributes attrs = Files.readAttributes(path, BasicFileAttributes.class); FileTime fileTime = attrs.creationTime(); System.out.println("The Creation Time of File is ::" + fileTime); } catch (IOException e) { System.err.println("Error occurred"); } } } |
Output
1 |
The Creation Time of File is ::2015-11-22T13:45:14.03163Z |