`

java.text.ParseException: Unparseable date:

阅读更多

java.text.ParseException: Unparseable date: "201011"
	at java.text.DateFormat.parse(DateFormat.java:337)
	at com.xjgz.util.DateUtils.strToDate(DateUtils.java:36)
	at com.xjgz.action.TemplateAnalysisAction.findIndiDataByDate(TemplateAnalysisAction.java:153)
	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:441)
	at com.opensymphony.xwork2.DefaultActionInvocation.invokeActionOnly(DefaultActionInvocation.java:280)
	at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:243)
	at com.opensymphony.xwork2.interceptor.DefaultWorkflowInterceptor.doIntercept(DefaultWorkflowInterceptor.java:165)
	at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)
	at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
	at com.opensymphony.xwork2.validator.ValidationInterceptor.doIntercept(ValidationInterceptor.java:252)
	at org.apache.struts2.interceptor.validation.AnnotationValidationInterceptor.doIntercept(AnnotationValidationInterceptor.java:68)
	at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)
	at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
	at com.opensymphony.xwork2.interceptor.ConversionErrorInterceptor.intercept(ConversionErrorInterceptor.java:122)
	at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
	at com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:195)
	at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)

 只贴了其中一部分的错误信息.

这个问题也是研究了1个小时 才发现问题所在.

例如1:

String nowDate = "20100205";
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
date = sdf.parse();
System.out.println(date);

 当前nowDate时间格式为20100205 ,而需要格式化成yyyy-MM-dd,这时就会报以上的错误.

修改时需要吧时间格式修改为"yyyyMMdd"才可以,或者修改时间格式为"2010-02-05".

 

例如2:

 

String nowDate = "2010-02";
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
date = sdf.parse();
System.out.println(date);

 

 当前nowDate时间格式为2010-02 ,而需要格式化成yyyy-MM-dd,这样也会报以上的错误.

修改时需要吧时间格式修改为"yyyy-MM",或者修改时间格式为"2010-02-05".


总结:在格式化字符型时间为日期型时,需要将格式设置一致才可以.

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics