Change last modified date

This example will demonstrate how to change the file last modified date or late updated timestamp of on a file by using java, guava and apache commons. Lets say you have a change control process that looks at the last modified date when you upload a file. In the event you need to circumvent you can programmatically set the modified date by following the example below which should work on any platform such as linux, windows or macos. We will create a String that will represent the file name through out the snippets below.

Setup

private static final String OUTPUT_FILE_NAME = "ChangeFileLastModifiedDate.txt";

Straight up Java

@Test
public void change_last_modified_date_java () {

    SimpleDateFormat dateFormatter = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");

    File file = new File(OUTPUT_FILE_NAME);

    //display current lastmodified 
    logger.info(dateFormatter.format(file.lastModified()));

    //set current datetime to lastmodified 
    DateTime today = new DateTime(file.lastModified());

    //minus 5 days
    file.setLastModified(today.minusDays(5).getMillis());

    //display latest lastmodified 
    logger.info(dateFormatter.format(file.lastModified()));

}

Java 7 File I/O

Using the java 7 NIO we will set the set the last modified date to 5 days in the past. We will first create a Path which represents the location of the file then using joda we will create a current time. Next we need to create a FileTime object which represents the value of a file's time stamp attribute. Then using the Files utility class we will call the setLastModifiedTime which updates a file's last modified time attribute to the precision supported by the file system.

@Test
public void change_last_modified_date_java7()
        throws URISyntaxException, IOException {

    Path path = Paths.get(OUTPUT_FILE_NAME);

    DateTime today = new DateTime(new Date());

    FileTime fileTime = FileTime.from(today.minusDays(5).getMillis(),
            TimeUnit.MILLISECONDS);

    java.nio.file.Files.setLastModifiedTime(path, fileTime);
}

Google Guava

This example will show setting the last modified to 5 days ago, then calling guava Files.touch which should set the last modified date to current time.

/**
 * Files.touch creates an empty file or updates the last updated timestamp on the
 * same as the unix command of the same name.
 *
 * @throws IOException
 */
@Test
public void touch_file_guava () throws IOException {

    SimpleDateFormat dateFormatter = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
    DateTime today = new DateTime();

    File file = new File(OUTPUT_FILE_NAME);

    // set last modified to 5 days ago
    file.setLastModified(today.minusDays(5).getMillis());

    //display latest lastmodified 
    logger.info(dateFormatter.format(file.lastModified()));

    Files.touch(file);

    //display latest lastmodified 
    logger.info(dateFormatter.format(file.lastModified()));
}

Output

10/21/2013 12:14:24
10/26/2013 12:14:24

Apache Commons

This example will show setting the last modified to 5 days ago, then calling apache FileUtils.touch which should set the last modified date to current time.

/**
 * FileUtils.touch Implements the same behaviour as
 * the "touch" utility on Unix. It creates a new
 * file with size 0 or, if the file exists already,
 * it is opened and closed without modifying it,
 * but updating the file date and time.
 *
 *
 * @throws IOException
 */
@Test
public void touch_file_apache () throws IOException {

    SimpleDateFormat dateFormatter = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
    DateTime today = new DateTime();

    File file = new File(OUTPUT_FILE_NAME);

    // set last modified to 5 days ago
    file.setLastModified(today.minusDays(5).getMillis());

    //display latest lastmodified 
    logger.info(dateFormatter.format(file.lastModified()));

    FileUtils.touch(file);

    //display latest lastmodified 
    logger.info(dateFormatter.format(file.lastModified()));
}

Output

10/21/2013 08:40:21
10/26/2013 08:40:21