Java set file date

Change File Last Modified date in Java example

In this example we are going to see how you can change the “Last Modified” date of a File in your File System in Java. We are simply going to use the setLastModified method of the File class. We are also going to see how you can parse a string with a date format to a Date object which is kind of cool. So the basic steps to change the “Last Modified” date of file in Java are:

  • Use the SimpleDateFormat(«MM/dd/yyyy») constructor to make a new SimpleDateFormat class instance.
  • Construct a String object with the “MM/dd/yyyy” format.
  • Use parse(String date) method of the SimpleDateFormat class to create a new Date object with the date value of the String .
  • Use File.setLastModified(Date.getTime()) method to set the new “Last Modified” date of the file.
package com.javacodegeeks.java.core; import java.io.File; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; public class ChangeFileLastModifiedDate < public static final String filepath = "/home/nikos/Desktop/testFile.txt"; public static void main(String[] args) < try < File file = new File(filepath); SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy"); // print the original Last Modified date System.out.println("Original Last Modified Date : " + dateFormat.format(file.lastModified())); // set this date String newLastModifiedString = "01/31/1821"; // we have to convert the above date to milliseconds. Date newLastModifiedDate = dateFormat.parse(newLastModifiedString); file.setLastModified(newLastModifiedDate.getTime()); // print the new Last Modified date System.out.println("Lastest Last Modified Date : " + dateFormat.format(file.lastModified())); >catch (ParseException e) < e.printStackTrace(); >> >
Original Last Modified Date : 02/21/2013 New Last Modified Date : 02/02/2000 

This was an example on how to change the Last Modified date value of a File in your File System in Java.

Читайте также:  Try catch php require

Источник

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())); >
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())); >
10/21/2013 08:40:21 10/26/2013 08:40:21

Change last modified date posted by Justin Musgrove on 05 July 2014

Tagged: java and java-io

Источник

Оцените статью