Java IO & NIO - Files.setLastModifiedTime() Examples

Code Snippets Java IO & NIO Java 


Class:

java.nio.file.Files

java.lang.Objectjava.lang.Objectjava.nio.file.Filesjava.nio.file.FilesLogicBig

Method:

public static Path setLastModifiedTime(Path path,
                                       FileTime time)
                                throws IOException

Updates a file's last modified time attribute.

Parameters:
path - the path to the file
time - the new last modified time
Returns:
the path


Examples


package com.logicbig.example.files;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.attribute.FileTime;
import java.time.Instant;

public class SetLastModifiedTimeExample {

public static void main(String... args) throws IOException {
Path path = Files.createTempFile("test-file", ".txt");

System.out.println("-- lastModifiedTime before --");
FileTime lastModifiedTime = Files.getLastModifiedTime(path);
System.out.println(lastModifiedTime);

Instant instant = Instant.now()
.minusSeconds(10000 * 24 * 60 * 60);
FileTime fileTime = FileTime.from(instant);
Files.setLastModifiedTime(path, fileTime);

System.out.println("-- lastModifiedTime after --");
lastModifiedTime = Files.getLastModifiedTime(path);
System.out.println(lastModifiedTime);
}
}

Output

-- lastModifiedTime before --
2017-12-17T18:20:08.096912Z
-- lastModifiedTime after --
1990-08-01T18:20:08.100915Z




See Also