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

发布于 2024-10-24 05:42:00 字数 414 浏览 1 评论 0原文

数据库中日期的值为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

云归处 2024-10-31 05:42:01

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

请注意,虽然java.sql.Timestampjava.util.Date 的子类,但您在对待它时应该小心。来自 Javadoc

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

由于Timestamp类与上面提到的java.util.Date类之间存在差异,建议代码不要查看Timestamp > 值通常作为 java.util.Date 的实例。 Timestampjava.util.Date 之间的继承关系实际上表示实现继承,而不是类型继承。

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

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:

This type is a composite of a java.util.Date and a separate nanoseconds value. Only integral seconds are stored in the java.util.Date component. 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.Date implementation and therefore does not include nanos in its computation.

Due to the differences between the Timestamp class and the java.util.Date class mentioned above, it is recommended that code not view Timestamp values generically as an instance of java.util.Date. The inheritance relationship between Timestamp and java.util.Date really denotes implementation inheritance, and not type inheritance.

独夜无伴 2024-10-31 05:42:01

问题是关于格式化 Date/Timestamp,而不是内部精度(Date 保存毫秒)。尝试格式化 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.
}

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;

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.
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文