在 Java 中设置文件创建时间戳
2022-09-01 16:44:18
我知道设置创建时间戳在Java中不存在,因为Linux没有它,但是有没有办法在Java中设置文件(Windows)的创建时间戳?我有一个基本的修改时间戳编辑器,我就在这里。
import java.io.*;
import java.util.*;
import java.text.*;
import javax.swing.*;
public class chdt{
static File file;
static JFrame frame = new JFrame("Input a file to change");
public static void main(String[] args) {
try{
final JFileChooser fc = new JFileChooser();
fc.setMultiSelectionEnabled(false);
//BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
//System.out.println("Enter file name with extension:");
//String str = bf.readLine();
JOptionPane.showMessageDialog(null, "Input a file to change.");
frame.setSize(300, 200);
frame.setVisible(true);
int retVal = fc.showOpenDialog(frame);
if (retVal == JFileChooser.APPROVE_OPTION) {
file = fc.getSelectedFile();
frame.setVisible(false);
} else {
JOptionPane.showMessageDialog(null, "3RR0RZ! You didn't input a file.");
System.exit(0);
}
//System.out.println("Enter last modified date in 'dd-mm-yyyy-hh-mm-ss' format:");
//String strDate = bf.readLine();
String strDate = JOptionPane.showInputDialog("Enter last modified date in 'dd-mm-yyyy-hh-mm-ss' format:");
SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy-HH-mm-ss");
Date date = sdf.parse(strDate);
if (file.exists()){
file.setLastModified(date.getTime());
JOptionPane.showMessageDialog(null, "Modification is successful!");
}
else{
JOptionPane.showMessageDialog(null, "File does not exist! Did you accidentally it or what?");
}
}
catch(Exception e){
e.printStackTrace();
JOptionPane.showMessageDialog(null, "3RR0RZ");
}
}
}