Determining age of a file in Java -
i have requirement determine how long file has been on ntfs file system.
i can read file creation time, , system time in millis, 2 not add up. off number of years. ntfs epoc 1 jan 1601 @ 0000 hrs utc, whereas java epoc 1 jan 1970. also, ntfs time given in 100's of nanoseconds.
taking account, still doesn't add up. going wrong?
public static final double secondsperjulianyear = 316880878; public static final double minutesperjulianyear = 5281347.966666667; public static final double secondsperminute = 60.0; public static final double millispersecond = 1000.0; public static final double yearsjavatimeahead = 369.0; public static long ntfsfilecreationtime(final file file) { filetime filecreationtime = null; try { path filepath = paths.get(file.touri()); if (files.exists(filepath)) { basicfileattributes attributes = files.readattributes(filepath, basicfileattributes.class); filecreationtime = attributes.creationtime(); } } catch (ioexception ex) { ex.printstacktrace(); } return getjavamillisfromntfstime(filecreationtime.tomillis()); } public static long getjavamillisfromntfstime(final long ntfstime) { double ntfsnanos = ntfstime * 100; // ntfs time unit equal 100 nanoseconds double ntfsmillis = (ntfsnanos / 10e6); // there 10e6 nanoseconds in millisecond double millisjavaepocahead = yearsjavatimeahead * minutesperjulianyear * secondsperminute * millispersecond; return (long) (ntfsmillis - millisjavaepocahead); } according windows 7, creation date is: 1/6/2016 console output of 1 of files: system.currenttimemillis(): 1454896412295 filecreationtime(filename): -116929029460761 file age: 118383925873056
from javadoc filetime
public static filetime frommillis(long value)
returns
a filetime representing given value in milliseconds.
parameters:
value - value, in milliseconds, since epoch (1970-01-01t00:00:00z); can negative returns: filetime representing given value
note, epoch same, , time in millis.
so method should be
public static long getjavamillisfromntfstime(final long ntfstime) { return ntfstime; }
note: when exception, can't continue , pretend didn't happen. in case nullpointerexception. suggest either don't catch ioexception or return invalid value long.min_value
Comments
Post a Comment