12 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments
rushman
13 years ago

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.

narain
13 years ago

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);

10 years ago
Reply to  narain

Hi All of you
I am looking for a code,which will display to me date and time Dynamically.means Just like a clock,run time.Please help me ASAP.Thanks in advance

Sufyan Khan

Mithilesh
6 years ago

this code will work in Linux platform?

tushar
9 years ago

Hi, will you please explain how for loop work here :
why i<6 only …i don't understand it…

RamuM
13 years ago

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
13 years ago

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
12 years ago
Reply to  parimala

Your problem could be the white spaces. Try this:

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

Gustavo Amaro
12 years ago
Reply to  saraby

Thank youuuuuuuuuuuuuuuu so much !!!! 😀 save me !!!