0

I have found solution how to set last modified time in Java for specific file (Android: How to get & set directory modification date).

Is there a way to set creation date and time in Java for specific file as well?

3 Answers 3

4

In java 7 it is possible to set creation time to directory or file using such code:

//date which is used as date of creation
Date creationDate;
//path to directory of file to which to set creation time
Path somePath;
...
FileTime time = FileTime.fromMillis(creationDate.getTime());
Files.setAttribute(somePath, "creationTime", time);
Sign up to request clarification or add additional context in comments.

1 Comment

I have tried this solution but file creation time I can't change it.
3

Exactly the same way. A Date contains an instant in time, with a millisecond precision. Just change the format used to parse the date. Read the documentation for that.

Comments

0

This does not work on MAC for OSX a UNIX variant using HFS file system.

import java.io.*;
import java.util.*;
import java.nio.*;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.attribute.FileTime;
import static java.nio.file.LinkOption.NOFOLLOW_LINKS;

class changetime{

    public static void main(String args[]){

        Path p = Paths.get("./test.txt");
        System.out.println(p);
        try {
                Calendar c = Calendar.getInstance();
                c.set(2010, Calendar.MARCH, 20);
                Files.setAttribute(p, "basic:creationTime", FileTime.fromMillis(c.getTimeInMillis()), NOFOLLOW_LINKS);
        } catch (IOException e) {
                System.err.println("Cannot change the creation time. " + e);
        }
    }//END Main

}//END Class

1 Comment

can you please provide NOFOLLOW_LINKS

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.