Java File setLastModified() method example

HQ ┬╗ Java Tutorial ┬╗ ┬╗ File ┬╗ setLastModified() method example
Java File setLastModified() method example 2018-02-16T07:26:39+00:00 setLastModified(long time)


On this document we will be showing a java example on how to use the setLastModified(long time) method of File Class. This method sets the last-modified time of the file or directory named by this abstract pathname.

All platforms support file-modification times to the nearest second, but some provide more precision. The argument will be truncated to fit the supported precision. If the operation succeeds and no intervening operations on the file take place, then the next invocation of the lastModified() method will return the (possibly truncated) time argument that was passed to this method.


  • IllegalArgumentException – If the argument is negative
  • SecurityException – If a security manager exists and its SecurityManager.checkWrite(java.lang.String) method denies write access to the named file

Method Syntax

public boolean setLastModified(long time)

Method Argument

Data Type Parameter Description
long time The new last-modified time, measured in milliseconds since the epoch (00:00:00 GMT, January 1, 1970)

Method Returns

This method returns boolean, true if and only if the operation succeeded; false otherwise.


Requires Java 1.2 and up

Java File setLastModified(long time) Example

Below is a java code demonstrates the use of setLastModified(long time) method of File class. The example presented might be simple however it shows the behaviour of the setLastModified(long time) method of File class. Basically on this example, we just make use of the Calendar class to get the current time and set it as value on the setLastModified() method. This would set the last modified date of the File as the current time.

Sample Output

Below is the sample output when you run the above example.

java lang File setLastModified() example output

Contact Info


Our Goal

JavaTutorialHQ aims to to be The Ultimate Guide on Java with hundreds of examples from basic to advance Topics.

By continuing to use the site, you agree to the use of cookies. more information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this.