2012-07-05 44 views
0

我正在研究一个在linux机器上运行的java应用程序,它应该将文件的时间戳更改为以纪元形式存储的另一个时间戳。 需要更改时间戳的文件存在于本地文件系统中。使用纪元时间改变文件的时间戳使用java

防爆 - localFile.txt其时间戳显示07月17日20:03 5个需要改变,以划时代“1341446400000”

我写这样的代码 -

private void modifyTime(final String localFile, final long originalEpoch) throws IOException { 
    String getDateFromEpoch = "date [email protected]" + 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 [email protected]" + String.valueOf(originalEpoch);正在返回类似于Thu Jul 5 20:03:32 UTC 2012。在触摸命令中使用此功能对我来说不起作用。

有没有办法做到这一点?

+0

使用下面的答案,让您的生活轻松了许多... – jahroy 2012-07-05 21:42:32

回答