java 从数据库显示java中的时间戳值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5369455/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 10:47:35  来源:igfitidea点击:

display timestamp value in java from database

javajdbctimestamp

提问by Deep

the value of date in database is 2011-03-19 18:49:04

数据库中日期的值为 2011-03-19 18:49:04

Timestamp date;
ResultSet rs=smt.executeQuery("select * from posttopic where name='"+logn+"'");
while(rs.next()){
    name=rs.getString(1);
    title=rs.getString(2);
    subject=rs.getString(3);
    message=rs.getString(4);
    date=rs.getTimestamp(5);
    System.out.print(date);
}

the value of date the the above function is returning is 2011-03-19 18:49:04.0.

上述函数返回的日期值为 2011-03-19 18:49:04.0。

Why it is appending .0 at the end?How to remove it?

为什么它在末尾附加 .0?如何删除它?

采纳答案by David Oliván Ubieto

The question is about formatting the Date/Timestamp, not the internal precision (Date holds milliseconds). Try formatting the Timestampto not show the fractional seconds, use SimpleDateFormatfor example: Timestamp date;

问题是关于格式化Date/ Timestamp,而不是内部精度(日期保持毫秒)。尝试格式化Timestamp不显示小数秒,SimpleDateFormat例如使用:时间戳日期;

ResultSet rs=smt.executeQuery("select * from posttopic where name='"+logn+"'");
// Create a date formatter with pattern as required: year-month-day hour:minute:second
java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
while(rs.next())
{
    name=rs.getString(1);
    title=rs.getString(2);
    subject=rs.getString(3);
    message=rs.getString(4);
    date=rs.getTimestamp(5);
    System.out.print(sdf.format(date)); // Format the date using the specified pattern.
}

回答by skaffman

java.sql.Timestamp(as returned by getTimestamp) includes a nanosecond component, and its toString()method appends that nanosecond value to the end of the String. In your case, the nanosecond value is zero (your data only has one-second precision).

java.sql.Timestamp(由 返回getTimestamp)包括一个纳秒分量,它的toString()方法将该纳秒值附加到字符串的末尾。在您的情况下,纳秒值为零(您的数据只有一秒的精度)。

Note that while java.sql.Timestampis a subclass of java.util.Date, you should be careful about treating it as such. From the Javadoc:

请注意, whilejava.sql.Timestamp是 的子类java.util.Date,您应该小心对待它。从Javadoc

This type is a composite of a java.util.Dateand a separate nanoseconds value. Only integral seconds are stored in the java.util.Datecomponent. The fractional seconds - the nanos - are separate. The Timestamp.equals(Object)method never returns true when passed an object that isn't an instance of java.sql.Timestamp, because the nanos component of a date is unknown. As a result, the Timestamp.equals(Object)method is not symmetric with respect to the java.util.Date.equals(Object)method. Also, the hashcode method uses the underlying java.util.Dateimplementation and therefore does not include nanos in its computation.

Due to the differences between the Timestampclass and the java.util.Dateclass mentioned above, it is recommended that code not view Timestampvalues generically as an instance of java.util.Date. The inheritance relationship between Timestampand java.util.Datereally denotes implementation inheritance, and not type inheritance.

这种类型是 ajava.util.Date和单独的纳秒值的组合。组件中仅存储整数秒java.util.Date。小数秒 - 纳米 - 是分开的。Timestamp.equals(Object)当传递的对象不是 的实例时java.sql.Timestamp,该方法永远不会返回 true ,因为日期的 nanos 组件是未知的。结果,该Timestamp.equals(Object)方法相对于该java.util.Date.equals(Object)方法不对称。此外,hashcode 方法使用底层java.util.Date实现,因此在其计算中不包括 nanos。

由于Timestamp类和java.util.Date上面提到的类之间的差异,建议代码不要将Timestamp值一般视为 的实例java.util.DateTimestamp和之间的继承关系java.util.Date真正表示的是实现继承,而不是类型继承。