为什么纳米秒必须使用int而不是在爪哇

发布于 2025-01-24 17:03:17 字数 953 浏览 2 评论 0原文

我一直在进行此练习,这是

import java.time.*;
import java.util.*;
public class Exercise31 {  
    public static void main(String[] args){
        LocalDateTime dateTime = LocalDateTime.of(2016, 9, 16, 0, 0);
        LocalDateTime dateTime2 = LocalDateTime.now();
        int diffInNano = java.time.Duration.between(dateTime, dateTime2).getNano();
        long diffInSeconds = java.time.Duration.between(dateTime, dateTime2).getSeconds();
        long diffInMilli = java.time.Duration.between(dateTime, dateTime2).toMillis();
        long diffInMinutes = java.time.Duration.between(dateTime, dateTime2).toMinutes();
        long diffInHours = java.time.Duration.between(dateTime, dateTime2).toHours();
        System.out.printf("\nDifference is %d Hours, %d Minutes, %d Milli, %d Seconds and %d Nano\n\n",
                diffInHours, diffInMinutes, diffInMilli, diffInSeconds, diffInNano );
    }
}

纳秒代码不需要长时间而不是INT,因为纳秒范围内的纳秒?

I have been doing this exercise and this is the code

import java.time.*;
import java.util.*;
public class Exercise31 {  
    public static void main(String[] args){
        LocalDateTime dateTime = LocalDateTime.of(2016, 9, 16, 0, 0);
        LocalDateTime dateTime2 = LocalDateTime.now();
        int diffInNano = java.time.Duration.between(dateTime, dateTime2).getNano();
        long diffInSeconds = java.time.Duration.between(dateTime, dateTime2).getSeconds();
        long diffInMilli = java.time.Duration.between(dateTime, dateTime2).toMillis();
        long diffInMinutes = java.time.Duration.between(dateTime, dateTime2).toMinutes();
        long diffInHours = java.time.Duration.between(dateTime, dateTime2).toHours();
        System.out.printf("\nDifference is %d Hours, %d Minutes, %d Milli, %d Seconds and %d Nano\n\n",
                diffInHours, diffInMinutes, diffInMilli, diffInSeconds, diffInNano );
    }
}

Doesnt nanoseconds have to use long instead of int because the nanoseconds in the range?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

幽梦紫曦~ 2025-01-31 17:03:17

那是因为 documentation 说,我们的持续时间包括两个字段,一个是秒,另一个是纳米。
因此,当您要求两者之间的持续时间时,您将获得2个值:

diff = seconds + nanos

因此在这种情况下,纳米仅计数高达999,9999999(0.99 ...秒),因此整数就足够了。

所以...

如果您需要在纳米人中持续时间,则必须做类似的事情:

Long totalDurationNanos = (duration.getSeconds() * 1_000_000_000f) + duration.getNanos();

编辑:

如评论中提到的,在您的情况下有一种更简单的方法:

两者

java.time.Duration.between(dateTime, dateTime2).toNanos()

都会

ChronoUnit.NANOS.between(dateTime, dateTime2)

输出长期格式化的纳米秒持续时间

That's because like documentation says, we have a duration which consist of two fields, one is seconds and the other one is nanos.
So when you ask for duration between, you get 2 values :

diff = seconds + nanos

So in this case, nanos only count up to 999,999,999 (0.99... seconds), so integer is enough.

So ...

If you need duration in nanos, you'll have to do something like this :

Long totalDurationNanos = (duration.getSeconds() * 1_000_000_000f) + duration.getNanos();

EDIT :

As mentioned in comments, there is an easier way in your case :

Both

java.time.Duration.between(dateTime, dateTime2).toNanos()

And

ChronoUnit.NANOS.between(dateTime, dateTime2)

would output you long formatted nanosecond duration

我们的影子 2025-01-31 17:03:17

getnano()Javadocs:

Returns:
the nanoseconds within the second part of the length of the duration, from 0 to 999,999,999

getNano() JavaDocs:

Returns:
the nanoseconds within the second part of the length of the duration, from 0 to 999,999,999
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文