现在用Java获取当前时间精度(微秒)的任何新方法? [英] Any new method to get current time with accuracy in microseconds in Java now?
问题描述
我在下面的页面中查看了2009年Java中没有方法可以获得精确的微秒级数据。
关于 java.time
java.time 框架已构建进入Java 8及更高版本。这些类取代了麻烦的旧遗留日期时间类,例如 java.util.Date
, 日历
,& SimpleDateFormat
。
Joda-Time 项目,现已进入维护模式 ,建议迁移到 java.time classes。
要了解更多信息,请参阅 Oracle Tutorial 。并搜索Stack Overflow以获取许多示例和解释。规范是 JSR 310 。
您可以直接与数据库交换 java.time 对象。使用符合 JDBC驱动程序 / jeps / 170rel =nofollow noreferrer> JDBC 4.2 或更高版本。不需要字符串,不需要 java.sql。*
类。
从哪里获取java.time班?
- Java SE 8 , Java SE 9 ,以及之后
- 内置。
- 带有捆绑实现的标准Java API的一部分。
- Java 9添加了一些小功能和修复。
- Java SE 6 和 Java SE 7
- 大部分java.time功能都被反向移植到Java 6& 7 ThreeTen-Backport 。
- Android
- 更新版本的Android捆绑实现java.time类。
- 对于早期的Android(< 26), ThreeTenABP 项目适应 ThreeTen-Backport (如上所述)。请参阅 如何使用ThreeTenABP ...... 。
ThreeTen-Extra 项目使用其他类扩展java.time。该项目是未来可能添加到java.time的试验场。您可以在这里找到一些有用的课程,例如 Interval
, YearWeek
, YearQuarter
,更多。
I checked the below page that there is no method to get current time with accuracy in microsecond in Java in 2009.
Current time in microseconds in java
The best one is System.currentTimeMillis() which gives current time with accuracy in millisecond, while System.nanoTime() gives the current timestamp with accuracy in nanoseconds, but this timestamp could not be used to convert to current time with high accuracy.
May I know if there is any new update in this for Java after 6 years? Thanks.
Edit 1. System.nanoTime() is useful for estimating duration, but not giving current time.
Edit 2. It is good to have solutions in Java 8. Is there any other way to do it in Java 7? Thanks!
tl;dr
Instant.now()
2018-03-09T21:03:33.831515Z
Using Java 9 and later, you can likely capture the current moment with microseconds resolution.
Example seen above is Oracle Java 9.0.4 for macOS Sierra. Note the 6 digits of fractional second, meaning microseconds.
java.time
The java.time framework built into Java 8 and later has classes to represent date-time values with a resolution of nanoseconds, 9 digits of a decimal fraction of a second.
Java 9
Java 9 has a fresh implementation of Clock
with up to nanosecond resolution. Actual values depend on the limits of the underlying hardware clock of any particular computer.
For more info see the Question, Why does the new Java 8 Date Time API not have nanosecond precision?.
Avoid the older date-time classes. The classes such as java.util.Date/.Calendar bundled with early versions of Java have only millisecond resolution. Same for Joda-Time. Both are supplanted by the java.time classes.
Java 8
Java 8 implements the Clock
interface with only millisecond resolution (3 digits of a fractional second). So while the java.time classes are capable of carrying nanoseconds, it is not able to capture the current time with nanoseconds.
Native code
Perhaps you could write or find an implementation of Clock
that makes a call to a native library in the host OS to get a more accurate time.
Database
If you happen to be using a database already, you could ask it for the time. As of JDBC 4.2 and later, you can directly exchange java.time objects with your database.
Instant instant = myPreparedStatement.getObject( … , Instant.class ) ;
Prior to JDBC 4.2, you could go through the troublesome legacy java.sql.Timestamp
that carries nanosecond resolution. For example, Postgres provides microsecond resolution (assuming the host computer clock supports that), 6 digits of a fractional second. You can convert from a java.sql.Timestamp
to an Instant
and ZonedDateTime
using new methods added to the old classes.
About java.time
The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date
, Calendar
, & SimpleDateFormat
.
The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.
To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.
You may exchange java.time objects directly with your database. Use a JDBC driver compliant with JDBC 4.2 or later. No need for strings, no need for java.sql.*
classes.
Where to obtain the java.time classes?
- Java SE 8, Java SE 9, and later
- Built-in.
- Part of the standard Java API with a bundled implementation.
- Java 9 adds some minor features and fixes.
- Java SE 6 and Java SE 7
- Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport.
- Android
- Later versions of Android bundle implementations of the java.time classes.
- For earlier Android (<26), the ThreeTenABP project adapts ThreeTen-Backport (mentioned above). See How to use ThreeTenABP….
The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval
, YearWeek
, YearQuarter
, and more.
这篇关于现在用Java获取当前时间精度(微秒)的任何新方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!