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
01.
package
com.javatutorialcorner.file;
02.
03.
import
java.io.File;
04.
import
java.util.Date;
05.
06.
public
class
ChangeFileLastModifiedDate {
07.
08.
public
static
void
main(String[] args) {
09.
10.
File jtcFile =
new
File(
"/home/annamalai/workspace/File/jtc.txt"
);
11.
Long lastModified = jtcFile.lastModified();
12.
Date lastModifiedDate =
new
Date(lastModified);
13.
System.out.println(
"Last Modified Date : "
+ lastModifiedDate);
14.
15.
System.out.println(jtcFile.setLastModified(System.currentTimeMillis()));
16.
lastModifiedDate =
new
Date(jtcFile.lastModified());
17.
System.out.println(
"New Last Modified Date : "
+ lastModifiedDate);
18.
19.
}
20.
21.
}
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