File modification time
This task will attempt to get and set the modification time of a file.
File modification time
You are encouraged to solve this task according to the task description, using any language you may know.
You are encouraged to solve this task according to the task description, using any language you may know.
Java
import java.util.File; import java.util.Time; import java.util.Date; public class FileModicationTimeTest { // if 0L is returned then the file does not exist public static long getFileModificationTime(String filename) { // Get the last modified time long modifiedTime = new File(filename).lastModified(); return modifiedTime; } public static boolean setFileModificationTimeToNow(String filename) // Set the last modified time long newModifiedTime = System.currentTimeMillis(); boolean success = new File(filename).setLastModified(newModifiedTime); return success; } public static boolean setFileModificationTime(String filename, long timeInMs) // Set the last modified time boolean success = new File(filename).setLastModified(newModifiedTime); return success; } public static void test(String type, String filename) { long t = getFileModificationTime(filename); System.out.println("The following " + type + " called " + filename + (t == 0 ? " does not exists : " was modified at " + new Time(t).toString() ) ); System.out.println("The following " + type + " called " + filename + (!setFileModificationTimeToNow(filename) ? " does not exists. " : " was modified to current time." ) ); System.out.println("The following " + type + " called " + filename + (!setFileModificationTime(filename,t) ? " does not exists. " : " was modified to previous time." ) ); } public static void main(String args[]) { test("file", "output.txt"); test("file", File.seperator + "output.txt"); test("directory", "docs"); test("directory", File.seperator + "docs" + File.seperator); } }