[JAVA] 자바에서 파일 날짜 변경, 시간 변경

[JAVA] 자바에서 파일 날짜 변경, 시간 변경

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.attribute.FileTime;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class MainClass {

    public static void main(String[] args) {
        String filePath = “C:\\test\\0001.png”;
        File file = new File(filePath);

        try {
            String pattern = “yyyy-MM-dd HH:mm:ss”;
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);
            Date timeToSet = simpleDateFormat.parse(“2021-02-01 10:00:00”);

            FileTime time = FileTime.fromMillis(timeToSet.getTime());

            // 만든 날짜 변경
            Files.setAttribute(file.toPath(), “creationTime”, time);
            
            // 수정한 날짜 변경
            Files.setAttribute(file.toPath(), “lastModifiedTime”, time);
            
            // 엑세스한 날짜 변경
            Files.setAttribute(file.toPath(), “lastAccessTime”, time);

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

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

참고사이트 : http://oliviertech.com/ko/java/how-to-update-the-date-time-of-a-file-using-java/