2010-12-06 26 views

cevap

12

:

new File("/path/to/file").lastModified()

3

Java 7 beri kullanabilirsiniz java.nio.file.Files.getLastModifiedTime(Path path):

private static void printFileTime(FileTime fileTime) { 
    DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy - hh:mm:ss"); 
    System.out.println(dateFormat.format(fileTime.toMillis())); 
} 
: printFileName şöyle olabilir

Path path = Paths.get("C:\\1.txt"); 

FileTime fileTime; 
try { 
    fileTime = Files.getLastModifiedTime(path); 
    printFileTime(fileTime); 
} catch (IOException e) { 
    System.err.println("Cannot get the last modified time - " + e); 
} 

Çıktı:

10/06/2016 - 11:02:41 
+0

cevabın doğru ve iyi izah ancak uzun modası geçmiş ve 'herkesin bildiği zahmetli SimpleDateFormat' sınıfını kullanmak genç olanları öğretmek etmeyiniz. Bunun yerine, Java 8'den beri 'FileTime.toInstant()' işlevini kullanın, 'Instant'' '' ZonedDateTime' 'e dönüştürün ve ya sadece yazdırın ya da 'DateTimeFormatter'ı kullanarak biçimlendirin. –

İlgili konular