Java library for dealing with win32 FILETIME?

前端 未结 3 748
鱼传尺愫
鱼传尺愫 2021-02-06 11:28

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.

(

3条回答
  •  旧时难觅i
    2021-02-06 11:50

    Here's a Java 8+ java.time based solution that keeps 100-nanosecond precision:

    public static final Instant ZERO = Instant.parse("1601-01-01T00:00:00Z");
    
    public static long fromInstant(Instant instant) {
        Duration duration = Duration.between(ZERO, instant);
        return duration.getSeconds() * 10_000_000 + duration.getNano() / 100;
    }
    
    public static Instant toInstant(long fileTime) {
        Duration duration = Duration.of(fileTime / 10, ChronoUnit.MICROS).plus(fileTime % 10 * 100, ChronoUnit.NANOS);
        return ZERO.plus(duration);
    }
    

提交回复
热议问题