Display timestamp value in java from database

date value in the database 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 date value returned by the function above is 2011-03-19 18: 49: 04.0.

Why is this the addition of .0 at the end? How to remove it?

+3
source share
2 answers

The question is about formatting Date/ Timestamprather than internal precision (the date contains milliseconds). Try formatting Timestampso as not to show fractional seconds, use SimpleDateFormat, for example: Date of time stamp;

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.
}
+2
source

java.sql.Timestamp ( getTimestamp) , toString() . ( ).

, , java.sql.Timestamp java.util.Date, , . Javadoc:

java.util.Date . java.util.Date . - - . Timestamp.equals(Object) true , java.sql.Timestamp, nanos . Timestamp.equals(Object) java.util.Date.equals(Object). , hashcode java.util.Date , , nos .

- Timestamp java.util.Date, , , Timestamp java.util.Date. Timestamp java.util.Date , .

+4

All Articles