3
new File(url).lastModified()
返回一個long
,該值等於自基於GMT的歷元開始以來的毫秒數。獲取文件的最後修改日期/時間作爲本地日期/時間字符串
什麼是將此轉換爲代表系統本地日期/時間的String
的簡單方法?
如果你真的需要看到我在這裏是一個嘗試,但它是一個可怕的爛攤子,這是錯的呢:
LocalDateTime.ofEpochSecond(new File(url).lastModified()/1000,0,ZoneOffset.UTC).atZone(ZoneId.of("UTC")).format(DateTimeFormatter.ofLocalizedDateTime(FormatStyle.LONG))
超越LocalDateTime
我只是不知道如何時API作品。
是否可以格式化FileTime到不同的默認值,而不必先用'FileTime :: toMillis'回到原來的方式? – Museful
@tennenrishin是的,你可以通過['toInstant()']獲得一個Instant(https://docs.oracle.com/javase/8/docs/api/java/nio/file/attribute/FileTime.html#toInstant - )並使用['DateTimeFormatter']格式化(https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html)。 – Tunaki
'DateTimeFormatter.ofPattern(「uuuu-MMM-dd HH:mm:ss」)。format(fileTime.toInstant())'throws' java.time.temporal.UnsupportedTemporalTypeException:Unsupported field:Year' for me。我究竟做錯了什麼? – Museful