- Java file time
- Files
- BasicFileAttributes
- Java file creation time
- Java file last modification time
- Java file last access time
- Author
- File access time java
- Method Summary
- Method Detail
- lastModifiedTime
- lastAccessTime
- creationTime
- isRegularFile
- isDirectory
- isSymbolicLink
- isOther
- size
- fileKey
- How to get file last modified date in Java
- 1. BasicFileAttributes (NIO)
- 2. File.lastModified (Legacy IO)
Java file time
In Java file time tutorial, we show how to determine file creation, last modification, and last access time in Java with Files and BasicFileAttributes .
Files
Files is a Java class that contains static methods that operate on files, directories, or other types of files. Mostly, these methods will delegate to the associated file system provider to perform the file operations.
BasicFileAttributes
BasicFileAttributes holds basic file attributes. These are attributes that are common to many file systems and consist of mandatory and optional file attributes, such as size of file creation time. BasicFileAttributes are retrieved with Files.readAttributes method.
Java file creation time
The file creation time in Java is retrieved with BasicFileAttributes.creationTime method.
package com.zetcode; import java.io.File; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.attribute.BasicFileAttributes; public class JavaFileLastCreationTime < public static void main(String[] args) throws IOException < String fileName = "/home/jano/world.sql"; File myfile = new File(fileName); Path path = myfile.toPath(); BasicFileAttributes fatr = Files.readAttributes(path, BasicFileAttributes.class); System.out.printf("File creation time: %s%n", fatr.creationTime()); >>
This example prints the creation time of the specified file.
Java file last modification time
The BasicFileAttributes.lastModifiedTime method gets the last modification time of a file in Java.
package com.zetcode; import java.io.File; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.attribute.BasicFileAttributes; public class JavaFileLastModifiedTime < public static void main(String[] args) throws IOException < String fileName = "/home/jano/world.sql"; File myfile = new File(fileName); Path path = myfile.toPath(); BasicFileAttributes fatr = Files.readAttributes(path, BasicFileAttributes.class); System.out.printf("Last modification time: %s%n", fatr.lastModifiedTime()); >>
This example prints the last modification time of the specified file.
Java file last access time
The last access time of a file in Java is retrieved with BasicFileAttributes.lastAccessTime method.
package com.zetcode; import java.io.File; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.attribute.BasicFileAttributes; public class JavaFileLastAccessTime < public static void main(String[] args) throws IOException < String fileName = "/home/jano/world.sql"; File myfile = new File(fileName); Path path = myfile.toPath(); BasicFileAttributes fatr = Files.readAttributes(path, BasicFileAttributes.class); System.out.printf("Last access time: %s%n", fatr.lastAccessTime()); >>
This example prints the last access time of the specified file.
In this article we have determined the file creation, last modification, and last access time with Files and BasicFileAttributes .
Author
My name is Jan Bodnar and I am a passionate programmer with many years of programming experience. I have been writing programming articles since 2007. So far, I have written over 1400 articles and 8 e-books. I have over eight years of experience in teaching programming.
File access time java
Basic attributes associated with a file in a file system. Basic file attributes are attributes that are common to many file systems and consist of mandatory and optional file attributes as defined by this interface. Usage Example:
Path file = . BasicFileAttributes attrs = Files.readAttributes(file, BasicFileAttributes.class);
Method Summary
Method Detail
lastModifiedTime
Returns the time of last modification. If the file system implementation does not support a time stamp to indicate the time of last modification then this method returns an implementation specific default value, typically a FileTime representing the epoch (1970-01-01T00:00:00Z).
lastAccessTime
Returns the time of last access. If the file system implementation does not support a time stamp to indicate the time of last access then this method returns an implementation specific default value, typically the last-modified-time or a FileTime representing the epoch (1970-01-01T00:00:00Z).
creationTime
Returns the creation time. The creation time is the time that the file was created. If the file system implementation does not support a time stamp to indicate the time when the file was created then this method returns an implementation specific default value, typically the last-modified-time or a FileTime representing the epoch (1970-01-01T00:00:00Z).
isRegularFile
isDirectory
isSymbolicLink
isOther
size
Returns the size of the file (in bytes). The size may differ from the actual size on the file system due to compression, support for sparse files, or other reasons. The size of files that are not regular files is implementation specific and therefore unspecified.
fileKey
Returns an object that uniquely identifies the given file, or null if a file key is not available. On some platforms or file systems it is possible to use an identifier, or a combination of identifiers to uniquely identify a file. Such identifiers are important for operations such as file tree traversal in file systems that support symbolic links or file systems that allow a file to be an entry in more than one directory. On UNIX file systems, for example, the device ID and inode are commonly used for such purposes. The file key returned by this method can only be guaranteed to be unique if the file system and files remain static. Whether a file system re-uses identifiers after a file is deleted is implementation dependent and therefore unspecified. File keys returned by this method can be compared for equality and are suitable for use in collections. If the file system and files remain static, and two files are the same with non- null file keys, then their file keys are equal.
Report a bug or suggest an enhancement
For further API reference and developer documentation see the Java SE Documentation, which contains more detailed, developer-targeted descriptions with conceptual overviews, definitions of terms, workarounds, and working code examples.
Java is a trademark or registered trademark of Oracle and/or its affiliates in the US and other countries.
Copyright © 1993, 2023, Oracle and/or its affiliates, 500 Oracle Parkway, Redwood Shores, CA 94065 USA.
All rights reserved. Use is subject to license terms and the documentation redistribution policy.
How to get file last modified date in Java
In Java, we can use Files.readAttributes() to get the file metadata or attribute, and then lastModifiedTime() to display the last modified date of a file.
Path file = Paths.get("/home/mkyong/file.txt"); BasicFileAttributes attr = Files.readAttributes(file, BasicFileAttributes.class); System.out.println("lastModifiedTime: " + attr.lastModifiedTime());
1. BasicFileAttributes (NIO)
This example uses the java.nio.* to display the file attributes or metadata – creation time, last access time, and last modified time.
package com.mkyong.io.howto; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.nio.file.attribute.BasicFileAttributes; public class GetLastModifiedTime1 < public static void main(String[] args) < String fileName = "/home/mkyong/file.txt"; try < Path file = Paths.get(fileName); BasicFileAttributes attr = Files.readAttributes(file, BasicFileAttributes.class); System.out.println("creationTime: " + attr.creationTime()); System.out.println("lastAccessTime: " + attr.lastAccessTime()); System.out.println("lastModifiedTime: " + attr.lastModifiedTime()); >catch (IOException e) < e.printStackTrace(); >> >
creationTime: 2020-07-20T09:29:54.627222Z lastAccessTime: 2020-07-21T12:15:56.699971Z lastModifiedTime: 2020-07-20T09:29:54.627222Z
The BasicFileAttributes also works for the directory, and we can use the same code to display the last modified time of a directory.
2. File.lastModified (Legacy IO)
For legacy IO, we can use the File.lastModified() to get the last modified time; the method returns a long value measured in milliseconds since the [epoch time](https://en.wikipedia.org/wiki/Epoch_(computing). We can use SimpleDateFormat to make it a more human-readable format.
package com.mkyong.io.howto; import java.io.File; import java.text.SimpleDateFormat; public class GetLastModifiedTime2 < public static void main(String[] args) < String fileName = "/home/mkyong/test"; File file = new File(fileName); System.out.println("Before Format : " + file.lastModified()); SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss"); System.out.println("After Format : " + sdf.format(file.lastModified())); >>
Before Format : 1595237394627 After Format : 07/20/2020 17:29:54