如何打印秒数值最多6位精度的时间 [英] How to print time up to 6 digits of precision for seconds value
问题描述
我在列中有一个类型为timestamp的值。假设我有一个值 2007-05-04 08:48:40.969774
I have a value in column which is of type timestamp. Lets say I have a value 2007-05-04 08:48:40.969774
现在,当尝试从数据库中获取值时将此时间戳值返回给函数,我应该使用什么SimpleDateFormatter模式,以便返回秒旁边的小数部分。
Now, when trying to fetch the value from the database and return this timestamp value to a function, what SimpleDateFormatter pattern should I use so that the fraction part beside the seconds also gets returned.
我使用了 yyyy-MM-dd hh:mm:ss ,但只返回到秒,忽略了旁边的分数秒(0.969774)。我还需要帮助返回这个小数部分,精度为6位。
I have used yyyy-MM-dd hh:mm:ss, but that only returns till the seconds and ignores the fraction present beside the seconds(.969774). I need help in returning this fraction part also, with a 6-digits precision.
推荐答案
格式化<$的默认方式c $ c> java.util.Date (或 java.sql.Timestamp
)只有毫秒精度。您可以使用 yyyy-MM-dd hh:mm:ss.SSS
来获得毫秒精度。
The default ways of formatting a java.util.Date
(or java.sql.Timestamp
) has only millisecond precision. You can use yyyy-MM-dd hh:mm:ss.SSS
to get that millisecond precision.
A java.sql.Timestamp
实际上具有(最高)纳秒精度(假设数据库服务器和驱动程序实际支持它)。在Java 8中格式化它的最简单方法是将时间戳转换为 java.time.LocalDateTime
(使用 Timestamp.toLocalDateTime()
)并使用 java.time.format.DateTimeFormatter
中支持的 java.time
格式化选项到纳秒。
A java.sql.Timestamp
actually does have (up to) nanosecond precision (assuming the database server and the driver actually support it). The easiest way to format it in Java 8 is to convert the timestamp to a java.time.LocalDateTime
(using Timestamp.toLocalDateTime()
) and use the java.time
formatting options in java.time.format.DateTimeFormatter
which support up to nanoseconds.
如果使用Java 7或更早版本,则需要额外的工作,因为普通的日期格式化程序不支持它。例如,您可以使用日期格式设置,格式为 yyyy-MM-dd hh:mm:ss
(仅格式化为秒)并附加亚秒秒纳秒(带 Timestamp.getNanos()
你自己的适当零填充。
If you use Java 7 or earlier it will take some extra effort, as the normal date formatters don't support it. For example you could use a dateformatter with pattern yyyy-MM-dd hh:mm:ss
(to only format up to seconds) and append the sub-second nano seconds (with appropriate zero-padding) of Timestamp.getNanos()
yourself.
这篇关于如何打印秒数值最多6位精度的时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!