Search code examples
javadatedate-formatsimpledateformat

Get the dd-MM-yyyy format of date in Java (JSP)


I'm using an input=date calendar, and i want to output this format of date (dd-MM-yyyy) in my jsp ,when i choose the 2nd day of april 2014 in the calendar given by the input ,I have this output in my jsp:

Wed Apr 02 00:00:00 WET 2014

Here you are the code:

index.jsp :

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>

    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <form name="datepickeer" action="showdates.jsp" method="POST">
            <table>
                <tr><td>Date début :</td> <td><input type = "date" name = "datedebut">
                    </td><tr>
                <tr><td><input type = "submit" name = "submit" value = "submit">
                    </td></tr>
            </table>
        </form>
    </body>
</html>

showdates.jsp :

<%@ page import="java.util.Date,java.text.SimpleDateFormat,java.text.ParseException"%>
<%@page import="java.text.SimpleDateFormat"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <% String dateStr = request.getParameter("datedebut");
            SimpleDateFormat formater = new SimpleDateFormat("yyyy-MM-dd");
            Date result = formater.parse(dateStr);
            out.println(result);
        %>
    </body>
</html>

What's the problem with my code?


Solution

  • String dateStr = request.getParameter("datedebut");
      SimpleDateFormat formater = new SimpleDateFormat("yyyy-MM-dd");
      Date result = formater.parse(dateStr);
      SimpleDateFormat AppDateFormat = new SimpleDateFormat("dd-MM-yyyy");
      out.println(AppDateFormat.format(result));