I want to get ctime of a file in java. ctime is change time ,can be get by using "ls -lc filename" in linux. but how can I get it in java ? also I need to get it in java's FTP API.
Asked
Active
Viewed 1,555 times
-2
-
3Writing "to" instead of "2" just takes one more tap on your keyboard and makes you post better. – jhamon Dec 09 '16 at 08:41
-
2Is it [Getting the last modified date of a file in Java](http://stackoverflow.com/questions/4363197/getting-the-last-modified-date-of-a-file-in-java)? – zapl Dec 09 '16 at 08:43
-
Not lastModifiedTime(mtime), I need ctime in linux. – Geek2Sages Dec 09 '16 at 08:48
-
1Possible duplicate of [Determine file creation date in Java](http://stackoverflow.com/questions/2723838/determine-file-creation-date-in-java) – walen Dec 09 '16 at 08:51
-
Oh, actually ctime is different from creation date (gets updated when file content changes): http://www.linux-faqs.info/general/difference-between-mtime-ctime-and-atime - Typical linux filesystems don't even have a creation date at all: http://unix.stackexchange.com/questions/24441/get-file-created-creation-time – zapl Dec 09 '16 at 08:56
-
This is a great question. I think the people who downvoted it just failed to understand it. Takes all types to make the world go round, I guess. – Dawood ibn Kareem Dec 09 '16 at 10:40
2 Answers
3
You want to call
Files.getAttribute(path, "unix:ctime")
which will return the ctime (metadata changed time). This only works in Unix/Linux systems of course.
I think it returns a String
which you'll then have to parse if you want a date. I can't try it out, of course, because my computer runs Windows. I also don't know what time zone it will come back in. So you'll have to experiment for yourself to see what comes back.
I've made this Community Wiki, and I invite anyone who does know the time zone and return format of this (or who wants to try it out on a Unix machine) to edit this post accordingly.

Dawood ibn Kareem
- 77,785
- 15
- 98
- 110
-
Note that you'll have to cast the return object to a `java.nio.file.attribute.FileTime` – Moebius Aug 16 '19 at 12:47
-1
new File("C:\Users\username\Documents\filename").lastModified()
As given in the javadocs for java.io.File

Ashish John
- 1,867
- 2
- 23
- 38
-
-
Do you mean this... http://superuser.com/questions/234158/show-both-ctime-and-atime-in-ls-output – Ashish John Dec 09 '16 at 08:50