java.io.File setLastModified(long time)
Description
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.
Throws:
- 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.
Compatibility
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.
package com.javatutorialhq.java.examples; import java.io.File; import java.util.Calendar; /* * This example source code demonstrates the use of * setLastModified() method of File class. * */ public class FileSetLastModifiedExample { public static void main(String[] args) { // initialize File object File file = new File("C:javatutorialhqinputtest_file.txt"); if(file.exists()){ // initialize calendar object Calendar cal = Calendar.getInstance(); long modDate = cal.getTimeInMillis(); boolean result = file.setLastModified(modDate); // check if the rename operation is success if(result){ System.out.println("Operation Success"); }else{ System.out.println("Operation failed"); } }else{ System.out.println("File does not exist"); } } }