I am working on a java application running on linux machine, that should change a file's time-stamp to another time which is stored in form of epoch. The file whose time-stamp needs to be changed is present in local file system.
Ex - localFile.txt whose timestamp shows 17 Jul 5 20:03 needs to be changed to epoch "1341446400000"
I have written code like this -
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;
}
Running "date -d@" + String.valueOf(originalEpoch);
is returning something like Thu Jul 5 20:03:32 UTC 2012. Using this in touch command is not working for me.
Is there a way of doing this?
Sounds like you just want File.setLastModified
http://docs.oracle.com/javase/7/docs/api/java/io/File.html#setLastModified(long)