AJF
AJF

Reputation: 1931

Retrieve complete date in java from DB

I have a java servlet that calls a method in a java class named OAVDbUtil.java to obtain a date from a SQL database. I can retrieve the date but not the complete value. In the SQL Server database the field is "EventTime" and is a datetime field and an example of the date data is;

2015-02-16 11:48:15.730

In OAVDbUtil.java I have the code to retrieve the value in a results set and place it in a variable like this, where Date is of type java.sql.Date

Date loginDate = myRs.getDate("EventTime");

But it retrieves the data as "2015-02-16". How do I retrieve the date as "2015-02-16 11:48:15.730"?

Upvotes: 1

Views: 2598

Answers (2)

Jeff Miller
Jeff Miller

Reputation: 1444

Use getTimestamp:

Timestamp loginDate = myRs.getTimestamp("EventTime");

Use SimpleDateFormat to convert it to a String.

Upvotes: 0

dcsohl
dcsohl

Reputation: 7406

It's confusing, but important to note that, unlike java.util.Date, java.sql.Date really is just the date. The time-of-day is not included.

For what you want, you should use a java.sql.Timestamp.

Upvotes: 3

Related Questions