从数据库中显示java中的时间戳值 [英] display timestamp value in java from database

查看:638
本文介绍了从数据库中显示java中的时间戳值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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);
}

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

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

为什么它最后会附加.0?如何删除它?

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

推荐答案

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

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

这篇关于从数据库中显示java中的时间戳值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆