Retrieving the creation date of a file and displaying it should be a simple task. Yeah, right. The Java documentation has page after page regarding time and dates.
Some examples on the web suggest using:
String timeStamp = attr.creationTime().toString();
This returns a date string in Zulu time such as: 2009-02-13T23:31:30.123Z
Attempting to reformat this to reflect the time zone is a pain. Removing the ‘T’, the ‘Z’, and the fractional seconds and using the following will still fail to adjust for the time zone:
Date date = simpleDateFormat.parse(timeStamp, new ParsePosition(0)); timeStamp = simpleDateFormat.format(date);
A simpler solution is:
//----------------------------------------------------------------------------- // ViewerReporter::getFileCreationDateTimeString // // Returns the file creation timestamp for pFilename as a formatted String. // // On Linux systems, this returns the last modified date. // private String getFileCreationDateTimeString(String pFilename) { Path path = Paths.get(pFilename); BasicFileAttributes attr; try{ attr = Files.readAttributes(path, BasicFileAttributes.class); }catch (IOException e){ return(""); } Date date = new Date(attr.creationTime().toMillis()); SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MM-dd-yyyy HH:mm:ss"); String timeStamp = simpleDateFormat.format(date); return(timeStamp); }//end of ViewerReporter::getFileCreationDateTimeString //-----------------------------------------------------------------------------