Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java library for dealing with win32 FILETIME?

Tags:

java

Are there any Java libraries around for dealing with the win32 FILETIME/ time intervals ? It's basically a 64 bit timestamp in 100ns intervals since January 1, 1601.

(For my particular needs, converting to/from java.util.Date or an appropriate joda time equivalent would do, although I'll need access to at least microsecond resolution - which neither seems to provide.)

like image 843
Lyke Avatar asked Mar 22 '11 22:03

Lyke


1 Answers

If you are fine with millisecond resolution, this would work:

/** Difference between Filetime epoch and Unix epoch (in ms). */
private static final long FILETIME_EPOCH_DIFF = 11644473600000L;

/** One millisecond expressed in units of 100s of nanoseconds. */
private static final long FILETIME_ONE_MILLISECOND = 10 * 1000;

public static long filetimeToMillis(final long filetime) {
    return (filetime / FILETIME_ONE_MILLISECOND) - FILETIME_EPOCH_DIFF;
}

public static long millisToFiletime(final long millis) {
    return (millis + FILETIME_EPOCH_DIFF) * FILETIME_ONE_MILLISECOND;
}

At this point, converting from ms to a Date object is quite straightforward.

like image 71
Niraj Tolia Avatar answered Sep 21 '22 01:09

Niraj Tolia