Safe to convert Java.sql.date to Java.util.date by up casting?
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.u开发者_Go百科til.date? Or is there some other way to convert them?
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.
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.
Though you can easily obtain the java.sql.Date from java.util.Date, you need to be careful that java.sql.Date has only Date information minus time details.
If needed, you need to look at java.sql.Time or java.sql.Timestamp.
精彩评论