import java.nio.file.*; import java.io.IOException; public class FileLastModifiedTime { public static void main(String[] args) { Path path = Paths.get("C:/TestFile.txt"); try { FileTime lastModifiedTime = Files.getLastModifiedTime(path); System.out.println("Last Modified Time : " + lastModifiedTime); } catch (IOException e) { System.out.println("IOException Occurred" + e.getMessage()); } } }
import java.nio.file.*; import java.io.IOException; public class CompareFileLastModifiedTime { public static void main(String[] args) { Path path1 = Paths.get("C:/TestFile1.txt"); Path path2 = Paths.get("C:/TestFile2.txt"); try { FileTime lastModifiedTime1 = Files.getLastModifiedTime(path1); FileTime lastModifiedTime2 = Files.getLastModifiedTime(path2); int comparison = lastModifiedTime1.compareTo(lastModifiedTime2); if (comparison == 0) { System.out.println("Both files modified at the same time."); } else if (comparison < 0) { System.out.println("File 1 was modified before file 2."); } else { System.out.println("File 1 was modified after file 2."); } } catch (IOException e) { System.out.println("IOException Occurred" + e.getMessage()); } } }In this example, we are comparing the last modified time of two files "C:/TestFile1.txt" and "C:/TestFile2.txt". If the files were modified at the same time, a message is printed indicating that. Otherwise, the message indicates which file was modified earlier or later. Both examples use the `Files.getLastModifiedTime()` method provided by the `java.nio.file` package.