java.lang.ClassCastException: java.util.Date cannot be cast to java.lang.Str

2024-11-26 09:45:03
推荐回答(4个)
回答1:

类型转换出错,
Data类型不能转成String
如果非要转的话用String.valueOf(data);
类型转换错误,不能把Date型强转为String型
java代码1
public int getAllUserNum(){
Session session = HiberSF.getSession();
try {
session.getTransaction().begin();
//解决办法直接调用:HashCode();
//count(*)也可以理解为返回一个Object的对象,然后直接
//调用 hashCode();
Integer lo = (Integer)session.createQuery("select count(*) from User").uniqueResult().hashCode();
session.getTransaction().commit();
return sum;
} catch (RuntimeException e) {
session.getTransaction().rollback();
throw e;
// TODO: handle exception
} finally {
session.close();
}
}

java代码1
public int getAllUserNum(){
Session session = HiberSF.getSession();
try {
session.getTransaction().begin();

Long lo = (Long)session.createQuery("select count(*) from User").uniqueResult();
//解决办法把long型拆分字符串进行转换
Integer sum = new Integer(String.valueOf(lo));
session.getTransaction().commit();
return sum;
} catch (RuntimeException e) {
session.getTransaction().rollback();
throw e;
// TODO: handle exception
} finally {
session.close();
}
}

回答2:

SimpleDateFormat sdf=new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒 "); //设置自己想要的时间格式
String aa=sdf.format(new Date());
System.out.println(aa);

回答3:

sdf的parse是将String转为Date类型,而你是要将date类型转成String应该用un.setPassTime(sdf.format(s[4]));

回答4:

sdf.format(date)