Sunday, November 17, 2024
Google search engine
HomeLanguagesJavaJava Program to Get the Creation Time of a File

Java Program to Get the Creation Time of a File

Use java.nio package for extracting the creation date and time of any file through java. To extract the date and time of the file use BasicFileAttributes class. java.nio package helps us to get the creation time, last access time, and last modified time, it works for both file and directory.

Approach:

  1. Import the necessary java libraries.
  2. Store the path of the file whose creation time we want.
  3. Create the path object and specify the path of the file into it.
  4. Then we have to create BasicFileAttributes class using the readAttributes() method.
  5. In readAttributes() method we have to pass two parameters which are path object and BasicFileAttributes class.
  6. Now we have to just call creationTime() method using attributes.

Below is the implementation to get the creation time of the file:

Java




// Java program to get the creation time of a file
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.attribute.BasicFileAttributes;
 
public class JavafileCreationTime {
   
    public static void main() throws IOException
    {
        // storing the path of the file in the variable
        String filename
            = "C:/Users/HARDSOL/Desktop/New folder (2)";
       
        // creating the File class object
        File my_file = new File(filename);
       
        // creating the path object
        Path path = my_file.toPath();
       
        // creating BasicFileAttributes class object using
        // readAttributes method
        BasicFileAttributes file_att = Files.readAttributes(
            path, BasicFileAttributes.class);
       
        // printing the file creation time by calling
        // creationTime() method
        System.out.printf("File Creation Time %s%n ",
                          file_att.creationTime());
    }
}


Output:

Below is the implementation to get the creation time of the file in the SimpleDateFormat:

Java




// Java program to get the creation time of a file
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.attribute.BasicFileAttributes;
import java.text.SimpleDateFormat;
 
public class JavafileCreationTimee {
    public static void main() throws IOException
    {
        // storing the path of the file in the variable
        String filename
            = "C:/Users/HARDSOL/Desktop/New folder (2)";
       
        // creating the File class object
        File my_file = new File(filename);
       
        // creating the path object
        Path path = my_file.toPath();
       
        // creating BasicFileAttributes class object using
        // readAttributes method
        BasicFileAttributes file_att = Files.readAttributes(
            path, BasicFileAttributes.class);
       
        // creating simple date format object to make the
        // output more readable
        SimpleDateFormat sd
            = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
       
        System.out.print("File Creation Time: ");
       
        // converting time to milliseconds then specifying
        // the format in which we want the output
        System.out.print(
            sd.format(file_att.creationTime().toMillis()));
    }
}


Output: 

 

RELATED ARTICLES

Most Popular

Recent Comments