Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Safe to convert Java.sql.date to Java.util.date by up casting?

Tags:

java

date

jdbc

Java.sql.date extends java.util.date, so is it save to convert between the two by casting a java.sql.date as a java.util.date? Or is there some other way to convert them?

like image 352
Daniel Bingham Avatar asked Jan 29 '10 20:01

Daniel Bingham


People also ask

What is the difference between Java Util date and Java sql date?

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

What is wrong with Java Util date?

util. Date (just Date from now on) is a terrible type, which explains why so much of it was deprecated in Java 1.1 (but is still being used, unfortunately). Design flaws include: Its name is misleading: it doesn't represent a Date , it represents an instant in time.


2 Answers

You don't necessarily need to cast, you can just treat a SQL Date as if it was a util Date:

java.sql.Date sqlDate = new java.sql.Date(whenever);
java.util.Date utilDate = sqlDate;

Compiles and runs just fine.

like image 106
mattjames Avatar answered Sep 22 '22 14:09

mattjames


Yes, you can just upcast it.

The java.sql.Date is in fact nothing less or more than a representation of the SQL Date type, which has only year, month and day filled.

like image 35
BalusC Avatar answered Sep 20 '22 14:09

BalusC