在java中显示数据库中的时间戳值
数据库中日期的值为 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);
}
上述函数返回的日期值为 2011-03-19 18:49:04.0。
为什么最后附加.0?如何删除它?
the value of date in database is
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.
Why it is appending .0 at the end?How to remove it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
java.sql.Timestamp
(由getTimestamp
返回)包含纳秒组件,其toString()
方法将该纳秒值附加到末尾字符串的。在您的情况下,纳秒值为零(您的数据只有一秒精度)。请注意,虽然
java.sql.Timestamp
是java.util.Date
的子类,但您在对待它时应该小心。来自 Javadoc:java.sql.Timestamp
(as returned bygetTimestamp
) includes a nanosecond component, and itstoString()
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).Note that while
java.sql.Timestamp
is a subclass ofjava.util.Date
, you should be careful about treating it as such. From the Javadoc:问题是关于格式化
Date
/Timestamp
,而不是内部精度(Date 保存毫秒)。尝试格式化Timestamp
以不显示小数秒,使用SimpleDateFormat
例如:时间戳日期;
The question is about formatting the
Date
/Timestamp
, not the internal precision (Date holds milliseconds). Try formatting theTimestamp
to not show the fractional seconds, useSimpleDateFormat
for example:Timestamp date;