Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting java.sql.Date to java.util.Date

Tags:

java

jdbc

What's the simplest way to convert a java.sql.Date object to a java.util.Date while retaining the timestamp?

I tried:

java.util.Date newDate = new Date(result.getDate("VALUEDATE").getTime()); 

with no luck. It's still only storing the date portion into the variable.

like image 818
mservidio Avatar asked May 20 '11 00:05

mservidio


People also ask

What is difference between java sql date and java Util date?

sql. Date just represent DATE without time information while java. util. Date represents both Date and Time information.

How is date stored in database 1 point java sql date java Util date java sql datetime java Util datetime?

Its main purpose is to represent SQL DATE, which keeps years, months and days. No time data is kept. In fact, the date is stored as milliseconds since the 1st of January 1970 00:00:00 GMT and the time part is normalized, i.e. set to zero. Basically, it's a wrapper around java.

Should I use sql date or UTIL date?

util. Date class which represents date without time information and it should be used only when dealing with databases. To conform with the definition of SQL DATE, the millisecond values wrapped by a java. sql.


1 Answers

The class java.sql.Date is designed to carry only a date without time, so the conversion result you see is correct for this type. You need to use a java.sql.Timestamp to get a full date with time.

java.util.Date newDate = result.getTimestamp("VALUEDATE"); 
like image 184
x4u Avatar answered Sep 27 '22 22:09

x4u