2012-07-05 24 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

回答