In Java, we can use the NIO Files.setLastModifiedTime(path, FileTime)
to update the last modified date or time of a file.
Path path = Paths.get("/path/file"); LocalDate newLocalDate = LocalDate.of(1997, 12, 31); // year, month, dayOfMonth, hour, minute, second LocalDateTime newLocalDateTime = LocalDateTime.of(1999, 9, 30, 10, 30, 22); // convert LocalDateTime to Instant Instant instant = newLocalDateTime.toInstant(ZoneOffset.UTC); // convert LocalDate to Instant, need a time zone Instant instant = newLocalDate.atStartOfDay(ZoneId.systemDefault()).toInstant(); // convert instant to filetime // update last modified time of a file Files.setLastModifiedTime(path, FileTime.from(instant));
1. Java NIO – Update last modified date of a file.
For Java NIO java.nio.file.Files
, we can use Files.setLastModifiedTime()
to update the last modified date of a file.
UpdateLastModifiedTime.java
package com.favtuts.io.howto; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.nio.file.attribute.BasicFileAttributes; import java.nio.file.attribute.FileTime; import java.time.Instant; import java.time.LocalDate; import java.time.ZoneId; public class UpdateLastModifiedTime { public static void main(String[] args) throws IOException { String fileName = "/home/tvt/workspace/favtuts/google.png"; Path file = Paths.get(fileName); BasicFileAttributes attr = Files.readAttributes(file, BasicFileAttributes.class); FileTime lastModifiedTime = attr.lastModifiedTime(); // print original last modified time System.out.println("[original] lastModifiedTime: " + lastModifiedTime); LocalDate newLocalDate = LocalDate.of(1998, 9, 30); // convert LocalDate to instant, need time zone Instant instant = newLocalDate.atStartOfDay(ZoneId.systemDefault()).toInstant(); // convert instant to filetime // update last modified time Files.setLastModifiedTime(file, FileTime.from(instant)); // read last modified time again FileTime newLastModifiedTime = Files.readAttributes(file, BasicFileAttributes.class).lastModifiedTime(); System.out.println("[updated] lastModifiedTime : " + newLastModifiedTime); } }
Output
[original] lastModifiedTime: 2022-05-09T09:00:07.420532Z
[updated] lastModifiedTime : 1998-09-29T17:00:00Z
Review the Files.setLastModifiedTime
method signature, the new modified time is a FileTime.
Files.java
package java.nio.file; //... public static Path setLastModifiedTime(Path path, FileTime time) throws IOException { getFileAttributeView(path, BasicFileAttributeView.class) .setTimes(Objects.requireNonNull(time), null, null); return path; }
We can use FileTime.from(Instant instant)
to convert a Java 8 Instant
to a FileTime
.
Files.setLastModifiedTime(file, FileTime.from(instant));
or FileTime.fromMillis(long value)
to convert milliseconds since the epoch to the FileTime
.
FileTime now = FileTime.fromMillis(System.currentTimeMillis());
Files.setLastModifiedTime(path, now);
2. Legacy IO – Update last modified time of a file.
For legacy IO java.io.File
, we can use File.setLastModified()
to update the last modified date.
UpdateLastModifiedTime2.java
package com.favtuts.io.howto; import java.io.File; import java.io.IOException; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; public class UpdateLastModifiedTime2 { private static final SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy"); public static void main(String[] args) throws ParseException { String fileName = "/home/tvt/workspace/favtuts/google.png"; File file = new File(fileName); // print original last modified time System.out.println("[original] lastModifiedTime:" + sdf.format(file.lastModified())); //need convert the above date to milliseconds in long value Date newLastModified = sdf.parse("31/08/1998"); // update last modified date file.setLastModified(newLastModified.getTime()); //print the latest last modified date System.out.println("[updated] lastModifiedTime:" + sdf.format(file.lastModified())); } }
Output
[original] lastModifiedTime:30/09/1998
[updated] lastModifiedTime:31/08/1998
Review the File.setLastModified
method signature; the argument long time
is measured in milliseconds since the epoch (00:00:00 GMT, January 1, 1970).
File.java
package java.io; //... public boolean setLastModified(long time) { if (time < 0) throw new IllegalArgumentException("Negative time"); SecurityManager security = System.getSecurityManager(); if (security != null) { security.checkWrite(path); } if (isInvalid()) { return false; } return fs.setLastModifiedTime(this, time); }
Download Source Code
$ git clone https://github.com/favtuts/java-core-tutorials-examples
$ cd java-io