刚刚在我的 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 存储,在秒后有 6 位数字,即微秒 (IMO)。 但是所有这些“时间戳”都是通过请求以下查询来创建的:
SELECT current timestamp as currenttimestamp FROM Table ( values (1)) temp
我想知道,鉴于上述结果,我是否不能只在我的代码中使用 new Date()
而不是从数据库中选择 current timestamp
。
谢谢。
PS:我搜索但没有找到相关(已回答)的问题,例如: Current time in microseconds in java 或者 Get time with hour, minute, second, millisecond, microsecond
最佳答案
来自 documentation of SimpleDateFormat :
Letter Date or Time Component Presentation Examples
S Millisecond Number 978
所以它是 毫秒,或 1/1000 秒。您只需将其格式化为 6 位数字,因此您添加了 3 个额外的前导零...
你可以这样检查:
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 fiddle )
关于java.util.Date 格式 SSSSSS : if not microseconds what are the last 3 digits?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19223171/