6

How to get the creation date of file in android. I know about file.lastModified(), but I really need the creation date, that you can see in OS Windows in Properties of file.

If someone know the solution of this task, please, write it below this message. Thanks!

Slavik Blase
  • 59
  • 1
  • 3

2 Answers2

4

Creation dates are not supported by every operating system. That's why Java doesn't have a method to get the creation date of a file. I ran into this problem recently also.

What I did was append the timestamp as appendix for the file.

File f = new File("myFile-" + System.currentTimeMillis());

When you later look for your file, you'll be able to extract the appendix and convert it back to a date to find it's creation date.

String fileName = f.getName();
String[] split = fileName.split("-");
long timeStamp = 0;

try {
    timeStamp = Long.parseLong(split[1]);
} catch(NumberFormatException nfe) {
    nfe.printStackTrace();
}

System.out.println("Creation date for file " + f + " is " + new Date(timeStamp));
JREN
  • 3,357
  • 3
  • 24
  • 43
-1

I find the solution of my question, may be some one need it: You can get creation date via executing bash command or script like described here

stat <filepath>

For executing script from android:

p = Runtime.getRuntime().exec("stat " + /mnt/sdcard01/somefile);
p.waitFor();

BufferedReader reader = 
     new BufferedReader(new InputStreamReader(
 p.getInputStream()));
String line = reader.readLine();
while (line != null) {
 line = reader.readLine();
}
Slavik Blase
  • 59
  • 1
  • 3
  • 1
    In the blog you shared the author has admitted in comments that `Creation Time` was a typo and Linux/Unix doesn't support creation time. – Ankit Bansal May 28 '15 at 12:28