LocalDateTime.now()在Windows和Mac计算机上具有不同级别的精度 [英] LocalDateTime.now() has different levels of precision on Windows and Mac machine

查看:224
本文介绍了LocalDateTime.now()在Windows和Mac计算机上具有不同级别的精度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Mac和Windows计算机上使用LocalDateTime.now()创建新的LocalDateTime时,我在Mac上获得nano 精度为6 ,在纳米精度为3 在我的Windows机器上.两者都在运行jdk-1.8.0-172.

When creating a new LocalDateTime using LocalDateTime.now() on my Mac and Windows machine i get a nano precision of 6 on my Mac and a nano precision of 3 on my Windows machine. Both are running jdk-1.8.0-172.

  • 是否有可能限制或提高其中一个的精度 机器?
  • 为什么精度实际上不同?
  • Is it possible to limit or increase the precision on one of the machines?
  • And why is the precision actually different?

推荐答案

精度有所不同,因为

The precision is different because LocalDateTime.now() uses a system default Clock.

在默认时区中从系统时钟获取当前日期时间.

Obtains the current date-time from the system clock in the default time-zone.

这将查询系统时钟在默认时区中获取当前日期时间.

This will query the system clock in the default time-zone to obtain the current date-time.

...

此Javadoc中的链接将您带至Clock.systemDefaultZone(),其中指出(强调我的):

The link in this Javadoc takes you to Clock.systemDefaultZone() which states (emphasis mine):

获得使用最佳系统时钟返回当前时刻的时钟,并使用默认时区将其转换为日期和时间.

Obtains a clock that returns the current instant using the best available system clock, converting to date and time using the default time-zone.

此时钟基于最佳可用系统时钟. 这可能使用 System.currentTimeMillis (),如果有可用的话,也可以使用更高分辨率的时钟.

This clock is based on the best available system clock. This may use System.currentTimeMillis(), or a higher resolution clock if one is available.

...

Java使用哪个时钟取决于很多因素,看起来您的Mac计算机的时钟精度为 micro 秒,而您的Windows计算机的时钟精度为 milli 秒精度.我不知道有什么方法可以提高时钟的精度,但是您可以肯定地降低精度以使其跨平台匹配.

Which clock Java uses can depend on a lot of things and it looks like your Mac computer has a clock with microsecond precision whereas your Windows computer has a clock with millisecond precision. I'm not aware of any way to increase the precision of a clock but you can definitely decrease the precision so that it matches across platforms.

一种选择是使用 Ole V.V. ,并且使用

One option is to do as Ole V.V. does in his answer and use LocalDateTime.truncatedTo(TemporalUnit).

另一种选择是插入您自己的Clock并使用

Another option is to plug in your own Clock and use LocalDateTime.now(Clock). If possible, I would use Clock.tickMillis(ZoneId) since this method returns a Clock that truncates to milliseconds.

获得一个时钟,该时钟使用最佳可用系统时钟返回整毫秒内的当前即时滴答声.

Obtains a clock that returns the current instant ticking in whole milliseconds using the best available system clock.

此时钟将始终具有被截断为毫秒的纳秒字段.这样可以确保可见时间以毫秒为单位.基础时钟是最佳的可用系统时钟,等效于使用system(ZoneId).

This clock will always have the nano-of-second field truncated to milliseconds. This ensures that the visible time ticks in whole milliseconds. The underlying clock is the best available system clock, equivalent to using system(ZoneId).

...

由于:
9

Since:
9

这篇关于LocalDateTime.now()在Windows和Mac计算机上具有不同级别的精度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆