本文介绍了您如何用Java 8 Instant表示MS-DTYP`DATETIME`?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Microsoft规范中, DATETIME 表示为2个32位整数: low

In the Microsoft Spec, DATETIME is represented as 2 32-bit integers: low and high

参考:

这是长的 130280867040000000

因此,高点和低点都是用

So the the high and low computed with

int high = (int)(fullval >> 32);
int low = (int)fullval;

如此之高= 30333378
和低= 552794112

如何将这些计算为Java 8 Instant?

How do I compute these to a Java 8 Instant?

推荐答案

对于1秒精度的转换,您自己的答案就可以了。万一您还需要转换秒数,这是一种方法。

For converting with 1 second precision your own answer is just fine. In case you also need to convert the fraction of second, here’s one way to do that.

    Instant msFiletimeEpoch = Instant.parse("1601-01-01T00:00:00Z");
    // a tick is 100 nanoseconds
    int nanosPerTick = 100;
    long ticksPerSecond = TimeUnit.SECONDS.toNanos(1) / nanosPerTick;

    long fullval = 130_280_867_040_000_000L;

    long seconds = fullval / ticksPerSecond;
    long nanos = fullval % ticksPerSecond * nanosPerTick;

    Instant answer = msFiletimeEpoch.plusSeconds(seconds).plusNanos(nanos);

    System.out.println(answer);

输出为:

让我们尝试在原始价值上再加上1个勾号;

Let’s try to put 1 more tick on your oroginal value; it should add 100 nanoseconds.

    long fullval = 130_280_867_040_000_001L;



是这样的。

未来很远的时间要注意:根据您的报价Microsoft整数都是无符号的。 Java 已签名。因此,在30828年的某个时候,我们将开始获得非常错误的结果。万一 long 的值为负,以防万一我们应该抛出异常。

Caveat for very far future dates: According to your quote the Microsoft integers are both unsigned. A Java long is signed. So some time in year 30828 we will start getting results that are very wrong. Just in case we ought to throw an exception if the long value is negative.

这篇关于您如何用Java 8 Instant表示MS-DTYP`DATETIME`?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 11:29