Kris Schouw Kris Schouw -4 years ago 176
Java Question

How can I convert a Timestamp into either Date or DateTime object?

I'm retrieving a timestamp object from a database using

, but I'd like an easy way to get the date in the format of
and the time in a format of
HH:MM xx
. I was tinkering around, it it looks as though I can do such by making use of the Date and/or DateTime objects within Java. Is that the best way to go, or do I even need to convert the timestamp to accomplish this? Any recommendations would be helpful.

while( {
Timestamp dtStart = resultSet.getTimestamp("dtStart");
Timestamp dtEnd = resultSet.getTimestamp("dtEnd");

// I would like to then have the date and time
// converted into the formats mentioned...

Answer Source

java.sql.Timestamp is a subclass of java.util.Date. So, just upcast it.

Date dtStart = resultSet.getTimestamp("dtStart");
Date dtEnd = resultSet.getTimestamp("dtEnd");

Using SimpleDateFormat and creating Joda DateTime should be straightforward from this point on.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download