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

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

问题描述

在我的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()));
}

并得到类似的结果:

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

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



注意:我与一个DB2数据库进行交互,其中使用TIMESTAMP作为TIMESTAMP存储使用时间顺序数据的时间顺序数据,秒数即秒数(IMO)之后。
但是所有这些时间戳都是通过请求以下查询创建的:

 将当前时间戳作为currenttimestamp FROM表(值(1))temp 

我想知道,鉴于上述结果,我无法使用在我的代码 new Date()而不是从数据库中选择当前时间戳



谢谢。



PS:我搜索但没有找到相关(回答)的问题,如:
java当前时间(微秒)

以小时,分,秒,毫秒,微秒为单位的时间

解决方案

Simp文档leDateFormat

 信函日期或时间组件演示示例
S毫秒数978

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



您可以通过以下方式检查:

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

输出:

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


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

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:

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 timestamp from the database.

Thanks.

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

解决方案

From the documentation of 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...

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)

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

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