How to modify a file date and time in Java

1 Answer

0 votes
import java.io.File;
import java.util.Date;
import java.text.SimpleDateFormat;
 
public class JavaApplication1 
{
    public static void main(String[] args) 
    {
        try
        {
            File file = new File("d:\\data.txt");
  
            SimpleDateFormat simpledateformat = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
            System.out.println(simpledateformat.format(file.lastModified())); 
             
            String newdate = "06/18/2013 12:10:31";
            Date date = simpledateformat.parse(newdate);
            file.setLastModified(date.getTime());
            System.out.println(simpledateformat.format(file.lastModified())); 
        }
        catch (Exception e)
        {
            System.out.println(e.toString());
        }  
    }
}


 
/*
 
run:
 
12/31/1999 03:42:17
06/18/2013 12:10:31
 
*/

 



answered Jun 19, 2015 by avibootz
edited Jul 27, 2022 by avibootz

Related questions

1 answer 214 views
1 answer 236 views
236 views asked Jun 18, 2015 by avibootz
1 answer 298 views
1 answer 280 views
2 answers 256 views
2 answers 251 views
251 views asked Jun 15, 2015 by avibootz
...