问题描述
- Java中使用SimpleDateFormat转换异常
-
异常代码:
String min = request.getParameter("minTime");
String max = request.getParameter("maxTime");
Date startTime = dateFormat.parse(dateFormat.format(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(min)));Date stopTime = dateFormat.parse(dateFormat.format(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(max)));
异常信息:
java.text.ParseException: Unparseable date: "6/1/2015 14:23:05"
at java.text.DateFormat.parse(DateFormat.java:357)
at com.pumandai.cps.controller.CpsController.toCpsLists(CpsController.java:126)
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:606)
at org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:219)
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:132)
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:745)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:686)
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:80)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:925)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:856)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:936)
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:838)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:812)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:857)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
at java.lang.Thread.run(Thread.java:745)
解决方案
java.text.ParseException: Unparseable date: "6/1/2015 14:23:05"
异常已经说明了你传入的日期格式与你定义的格式yyyy-MM-dd HH:mm:ss不符合,你传入的应该是2015-06-01 14:23:05才行。
不明白为什么你要用三次dateformat,new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(min)不就可以得到Date对象了么?
解决方案二:
直接 使用一次将字符串转换为Date类型就可以了吧
解决方案三:
你看下这个代码:
private static void strDateStr() {
String dateStr = "6/1/2015 14:23:05";
// String转换为Date,格式字符串必须与上面传入的日期字符串对应,不然会报ParseException
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
try {
Date date = sdf.parse(dateStr);
// 输出date日期,调用date的默认打印方法
System.out.println("默认的日期输出格式:" + date);
// 格式化date的输出样式,需要指定想要的格式,比如输出2015-06-01 12:11:11格式的
SimpleDateFormat outSdf = new SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss");
String outDateStr = outSdf.format(date);
System.out.println("格式化后的日期输出格式:" + outDateStr);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
输出的内容为:
默认的日期输出格式Mon Jun 01 14:23:05 CST 2015
格式化后的日期输出格式:2015-06-01 14:23:05
解决方案四:
初始化SimpleDateFormat时使用的格式字符串与你传入的日期格式不一致。