ParseException:无法解析日期 - 类:java.text.DateFormat
Posted
技术标签:
【中文标题】ParseException:无法解析日期 - 类:java.text.DateFormat【英文标题】:ParseException: Could not parse date - Class: java.text.DateFormat 【发布时间】:2015-08-12 01:25:03 【问题描述】:我的问题与关于从 JSP 获取日期的问题不同。
现在我有一个使用 Struts 2.2.1 的服务器
而Action
是这样的:
public class AddMeetingAction extends ActionSupport
private A a;
@Override
public String execute() throws Exception
....
//getter and setter..
而A
类是这样的:
public class A
private java.util.Date b;
....
当我使用 Postman 向此操作发出带有参数 "b":"2015-01-01"
的 HTTP POST
请求时,我可以正确获取 b
。
或者如果我使用 jQuery $.ajax()
、datatype=json
编写 javascript,并从引导程序 datetime-picker 插件获取日期。没关系。
但是,当 others 使用 ios 调用此操作,发送相同的字符串时,服务器会抛出异常:
Could not parse date - Class: java.text.DateFormat
File: DateFormat.java
Method: parse
Line: 357 - java/text/DateFormat.java:357:-1
at com.opensymphony.xwork2.conversion.impl.XWorkBasicConverter.doConvertToDate(XWorkBasicConverter.java:377)
at com.opensymphony.xwork2.conversion.impl.XWorkBasicConverter.convertValue(XWorkBasicConverter.java:115)
at com.opensymphony.xwork2.conversion.impl.XWorkBasicConverter.convertValue(XWorkBasicConverter.java:146)
at com.opensymphony.xwork2.conversion.impl.XWorkConverter.convertValue(XWorkConverter.java:334)
at com.opensymphony.xwork2.ognl.OgnlTypeConverterWrapper.convertValue(OgnlTypeConverterWrapper.java:39)
at ognl.OgnlRuntime.getConvertedType(OgnlRuntime.java:1060)
at ognl.OgnlRuntime.getConvertedTypes(OgnlRuntime.java:1077)
at ognl.OgnlRuntime.getConvertedMethodAndArgs(OgnlRuntime.java:1103)
at ognl.OgnlRuntime.getAppropriateMethod(OgnlRuntime.java:1189)
at ognl.OgnlRuntime.callAppropriateMethod(OgnlRuntime.java:1203)
at ognl.OgnlRuntime.setMethodValue(OgnlRuntime.java:1474)
at ognl.ObjectPropertyAccessor.setPossibleProperty(ObjectPropertyAccessor.java:85)
at ognl.ObjectPropertyAccessor.setProperty(ObjectPropertyAccessor.java:162)
at com.opensymphony.xwork2.ognl.accessor.ObjectAccessor.setProperty(ObjectAccessor.java:27)
at ognl.OgnlRuntime.setProperty(OgnlRuntime.java:2245)
at ognl.ASTProperty.setValueBody(ASTProperty.java:127)
at ognl.SimpleNode.evaluateSetValueBody(SimpleNode.java:220)
at ognl.SimpleNode.setValue(SimpleNode.java:301)
at ognl.ASTChain.setValueBody(ASTChain.java:227)
at ognl.SimpleNode.evaluateSetValueBody(SimpleNode.java:220)
at ognl.SimpleNode.setValue(SimpleNode.java:301)
at ognl.Ognl.setValue(Ognl.java:737)
at com.opensymphony.xwork2.ognl.OgnlUtil.setValue(OgnlUtil.java:209)
at com.opensymphony.xwork2.ognl.OgnlValueStack.trySetValue(OgnlValueStack.java:173)
at com.opensymphony.xwork2.ognl.OgnlValueStack.setValue(OgnlValueStack.java:160)
at com.opensymphony.xwork2.ognl.OgnlValueStack.setValue(OgnlValueStack.java:151)
at com.opensymphony.xwork2.interceptor.ParametersInterceptor.setParameters(ParametersInterceptor.java:288)
at com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:199)
at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
at com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:207)
at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
at com.opensymphony.xwork2.interceptor.StaticParametersInterceptor.intercept(StaticParametersInterceptor.java:190)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
at org.apache.struts2.interceptor.MultiselectInterceptor.intercept(MultiselectInterceptor.java:75)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
at org.apache.struts2.interceptor.CheckboxInterceptor.intercept(CheckboxInterceptor.java:94)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
at org.apache.struts2.interceptor.FileUploadInterceptor.intercept(FileUploadInterceptor.java:243)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
at com.opensymphony.xwork2.interceptor.ModelDrivenInterceptor.intercept(ModelDrivenInterceptor.java:100)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
at com.opensymphony.xwork2.interceptor.ScopedModelDrivenInterceptor.intercept(ScopedModelDrivenInterceptor.java:141)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
at org.apache.struts2.interceptor.debugging.DebuggingInterceptor.intercept(DebuggingInterceptor.java:267)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
at com.opensymphony.xwork2.interceptor.ChainingInterceptor.intercept(ChainingInterceptor.java:142)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
at com.opensymphony.xwork2.interceptor.PrepareInterceptor.doIntercept(PrepareInterceptor.java:166)
at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
at com.opensymphony.xwork2.interceptor.I18nInterceptor.intercept(I18nInterceptor.java:176)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
at org.apache.struts2.interceptor.ServletConfigInterceptor.intercept(ServletConfigInterceptor.java:164)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
at com.opensymphony.xwork2.interceptor.AliasInterceptor.intercept(AliasInterceptor.java:190)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
at com.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor.intercept(ExceptionMappingInterceptor.java:187)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
at org.apache.struts2.impl.StrutsActionProxy.execute(StrutsActionProxy.java:52)
at org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:485)
at org.apache.struts2.dispatcher.ng.ExecuteOperations.executeAction(ExecuteOperations.java:77)
at org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:91)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1023)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:1852)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:724)
Caused by: java.text.ParseException: Unparseable date: "2015-01-01"
at java.text.DateFormat.parse(DateFormat.java:357)
at com.opensymphony.xwork2.conversion.impl.XWorkBasicConverter.doConvertToDate(XWorkBasicConverter.java:367)
... 79 more
【问题讨论】:
发布您解析该日期的代码。 @Aleksandr M not duplicate.i 无法从 ios call.jsp ok.thx 获取日期 @Saif 日期由 struts2 解析,不是我的代码。方法是 com.opensymphony.xwork2.conversion.impl.XWorkBasicConverter 类中的 convertValue 日期转换是根据地区来完成的。所以这是一个重复。 您需要:1)更改iOS发送的格式,或2)创建自定义转换器。 Reading this might help 【参考方案1】:如果您无法修改请求特定区域设置日期的发件人,那么您至少可以在请求中添加一个参数request_locale
。其余的由i18n
拦截器完成。您可以阅读this 答案以获得更多帮助。
例子:
<action name="someAction" class="com.examples.SomeAction">
<interceptor-ref name="i18n"/>
<interceptor-ref name="defaultStack"/>
<result name="success">good_result.jsp</result>
</action>
另一种解决方案是更改属性的类型以避免转换错误
public class A
private java.lang.String b;
....
如果您需要将其转换为Date
,您可以使用DateFormat
。
【讨论】:
我更改了属性类型,并使用SimpleDateFormat("yyyy-mm-dd HH:mm:ss")
class 中的sdf.parse
method 转换它。它有效。感谢您的帮助。以上是关于ParseException:无法解析日期 - 类:java.text.DateFormat的主要内容,如果未能解决你的问题,请参考以下文章
Java.text.ParseException:无法解析的日期[重复]
java.text.ParseException:无法解析的日期:java.text.DateFormat.parse(DateFormat.java:579)