// @topic T11901 File attributes, creation time, modified time // @brief Using Path, Paths, BasicFileAttributes, Date, and GregorianCalendar package week11files; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.nio.file.attribute.BasicFileAttributes; import java.util.Date; import java.util.GregorianCalendar; import java.util.Scanner; public class AppMain { /** * @param args the command line arguments */ public static void main(String[] args) { for (;;) { String name; Scanner keyboard = new Scanner(System.in); System.out.print("Enter a file name >> "); name = keyboard.nextLine(); // Path filePath = Paths.get(name); System.out.println("Path is " + filePath.toString()); try { BasicFileAttributes attr = Files.readAttributes(filePath, BasicFileAttributes.class); String creationTime = attr.creationTime().toString(); long seconds = attr.creationTime().toMillis() / 1000; Date date = new Date( attr.creationTime().toMillis() ); GregorianCalendar calendar = new GregorianCalendar(); calendar.setTime(date); // For example: 2014-04-22T17:19:30.6266Z // 0 1 2 0 1 3------- final String DELIMITER_T = "T"; final String DELIMITER_DASH = "-"; final String DELIMITER_COLON = ":"; final int FILETIME_PART_DATE = 0; final int FILETIME_PART_TIME = 1; final int FILETIME_PART_YYYY = 0; final int FILETIME_PART_MM = 1; final int FILETIME_PART_DD = 2; final int FILETIME_PART_HOURS = 0; final int FILETIME_PART_MINUTES = 1; String[] parts = creationTime.split( DELIMITER_T ); String [] dateParts = parts[ FILETIME_PART_DATE ].split( DELIMITER_DASH ); String [] timeParts = parts[ FILETIME_PART_TIME ].split( DELIMITER_COLON ); System.out.println("Creation time " + attr.creationTime()); System.out.println("Creation time " + dateParts[ FILETIME_PART_MM ] + '/' + dateParts[ FILETIME_PART_DD ] + '/' + dateParts[ FILETIME_PART_YYYY ] + " " + timeParts[ FILETIME_PART_HOURS ] + ":" + timeParts[ FILETIME_PART_MINUTES ] ); System.out.println("Last modified time " + attr.lastModifiedTime()); System.out.println("Size " + attr.size()); } catch (IOException e) { System.out.println("IO Exception"); } } } }