Home » Java programming language

Java File Class boolean setLastModified(long set_new_time) method with Example

Java File Class boolean setLastModified(long set_new_time) method: Here, we are going to learn about the boolean setLastModified(long set_new_time) method of File class with its syntax and example.
Submitted by Preeti Jain, on July 15, 2019

File Class boolean setLastModified(long set_new_time)

  • This method is available in package java.io.File.setLastModified(long set_new_time).
  • This method is used to sets the last modified time of the given file.
  • In other words, Every file has some last modified time if file exists but by using this method we can change the last modified time of the file with the given new time.
  • The return type of this method is Boolean so it returns true if file last modification is set successfully else return false.

Syntax:

    boolean setLastModified(long  set_new_time){
    }

Parameter(s):

We pass only one object as a parameter in the method of the file i.e. set_new_time (We will set new time as last modified time of the file).

Return value:

The return type of this method is Boolean, it returns true if the given time is set successfully of last modification of the file else it returns false.

Java program to demonstrate example of setLastModified() method

import java.io.*;

public class SetFileLastModifiedTime {
    public static void main(String[] args) {
        try {
            // Create a file object file1
            File file1 = new File("E:\\Programs\\myjava.txt");

            // Previous Last Modified Time Of The File1
            System.out.println("The Previous Last Modification Time Of File1 is : " + file1.lastModified());

            // By using setLastModified(long set_new_time) method sets 
            // new last modified time of the file.
            System.out.println("The New Last Modification Time has set : " + file1.setLastModified(2000000));

            // Display New Last Modified Time Of The File1
            System.out.println("The New Last Modification Time Of File1 is : " + file1.lastModified());
        } catch (Exception e) {
            System.err.println("An error occurred");
            e.printStackTrace();
        }
    }
}

Output

E:\Programs>javac SetFileLastModifiedTime.java

E:\Programs>java SetFileLastModifiedTime 
The Previous Last Modification Time Of File1 is : 1000000
The New Last Modification Time Of File1 has set : true
The New Last Modification Time Of File1 is : 2000000


Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.