将java.sql.Date格式的String转换为java.util.date格式的String

时间:2013-04-03 07:10:56

标签: java date

我正在尝试将sql日期字符串转换为正常日期,但我收到此异常

  

java.text.ParseException:无法解析的日期:“2013-03-11”       at java.text.DateFormat.parse(Unknown Source)       在com.ninenexus.simplesignworkflow.Task.getTaskDetails(Task.java:1385)       在org.apache.jsp.task_jsp._jspService(task_jsp.java:259)       在org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)       在javax.servlet.http.HttpServlet.service(HttpServlet.java:722)       在org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:417)       在org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:391)       在org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)       在javax.servlet.http.HttpServlet.service(HttpServlet.java:722)       在org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:306)       在org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)       在org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:240)       at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:161)       在org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:164)       at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)       在org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)       在org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:380)       在org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:243)       在org.apache.coyote.http11.Http11Protocol $ Http11ConnectionHandler.process(Http11Protocol.java:188)       在org.apache.coyote.http11.Http11Protocol $ Http11ConnectionHandler.process(Http11Protocol.java:166)       at org.apache.tomcat.util.net.JIoEndpoint $ SocketProcessor.run(JIoEndpoint.java:288)       at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)       at java.util.concurrent.ThreadPoolExecutor $ Worker.run(Unknown Source)       在java.lang.Thread.run(未知来源)

我想要这样

"2013-03-11""11/03/2013" 我怎么能修好它?

4 个答案:

答案 0 :(得分:3)

你在做什么是不合理的。

首先,java.sql.Date已经是java.util.Date。

其次,java.sql.Date不是SQL日期字符串。 java.sql.Date和java.util.Date都与日期的显示格式无关。即没有包含“2013-03-11”的日期,而另一个日期包含“11/03/2013”​​。

答案 1 :(得分:1)

DateFormat df = new SimpleDateFormat("dd/MM/yyyy");  
String text = df.format(date);  

System.out.println("The date is: " + text); 

答案 2 :(得分:0)

获取SimpleDateFormat班级和parse日期字符串的帮助以获取Util Date object

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

    public class DateConverter {

        public static void main(String[] args) throws ParseException {

            String dateString = "03/04/2013";
            Date utiDate;
            SimpleDateFormat dateFormater = new SimpleDateFormat("dd/MM/yyyy");
            utiDate = dateFormater.parse (dateString);
            System.out.println(utiDate);
        }
    }

答案 3 :(得分:0)

以这种方式尝试:

DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
Date date = df.parse("2013-03-11");   // convert string to date
df=new SimpleDateFormat("dd/MM/yyyy");
// date formatted in whatever format you want. 
System.out.println("The date is: " + df.format(date));