Home  >  Article  >  Java  >  Java Example - Modify the last modification date of a file

Java Example - Modify the last modification date of a file

黄舟
黄舟Original
2017-02-15 10:12:591454browse

The following example demonstrates using the fileToChange.lastModified() and fileToChange setLastModified() methods of the File class to modify the last modification date of the file:

/*
 author by w3cschool.cc
 Main.java
 */import java.io.File;import java.util.Date;public class Main {
   public static void main(String[] args) 
   throws Exception {
      File fileToChange = new File
      ("C:/myjavafile.txt");
      fileToChange.createNewFile();
      Date filetime = new Date
      (fileToChange.lastModified());
      System.out.println(filetime.toString());
      System.out.println      (fileToChange.setLastModified      (System.currentTimeMillis()));
      filetime = new Date
      (fileToChange.lastModified());
      System.out.println(filetime.toString());
   }}

The output of the above code is:

Sat Mar 21 22:00:48 CST 2015
true
Fri Apr 10 11:09:19 CST 2015

The above is the Java example - modify the last modification date of the file. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn