java.util.Date 格式 SSSSSS:如果不是微秒,最后 3 位数字是多少? [英] java.util.Date format SSSSSS: if not microseconds what are the last 3 digits?

查看:39
本文介绍了java.util.Date 格式 SSSSSS:如果不是微秒,最后 3 位数字是多少?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Just tested this code on both my Windows (8) workstation and an 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()));
    }

并得到了类似的结果:

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?

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

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:

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

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

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

谢谢.

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

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

推荐答案

来自 SimpleDateFormat 文档:

Letter     Date or Time Component     Presentation     Examples  
S          Millisecond                Number           978 

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

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...

你可以这样检查:

    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));

输出:

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小提琴)

这篇关于java.util.Date 格式 SSSSSS:如果不是微秒,最后 3 位数字是多少?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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