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
display timestamp value in java from database
提问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 Timestamp
to not show the fractional seconds, use SimpleDateFormat
for 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.Timestamp
is 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.Date
and a separate nanoseconds value. Only integral seconds are stored in thejava.util.Date
component. The fractional seconds - the nanos - are separate. TheTimestamp.equals(Object)
method never returns true when passed an object that isn't an instance ofjava.sql.Timestamp
, because the nanos component of a date is unknown. As a result, theTimestamp.equals(Object)
method is not symmetric with respect to thejava.util.Date.equals(Object)
method. Also, the hashcode method uses the underlyingjava.util.Date
implementation and therefore does not include nanos in its computation.Due to the differences between the
Timestamp
class and thejava.util.Date
class mentioned above, it is recommended that code not viewTimestamp
values generically as an instance ofjava.util.Date
. The inheritance relationship betweenTimestamp
andjava.util.Date
really denotes implementation inheritance, and not type inheritance.
这种类型是 a
java.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.Date
。Timestamp
和之间的继承关系java.util.Date
真正表示的是实现继承,而不是类型继承。