How to get create date of file in java?

Below java code shows how to get create date of file in java –

/****************************************************************************************
 * Created on 08-2011 Copyright(c) https://kodehelp.com All Rights Reserved.
 ****************************************************************************************/
package com.kodehelp.java.io;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * Created by Vigilance India.
 * User: https://kodehelp.com
 * Date: Aug 4, 2011
 */
public class GetCreateDateOfFile {
    public static void main (String args[]){
        try{
            // File class of java don't provide any method to get the create date of file. So we have to
            // get the create date in different way. We will be executing the windows command from java code
            Process proc = Runtime.getRuntime().exec("cmd /c dir c:\\demofile.txt /tc");

            BufferedReader br = new BufferedReader(new InputStreamReader(proc.getInputStream()));

            String tempdata = "";

            for(int i=0; i<6; i++){
                tempdata = br.readLine();
            }

            System.out.println("Create Date/Time"+tempdata);

        }catch(IOException ex){
            ex.printStackTrace();
        }
    }
}

one comment

  1. Arun says:

    This will create problems for different locales. For example, Windows Vista with Chinese Locale will return date in 24 hour yyyy/mm/dd format

Please Post Your Comments & Reviews

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.