开发者

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜