How to get the file creation date in Java

There are no official way to get the file creation date in Java. However, you can use the following workaround to get the file creation date in Windows platform.

How it work

In Windows command prompt, type the command to list the file creation date.

C:\>cmd /c dir c:\logfile.log /tc
 Volume in drive C has no label.
 Volume Serial Number is 0410-1EC3
 
 Directory of c:\
 
31/05/2010  08:05                14 logfile.log
               1 File(s)             14 bytes
               0 Dir(s)  35,389,460,480 bytes free

The “31/05/2010 08:05” is what you need. The idea is use the Java “Runtime.getRuntime().exec” to execute the above command, hold the output, and parse it by lines until you get the date and time.

Example

In this example, it will get the creation date of file (c:\\logfile.log).

package com.mkyong.file;
 
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
 
public class GetFileCreationDateExample
{
    public static void main(String[] args)
    {	
 
    	try{
 
    		Process proc = 
    		   Runtime.getRuntime().exec("cmd /c dir c:\\logfile.log /tc");
 
    		BufferedReader br = 
    		   new BufferedReader(
    		      new InputStreamReader(proc.getInputStream()));
 
    		String data ="";
 
    		//it's quite stupid but work
    		for(int i=0; i<6; i++){
    			data = br.readLine();
    		}
 
    		System.out.println("Extracted value : " + data);
 
    		//split by space
    		StringTokenizer st = new StringTokenizer(data);
    		String date = st.nextToken();//Get date
    		String time = st.nextToken();//Get time
 
    		System.out.println("Creation Date  : " + date);
    		System.out.println("Creation Time  : " + time);
 
    	}catch(IOException e){
 
    		e.printStackTrace();
 
    	}
 
    }
}

Result

Extracted value : 31/05/2010  08:05  14 logfile.log
Creation Date  : 31/05/2010
Creation Time  : 08:05
Tags :

About the Author

mkyong
Founder of Mkyong.com and HostingCompass.com, love Java and open source stuff. Follow him on Twitter, or befriend him on Facebook or Google Plus. If you like my tutorials, consider make a donation to these charities.

Comments

  • Pingback: water ionizer loan()

  • Pingback: bottled alkaline water()

  • Pingback: pay per day loan plans()

  • Pingback: water ionizers()

  • Pingback: parking()

  • Pingback: her og nu laan()

  • Pingback: fue.mobi()

  • Pingback: hurtigt lan penge nu()

  • Pingback: parking()

  • Pingback: DIRECTV packages()

  • Pingback: DIRECTV vs Dish()

  • Pingback: alkaline()

  • Pingback: water ionizers()

  • Pingback: free movie downloads()

  • Pingback: watch movies online free()

  • Pingback: tv online, online tv()

  • Pingback: watch movies online free()

  • Pingback: Blue Coaster33()

  • rushman

    In Java7 there is a more sophisticated way to get (or set!) file attributes, including creation time.

    public void printFileCreationTime() throws IOException {
    Path path = Paths.get(“C:\\myfile.txt”);
    BasicFileAttributeView basicView = Files.getFileAttributeView(path, BasicFileAttributeView.class);
    BasicFileAttributes basicAttr = basicView.readAttributes();
    LOG.info(String.format(“BasicFileAttribute creationTime: %s”, basicAttr.creationTime()));
    }

    If you don’t use log4j, replace the LOG.info(String.format()) to System.out.format(), or something similar.

    There are lots of other attribute views, with lots of fields. Check them out.

  • RamuM

    Hi
    Please provide the MAC based file/directory creation time or any other work around compatible to java 6.
    Thank you very much.

    Ramu madabhushani

  • parimala

    Extracted value : null
    Exception in thread “main” java.lang.NullPointerException
    at java.util.StringTokenizer.(Unknown Source)
    at java.util.StringTokenizer.(Unknown Source)
    at ratna.GetFileCreationDateExample.main(GetFileCreationDateExample.java:32)

    • saraby

      Your problem could be the white spaces. Try this:

      String path =
      Process proc = Runtime.getRuntime().exec(“cmd /c dir \”” + path + “\” /tc”);

  • narain

    I think we can show AM/PM too.

    String date = st.nextToken();//Get date
    String time = st.nextToken();//Get time
    String amPm = st.nextToken();//Get AM or PM

    System.out.println(“Creation Date : ” + date);
    System.out.println(“Creation Time : ” + time + ” ” + amPm);

  • Pingback: Java I/O Tutorial()