【发布时间】:2012-07-06 08:20:36
【问题描述】:
我正在开发一个在 linux 机器上运行的 java 应用程序,它应该将文件的时间戳更改为以纪元形式存储的另一个时间。 需要更改时间戳的文件在本地文件系统中。
Ex - 时间戳显示 17 Jul 5 20:03 的 localFile.txt 需要更改为 epoch "1341446400000"
我写过这样的代码 -
private void modifyTime(final String localFile, final long originalEpoch) throws IOException {
String getDateFromEpoch = "date -d@" + String.valueOf(originalEpoch);
//getDateFromEpoch is returned in form - "Thu Jul 5 20:03:32 UTC 2012"
Process process = runCommand(getDateFromEpoch);
InputStream iStream = process.getInputStream();
BufferedReader bufReader = new BufferedReader(new InputStreamReader(iStream));
String originalDate = bufReader.readLine();
bufReader.close();
String touch = "touch -c -d " + originalDate + " " + localFile;
runCommand(touch);
}
private Process runCommand(final String cmd) throws IOException {
Process p = Runtime.getRuntime().exec(cmd);
try {
p.waitFor();
} catch (InterruptedException e) {
// ignore this exception
}
return p;
}
运行 "date -d@" + String.valueOf(originalEpoch); 正在返回类似于 Thu Jul 5 20:03:32 UTC 2012 的内容。在触摸命令中使用此命令对我不起作用。
有没有办法做到这一点?
【问题讨论】:
-
使用下面的答案让您的生活更轻松...
标签: java epoch unix-timestamp