How to print time up to 6 digits of precision for seconds value

前端 未结 4 741
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-01 23:58

I have a value in column which is of type timestamp. Lets say I have a value 2007-05-04 08:48:40.969774

Now, when trying to fetch the value from the

4条回答
  •  南笙
    南笙 (楼主)
    2021-01-02 00:53

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

    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.

提交回复
热议问题