欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Java 修改文件时间

程序员文章站 2022-03-06 18:36:22
import java.io.File;import java.io.IOException;import java.text.SimpleDateFormat;import java.util.Date;public class ToUpdataDate { public static void main(String[] args) throws Exception { String address = path; String pattern = "...
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class ToUpdataDate {

    public static void main(String[] args) throws Exception {
        String address = path;
        String pattern = "yyyy-MM-dd HH-mm-ss";
        t7(address, stringToData("2020-12-25 08-57-22", pattern));
    }

    //修改文件最后的修改日期
    public static void t7(String address, Date date) {
        try {
            File f = new File(address);
            f.createNewFile();
            Date d = new Date(f.lastModified());
            System.out.println("原始修改日期为:" + d.toString());
            boolean falg = f.setLastModified(date.getTime());
            System.out.println("修改:" + falg);
            d = new Date(f.lastModified());
            System.out.println("修改后日期:" + d.toString());

        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    public static Date stringToData(String data, String pattern) throws Exception {
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        return sdf.parse(data);
    }

}

本文地址:https://blog.csdn.net/qq_42363032/article/details/111825371

相关标签: tools