英文:
util date to the sql date typecasting
问题
java.util.Date utilDate = new java.util.Date();
// giving error
java.sql.Date sqlDate = (java.sql.Date) utilDate;
// correct
java.sql.Date sqlDate = new java.sql.Date(utilDate.getTime());
Why I am unable to typecast the util.date to SQL.date
英文:
java.util.Date utilDate = new java.util.Date();
// giving error
java.sql.Date sqlDate= (java.sql.Date) utilDate;
// correct
java.sql.Date sqlDate = new java.sql.Date(utilDate.getTime());
Why I am unable typecasting the util.date to SQL.date
答案1
得分: 0
java.util.Date
是java.sql.Date
的父类。在Java中,父类型无法转换为子类型。
private static class A {
}
private static class B extends A {
}
public static void main(String[] args) {
A a = new A();
B b = new B();
B ba = (B) a; // java.lang.ClassCastException
}
英文:
java.util.Date
is a parent of java.sql.Date
. In Java parent type cannot be casted to child.
private static class A {
}
private static class B extends A {
}
public static void main(String[] args) {
A a = new A();
B b = new B();
B ba = (B) a; // java.lang.ClassCastException
}
答案2
得分: 0
以下是翻译好的代码部分:
{
java.util.Calendar calender = Calendar.getInstance();
java.util.Date dateInUtils = new java.util.Date();
calender.setTime(dateInUtils);
calender.set(Calendar.HOUR_OF_DAY, 0);
calender.set(Calendar.MINUTE, 0);
calender.set(Calendar.SECOND, 0);
calender.set(Calendar.MILLISECOND, 0);
java.sql.Date dateInSql = new java.sql.Date(calender.getTime().getTime());
System.out.println("dateInUtils:" + dateInUtils);
System.out.println("dateInSql:" + dateInSql);
}
英文:
Can you please try the below solution to convert date in util to sql date:
{
java.util.Calendar calender = Calendar.getInstance();
java.util.Date dateInUtils = new java.util.Date();
calender.setTime(dateInUtils);
calender.set(Calendar.HOUR_OF_DAY, 0);
calender.set(Calendar.MINUTE, 0);
calender.set(Calendar.SECOND, 0);
calender.set(Calendar.MILLISECOND, 0);
java.sql.Date dateInSql = new java.sql.Date(calender.getTime().getTime());
System.out.println("dateInUtils:" + dateInUtils);
System.out.println("dateInSql:" + dateInSql);
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论