1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
/* Set last modified time of a file or directory This Java example shows how set last modified time of a particular file or directory using setLastModified method of Java File class. */ import java.io.*; import java.util.Date; public class SetFileLastModifiedTime { public static void main(String[] args) { //create file object File file = new File("C://FileIO//ReadText.txt"); Date lastModified = new Date(file.lastModified()); System.out.println("Last modified time of file is : " + lastModified ); /* * To set last modified time of a file, use * boolean lastModifiedTime(long time) method of Java File class. * * Here, time is milliseconds since Jan 01, 1970 00:00:00. * * This method returns true if operation was successful, false otherwise. */ lastModified = new Date(); //set last modified time to current time boolean blnSuccess = file.setLastModified(lastModified.getTime()); System.out.println("Was last modified time set successfully ?:" + blnSuccess); System.out.println("File last modification date is : " + new Date(file.lastModified())); } } /* Typical output would be Last modified time of file is : Sun Jan 06 16:23:55 EST 2008 Was last modified time set successfully ?:true File last modification date is : Mon Jan 07 20:50:00 EST 2008 */ |
Add Comment