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

本文探讨了Java中日期格式化产生的最后几位数,发现它们代表的是毫秒。作者通过代码示例和数据库查询,解释了这些数字的含义,并质疑是否可以直接使用`new Date()`代替数据库查询获取精确到毫秒的时间。
摘要由CSDN通过智能技术生成

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.

解决方案

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值