Endianness
One important piece of information that you didn't provide is the byte endianness of the binary number. I will assume that it's little endian, as in most systems.
Extracting seconds and nanoseconds
Instant
in Java 8 supports times specified with seconds since epoch as a long
(63 bits without counting the sign), in addition to the nanoseconds.
So the good news is that we are able to print all the dates that could be stored as nanoseconds in the 8-byte unsigned integer.
Since Java only has a signed long
8 byte primitive data type, we need to be careful to not handle the case where the most significant bit is 1 as a negative 2's complement number.
Possible solution
// constants
long nanosPerSecond = 1000_000_000L;
String pattern = "yyyy-MM-dd HH:mm:ss.n";
// assuming the bytes in the file are in little endian byte order
byte[] arr = Files.readAllBytes(path);
// transform to big endian (required by BigInteger)
ArrayUtils.reverse(arr);
// always positive
BigInteger number = new BigInteger(1, arr);
// get seconds since epoch and nano adjustment
long nanoAdjustment = number.mod(BigInteger.valueOf(nanosPerSecond))
.longValueExact();
long epochSecond = number.divide(BigInteger.valueOf(nanosPerSecond))
.longValueExact();
Instant instant = Instant.ofEpochSecond(epochSecond, nanoAdjustment);
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern).withZone(ZoneId.systemDefault());
System.out.println(formatter.format(instant));
So the latest date we can show, represented by FF FF FF FF FF FF FF FF
corresponds to the string 2554-07-22 01:34:33.709551615
.
00 00 00 00 00 00 00 00
will be epoch.