Following example shows how to change file last modified date using file.setLastModified() method in Java.
setLastModified
public boolean setLastModified(long time)
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.
Parameters:
time - The new last-modified time, measured in milliseconds since the epoch (00:00:00 GMT, January 1, 1970)
Returns:
true if and only if the operation succeeded; false otherwise
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
Sample Program
ChangeFileLastModifiedDate.java
Result
The above code will produce the following result. The result may vary based on system time
setLastModified
public boolean setLastModified(long time)
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.
Parameters:
time - The new last-modified time, measured in milliseconds since the epoch (00:00:00 GMT, January 1, 1970)
Returns:
true if and only if the operation succeeded; false otherwise
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
Sample Program
ChangeFileLastModifiedDate.java
package com.javatutorialcorner.file; import java.io.File; import java.util.Date; public class ChangeFileLastModifiedDate { public static void main(String[] args) { File jtcFile = new File("/home/annamalai/workspace/File/jtc.txt"); Long lastModified = jtcFile.lastModified(); Date lastModifiedDate = new Date(lastModified); System.out.println("Last Modified Date : " + lastModifiedDate); System.out.println(jtcFile.setLastModified(System.currentTimeMillis())); lastModifiedDate = new Date(jtcFile.lastModified()); System.out.println("New Last Modified Date : " + lastModifiedDate); } }
Result
The above code will produce the following result. The result may vary based on system time
Last Modified Date : Wed May 13 07:48:30 IST 2015 true New Last Modified Date : Fri May 15 06:38:29 IST 2015
0 comments:
Post a Comment