File setLastModified() method in Java with Examples Last Updated : 11 Jul, 2025 Summarize Comments Improve Suggest changes Share Like Article Like Report The setLastModified() method is a part of File class.The function sets the last modified time of the file or directory. The function sets the last modified value of the file in milliseconds.Function Signature: public boolean setLastModified(long time) Function Syntax: file.setLastModified(time) Parameters: This function accepts a long value as parameter which represents the new last modified time.Return value: The function returns a boolean value which states whether the new last modified time is set or not.Exception; This method throws following exceptions: SecurityException if the function is not allowed write access to the fileIllegalArgumentException if the argument is negative Below programs will illustrate the use of the setLastModified() function:Example 1: We will try to change the last modified time of a existing file in f: directory. Java // Java program to demonstrate the // use of setLastModified() function import java.io.*; public class solution { public static void main(String args[]) { // try-catch block to handle exceptions try { // Create a file object File f = new File("f:\\program.txt"); // The new last modified time long time = 100000000; // Check if the last modified time // can be set to new value if (f.setLastModified(time)) { // Display that the last modified time // is set as the function returned true System.out.println("Last modified time is set"); } else { // Display that the last modified time // cannot be set as the function returned false System.out.println("Last modified time cannot be set"); } } catch (Exception e) { System.err.println(e.getMessage()); } } } Output: Last modified time is set Example 2: We will try to change the last modified time of a non existing file in f: directory. Java // Java program to demonstrate the // use of setLastModified() function import java.io.*; public class solution { public static void main(String args[]) { // try-catch block to handle exceptions try { // Create a file object File f = new File("f:\\program1.txt"); // The new last modified time long time = 100000000; // Check if the last modified time // can be set to new value if (f.setLastModified(time)) { // Display that the last modified time // is set as the function returned true System.out.println("Last modified " + "time is set"); } else { // Display that the last modified time // cannot be set as // the function returned false System.out.println("Last modified time" + " cannot be set"); } } catch (Exception e) { System.err.println(e.getMessage()); } } } Output: Last modified time cannot be set The programs might not run in an online IDE. please use an offline IDE and set the Parent file of the file Comment More infoAdvertise with us Next Article File lastModified() method in Java with Examples A andrew1234 Follow Improve Article Tags : Java Java-Functions Java-IO package Java-File Class Practice Tags : Java Similar Reads Field setByte() method in Java with Examples setByte() method of java.lang.reflect.Field used to set the value of a field as a byte on the specified object. When you need to set the value of a field of an object as byte then you can use this method to set value over an Object. Syntax: public void setByte(Object obj, byte b) throws IllegalArgum 3 min read Field set() method in Java with Examples The set() method of java.lang.reflect.Field is used to set the value of the field represented by this Field object on the specified object argument to the specified new value passed as parameter. The new value is automatically unwrapped if the underlying field has a primitive type. If the field is s 4 min read File lastModified() method in Java with Examples The lastModified() function is a part of File class in Java . This function returns the time denoted by the this abstract pathname was last modified.The function returns long value measured in milliseconds, representing the time the file was last modified else returns 0L if the file does not exists 2 min read File length() method in Java with Examples The length() function is a part of File class in Java . This function returns the length of the file denoted by the this abstract pathname was length.The function returns long value which represents the number of bytes else returns 0L if the file does not exists or if an exception occurs. Function s 2 min read String replace() method in Java with Examples The String replace() method returns a new string after replacing all the old characters/CharSequence with a given character/CharSequence. Example:Return a new string where all " o" characters are replaced with "p" character: Java // Java program to demonstrate // the replace() method public class Ma 4 min read Modifiers methodModifiers() method in Java with Examples The methodModifiers() method of java.lang.reflect.Modifier class is used to get an integer value together with the modifiers of source language that can be applied to a Method. Syntax: public static boolean methodModifiers() Parameters: This method accepts nothing. Return: This method returns an int 1 min read Like