简体   繁体   中英

Java: resultSet.getTimestamp loses time

I have the following entry in my database:

Start                   End
2013-08-25 14:23:20     2013-09-14 14:23:20

(type is datetime, database is MySQL)

And in my java application I want to set this date and time to a calendar as followed:

java.sql.Timestamp timeStampStart = inResults.getTimestamp("start");
System.out.println("Start: " + timeStampStart);
Calendar start = new GregorianCalendar();
start.setTime(timeStampStart);

Edit:

added System.out.println to the code:

Start: 2013-08-25 15:11:08.0
End: 2013-09-14 15:11:08.0 (same code as for start)

Now the thing is, the time is lost when I call inResults.getTimestamp. I get the correct date, but the time is the time the queried is executed instead of the time in the database.

Any ideas?

No argument constructor

Constructs a default GregorianCalendar using the current time in the default time zone with the default locale.

You can create java.util.Date from your Timestamp then getHours(), getMinutes(), and getSeconds() from the Date and use it in Calendar's constructor. signature is below.

GregorianCalendar(int year, int month, int dayOfMonth, int hourOfDay, int minute, int second)

something like this:

    java.sql.Timestamp timeStampStart = inResults.getTimestamp("start"); 
    java.util.Date d = new java.util.Date(timeStampStart.getTime());
    Calendar start = new GregorianCalendar(d.getYear(), d.getMonth(), d.getDate(), d.getHours(), d.getMminutes(), d.getSeconds());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM