java.util.Date 格式 SSSSSS:如果不是微秒,最后 3 位数字是多少?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19223171/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 15:12:24  来源:igfitidea点击:

java.util.Date format SSSSSS: if not microseconds what are the last 3 digits?

javadateformattimestamp

提问by maxxyme

Just tested this code on both my Windows (8) workstation and an AIX:

刚刚在我的 Windows (8) 工作站和 AIX 上测试了这段代码:

    public static void main(String[] args) {
        System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSSSSS").format(new Date()));
        System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSSSSS").format(new Date()));
    }

and got something similar to this as a result:

结果得到了类似的结果:

2013-10-07 12:53:26.000905
2013-10-07 12:53:26.000906

Can someone please explain me what are the last digits, if not microseconds?

如果不是微秒,有人可以解释我的最后一位数字是什么吗?

Note: I interact with a DB2 database in which chronological data is stored using timed columns as TIMESTAMP with 6 digits AFTER the seconds i.e. microseconds (IMO). But all those "timestamps" are created by requesting the following query:

注意:我与 DB2 数据库进行交互,其中使用定时列作为 TIMESTAMP 存储按时间顺序排列的数据,秒后有 6 位数字,即微秒 (IMO)。但是所有这些“时间戳”都是通过请求以下查询来创建的:

SELECT current timestamp as currenttimestamp FROM Table ( values (1)) temp

I wonder if, given the above results, I couldn't just use in my code new Date()instead of selecting the current timestampfrom the database.

我想知道,鉴于上述结果,我是否不能只在我的代码中使用new Date()而不是current timestamp从数据库中选择。

Thanks.

谢谢。

PS: I searched but found no relevant (answered) questions, like: Current time in microseconds in javaor Get time with hour, minute, second, millisecond, microsecond

PS:我搜索但没有发现相关(已回答)的问题,例如: Java 中的当前时间(以微秒为单位)以小时、分钟、秒、毫秒、微秒为单位获取时间

采纳答案by ppeterka

From the documentation of SimpleDateFormat:

SimpleDateFormat文档中

Letter     Date or Time Component     Presentation     Examples  
S          Millisecond                Number           978 

So it is milliseconds, or 1/1000th of a second. You just format it with on 6 digits, so you add 3 extra leading zeroes...

所以它是毫秒,或 1/1000 秒。您只需将其格式化为 6 位数字,因此您添加了 3 个额外的前导零...

You can check it this way:

你可以这样检查:

    Date d =new Date();
    System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S").format(d));
    System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SS").format(d));
    System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS").format(d));
    System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSSS").format(d));
    System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSSSS").format(d));
    System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSSSSS").format(d));

Output:

输出:

2013-10-07 12:13:27.132
2013-10-07 12:13:27.132
2013-10-07 12:13:27.132
2013-10-07 12:13:27.0132
2013-10-07 12:13:27.00132
2013-10-07 12:13:27.000132

(Ideone fiddle)

( Ideone 小提琴)

回答by Evgeniy Dorofeev

Use java.sql.Timestamp.toString if you want to get fractional seconds in text representation. The difference betwen Timestamp from DB and Java Date is that DB precision is nanoseconds while Java Date precision is milliseconds.

如果您想在文本表示中获得小数秒,请使用 java.sql.Timestamp.toString。时间戳与 DB 和 Java 日期之间的区别在于 DB 精度是纳秒,而 Java 日期精度是毫秒。

回答by tutejszy

I used yet another trick to format date with 6-digit precision (microseconds):

我使用了另一个技巧以 6 位精度(微秒)格式化日期:

System.out.println(
    new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.").format(microseconds/1000)
    +String.format("%06d", microseconds%1000000));

This technique can be extended further, to nanoseconds and up.

这种技术可以进一步扩展到纳秒及以上。

回答by user9301292

SSSSSS is microseconds. Let us say the time is 10:30:22 (Seconds 22) and 10:30:22.1 would be 22 seconds and 1/10 of a second . Extending the same logic , 10:32.22.000132 would be 22 seconds and 132/1,000,000 of a second, which is nothing but microseconds.

SSSSSS 是微秒。假设时间是 10:30:22(22 秒),10:30:22.1 是 22 秒和 1/10 秒。扩展相同的逻辑, 10:32.22.000132 将是 22 秒和 132/1,000,000 秒,这只不过是微秒。

回答by Basil Bourque

tl;dr

tl;博士

Instant.now()
       .toString() 

2018-02-02T00:28:02.487114Z

2018-02-02T00:28:02.487114Z

Instant.parse(
    "2018-02-02T00:28:02.487114Z"
)

java.time

时间

The accepted Answerby ppeterka is correct. Your abuse of the formatting pattern results in an erroneous display of data, while the internal value is always limited milliseconds.

接受的答案被ppeterka是正确的。您对格式模式的滥用会导致数据显示错误,而内部值始终限制为毫秒。

The troublesome SimpleDateFormatand Dateclasses you are using are now legacy, supplanted by the java.timeclasses. The java.timeclasses handle nanosecondsresolution, much finer than the millisecondslimit of the legacy classes.

您使用的麻烦SimpleDateFormatDate类现在是遗留的,由java.time类取代。该java.time类处理纳秒的分辨率,更精细的比毫秒限制遗留类。

The equivalent to java.util.Dateis java.time.Instant. You can even convert between them using new methods added to the old classes.

相当于java.util.Datejava.time.Instant。您甚至可以使用添加到旧类中的新方法在它们之间进行转换。

Instant instant = myJavaUtilDate.toInstant() ;

The Instantclass represents a moment on the timeline in UTCwith a resolution of nanoseconds(up to nine (9) digits of a decimal fraction).

Instant级表示时间轴上的时刻UTC,分辨率为纳秒(最多小数的9个位数)。

Capture the current moment in UTC. Java 8 captures the current moment in milliseconds, while a new Clockimplementation in Java 9 captures the moment in finer granularity, typically microseconds though it depends on the capabilities of your computer hardware clock & OS & JVM implementation.

以 UTC 格式捕获当前时刻。Java 8 以毫秒为单位捕获当前时刻,而ClockJava 9 中的新实现以更精细的粒度捕获时刻,通常为微秒,尽管这取决于计算机硬件时钟、操作系统和 JVM 实现的功能。

Instant instant = Instant.now() ;

Generate a String in standard ISO 8601 format.

生成标准 ISO 8601 格式的字符串。

String output = instant.toString() ;

2018-02-02T00:28:02.487114Z

2018-02-02T00:28:02.487114Z

To generate strings in other formats, search Stack Overflow for DateTimeFormatter, already covered many times.

要生成其他格式的字符串,请在 Stack Overflow 中搜索DateTimeFormatter,已经覆盖了很多次。

To adjust into a time zone other than UTC, use ZonedDateTime.

要调整到 UTC 以外的时区,请使用ZonedDateTime.

ZonedDateTime zdt = instant.atZone( ZoneId.of( "Pacific/Auckland" ) ) ;


About java.time

关于java.time

The java.timeframework is built into Java 8 and later. These classes supplant the troublesome old legacydate-time classes such as java.util.Date, Calendar, & SimpleDateFormat.

java.time框架是建立在Java 8和更高版本。这些类取代了麻烦的旧的遗留日期时间类,例如java.util.Date, Calendar, & SimpleDateFormat

The Joda-Timeproject, now in maintenance mode, advises migration to the java.timeclasses.

现在处于维护模式Joda-Time项目建议迁移到java.time类。

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.

要了解更多信息,请参阅Oracle 教程。并在 Stack Overflow 上搜索许多示例和解释。规范是JSR 310

Where to obtain the java.time classes?

从哪里获得 java.time 类?

The ThreeTen-Extraproject 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.

ThreeTen-额外项目与其他类扩展java.time。该项目是未来可能添加到 java.time 的试验场。你可能在这里找到一些有用的类,比如IntervalYearWeekYearQuarter,和更多