12 comments on “How to get file creation date in Java

    1. The NIO works for Linux and Windows.

      For example 3, this is a Windows solution before Java 7.

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

    Reply
    1. Skip the first five lines, and we only need the 6th line that contains the file creation time info.

      Reply
  2. 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.

    Reply
  3. 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

    Reply
  4. 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)

    Reply
    1. Your problem could be the white spaces. Try this:

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

      Reply
      1. Thank youuuuuuuuuuuuuuuu so much !!!! 😀 save me !!!

        Reply
  5. 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);

    Reply
    1. 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

      Reply

Leave a Comment

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