DateFormat.parse问题

时间:2014-05-15 14:03:36

标签: java jsp

我试图将字符串转换为日期。当我运行我的软件时,我得到了这个错误,很明显我正在解析它:

错误是这样的:

java.text.ParseException: Unparseable date: "2014-02-07 00:00:00"
   at java.text.DateFormat.parse(DateFormat.java:357)
   at com.parkinsoncontrol.WsParkinsonControl.consultConfiguration(WsParkinsonControl.java:200)
   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
   at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
   at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
   at java.lang.reflect.Method.invoke(Method.java:601)
   at org.glassfish.webservices.InstanceResolverImpl$1.invoke(InstanceResolverImpl.java:143)
  at com.sun.xml.ws.server.InvokerTube$2.invoke(InvokerTube.java:150)
  at com.sun.xml.ws.server.sei.EndpointMethodHandler.invoke(EndpointMethodHandler.java:261)
  at com.sun.xml.ws.server.sei.SEIInvokerTube.processRequest(SEIInvokerTube.java:100)
  at com.sun.xml.ws.api.pipe.Fiber.__doRun(Fiber.java:641)
  at com.sun.xml.ws.api.pipe.Fiber._doRun(Fiber.java:600)
  at com.sun.xml.ws.api.pipe.Fiber.doRun(Fiber.java:585)
  at com.sun.xml.ws.api.pipe.Fiber.runSync(Fiber.java:482)
  at com.sun.xml.ws.api.pipe.helper.AbstractTubeImpl.process(AbstractTubeImpl.java:116)
  at org.glassfish.webservices.MonitoringPipe.process(MonitoringPipe.java:142)
  at com.sun.xml.ws.api.pipe.helper.PipeAdapter.processRequest(PipeAdapter.java:119)
  at com.sun.xml.ws.api.pipe.Fiber.__doRun(Fiber.java:641)
  at com.sun.xml.ws.api.pipe.Fiber._doRun(Fiber.java:600)
  at com.sun.xml.ws.api.pipe.Fiber.doRun(Fiber.java:585)
  at com.sun.xml.ws.api.pipe.Fiber.runSync(Fiber.java:482)
  at com.sun.xml.ws.api.pipe.helper.AbstractTubeImpl.process(AbstractTubeImpl.java:116)
  at com.sun.enterprise.security.webservices.CommonServerSecurityPipe.processRequest(CommonServerSecurityPipe.java:212)
  at com.sun.enterprise.security.webservices.CommonServerSecurityPipe.process(CommonServerSecurityPipe.java:144)
  at com.sun.xml.ws.api.pipe.helper.PipeAdapter.processRequest(PipeAdapter.java:119)
  at com.sun.xml.ws.api.pipe.Fiber.__doRun(Fiber.java:641)
  at com.sun.xml.ws.api.pipe.Fiber._doRun(Fiber.java:600)
  at com.sun.xml.ws.api.pipe.Fiber.doRun(Fiber.java:585)
  at com.sun.xml.ws.api.pipe.Fiber.runSync(Fiber.java:482)
  at com.sun.xml.ws.server.WSEndpointImpl$2.process(WSEndpointImpl.java:314)
  at com.sun.xml.ws.transport.http.HttpAdapter$HttpToolkit.handle(HttpAdapter.java:608)
  at com.sun.xml.ws.transport.http.HttpAdapter.handle(HttpAdapter.java:259)
  at com.sun.xml.ws.transport.http.servlet.ServletAdapter.handle(ServletAdapter.java:162)
  at org.glassfish.webservices.JAXWSServlet.doPost(JAXWSServlet.java:145)
  at javax.servlet.http.HttpServlet.service(HttpServlet.java:754)
  at javax.servlet.http.HttpServlet.service(HttpServlet.java:847)
  at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1539)
  at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:281)
  at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
  at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:655)
  at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:595)
  at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:98)
  at com.sun.enterprise.web.PESessionLockingStandardPipeline.invoke(PESessionLockingStandardPipeline.java:91)
  at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:162)
  at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:330)
  at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:231)
  at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:174)
  at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:828)
  at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:725)
  at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:1019)
  at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:225)
  at com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:137)
  at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:104)
  at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:90)
  at com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:79)
  at com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:54)
  at com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:59)
  at com.sun.grizzly.ContextTask.run(ContextTask.java:71)
  at com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:532)
  at com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:513)
  at java.lang.Thread.run(Thread.java:722)

我正在开发的代码是:

DateFormat formatter; 
Date date ; 
formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss",Locale.ENGLISH);
    try {
        Date parsed = formatter.parse(dateI);
        System.out.append("La fecha es:"+parsed);
    } catch (ParseException ex) {
        Logger.getLogger(WsParkinsonControl.class.getName()).log(Level.SEVERE, null, ex);
    }

那么,我做错了什么?非常感谢和问候。

2 个答案:

答案 0 :(得分:2)

这是你的价值:

2014-02-07 00:00:00

这是您的格式:

yyyy-MM-dd'T'HH:mm:ss

他们不匹配。您的格式中间有T - 您的值不是。

如果 all 您的值有空格而不是T,请使用:

yyyy-MM-dd HH:mm:ss

如果某些值的值为T而某些值不是,则需要使用多个DateFormat个实例。

此外,您应该考虑在解析时要使用的时区。目前您只是使用系统默认时区。这是值的实际时区吗?

答案 1 :(得分:0)

你想这样做吗?

DateFormat formatter; 
Date date ; 
formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss",Locale.ENGLISH);
try {
    Date parsed = formatter.parse("2014-05-15T10:07:00");
    System.out.append("La fecha es:"+parsed);
}

输出: La fecha es:2014年5月15日10:07:00 EDT 2014