String strdate=this.getRequest().getParameter("vehicleInformTime")
System.out.println(strdate);
vehicleInformTime = DateFormate.getDateByStr(strdate);
/**
* 将String转换为Date
* @param str 如果日期str 为原始格式, dateFormat不用指定其格式
* @return
*/
public static Date getDateByStr(String str){
SimpleDateFormat dateFormat=new SimpleDateFormat();
Date date=null;
try {
date=dateFormat.parse(str);
} catch (ParseException e) {
System.out.println("日期转换错误");
e.printStackTrace();
}
return date;
}
strdate 打印出来后为:Sun Jan 02 2011 00:00:00 GMT+0800
日期转换错误
java.text.ParseException: Unparseable date: "Sun Jan 02 2011 00:00:00 GMT+0800"
at java.text.DateFormat.parse(DateFormat.java:337)
at com.jtd.shopcar.util.DateFormate.getDateByStr2(DateFormate.java:47)
at com.jtd.shopcar.action.ViewMaintainLogAction.doaddMaintainLog(ViewMaintainLogAction.java:253)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at com.opensymphony.xwork2.DefaultActionInvocation.invokeAction(DefaultActionInvocation.java:452)
at com.opensymphony.xwork2.DefaultActionInvocation.invokeActionOnly(DefaultActionInvocation.java:291)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:254)