How to modify a file time in Java

1 Answer

0 votes
package javaapplication1;

import java.io.*;
import java.text.*;
import java.util.Date;

public class JavaApplication1 
{
    public static void main(String[] args) 
    {
        try
        {
            File file = new File("d:\\data.txt");
 
            SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
            System.out.println(sdf.format(file.lastModified())); 
            
            sdf = new SimpleDateFormat("MM");
            String mm = sdf.format(file.lastModified());
            sdf = new SimpleDateFormat("dd");
            String dd = sdf.format(file.lastModified());
            sdf = new SimpleDateFormat("yyyy");
            String yyyy = sdf.format(file.lastModified());
            
            String newdate = mm + "/" + dd + "/" + yyyy + " " + "12:43:02";
            sdf = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
            Date date = sdf.parse(newdate);
            file.setLastModified(date.getTime());
            System.out.println(sdf.format(file.lastModified())); 
        }
        catch (Exception e)
        {
            System.out.println(e.toString());
        }  
    }
}

/*

run:

02/15/2014 23:06:33
02/15/2014 12:43:02

*/

 



answered Jun 21, 2015 by avibootz

Related questions

1 answer 1,290 views
1,290 views asked Jun 19, 2015 by avibootz
1 answer 317 views
1 answer 344 views
344 views asked Jun 19, 2015 by avibootz
1 answer 331 views
331 views asked Jun 18, 2015 by avibootz
1 answer 390 views
1 answer 386 views
...