简体   繁体   中英

Converting a String of java.sql.Date format to a String of java.util.date format

I am trying to convert sql date string to normal date, but I am getting this exception

java.text.ParseException: Unparseable date: "2013-03-11" at java.text.DateFormat.parse(Unknown Source) at com.ninenexus.simplesignworkflow.Task.getTaskDetails(Task.java:1385) at org.apache.jsp.task_jsp._jspService(task_jsp.java:259) at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70) at javax.servlet.http.HttpServlet.service(HttpServlet.java:722) at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:417) at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:391) at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334) at javax.servlet.http.HttpServlet.service(HttpServlet.java:722) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:306) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:240) at org.apache.catalina.core.StandardContextValve.invoke(Stan dardContextValve.java:161) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:164) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:380) at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:243) at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:188) at 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) at java.lang.Thread.run(Unknown Source)

I want like this

From "2013-03-11" to "11/03/2013" how can i FIX it?

What you are doing is simply unreasonable.

First, java.sql.Date is already a java.util.Date.

Second, java.sql.Date is NOT a SQL date string. Both java.sql.Date and java.util.Date has nothing to do with the format how the date is going to be displayed. ie There is no such thing as a Date containing "2013-03-11" while another Date containing "11/03/2013".

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

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

Take a help of SimpleDateFormat class and parse date string to get 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);
        }
    }

Try it in this way:

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));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM