Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Semrush - keyword research tool

Create your online store today with Shopify

Turn ChatGPT, Claude, Gemini, And CoPilot Into Your Personal Assistant, Business Coach, Content Creator, And More

AFFILIATE MARKETING Your all-in-one performance engine Manage affiliates, creators, and customer referrals in one unified platform—turning every partnership into measurable growth

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Disclosure: My content contains affiliate links.

43,227 questions

56,129 answers

573 users

How to modify a file date 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("HH");
            String hh = sdf.format(file.lastModified());
            sdf = new SimpleDateFormat("mm");
            String mm = sdf.format(file.lastModified());
            sdf = new SimpleDateFormat("ss");
            String ss = sdf.format(file.lastModified());
            
            String newdate = "02/15/2014 " + hh + ":" + mm + ":" + ss;
            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:

06/20/2015 23:06:33
02/15/2014 23:06:33

*/

 



answered Jun 20, 2015 by avibootz

Related questions

1 answer 1,236 views
1,236 views asked Jun 19, 2015 by avibootz
1 answer 245 views
245 views asked Jun 21, 2015 by avibootz
1 answer 266 views
1 answer 224 views
224 views asked Jun 18, 2015 by avibootz
1 answer 282 views
282 views asked Jun 18, 2015 by avibootz
1 answer 344 views
...