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











up vote
2
down vote

favorite
1












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?










share|improve this question
























  • Please do as this answer.
    – Tiny
    Apr 10 '14 at 9:46















up vote
2
down vote

favorite
1












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?










share|improve this question
























  • Please do as this answer.
    – Tiny
    Apr 10 '14 at 9:46













up vote
2
down vote

favorite
1









up vote
2
down vote

favorite
1






1





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?










share|improve this question















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?







java date date-format simpledateformat






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Apr 10 '14 at 9:15









Tiny

8,21375254483




8,21375254483










asked Apr 10 '14 at 9:01









Nihad KH

542211




542211












  • Please do as this answer.
    – Tiny
    Apr 10 '14 at 9:46


















  • Please do as this answer.
    – Tiny
    Apr 10 '14 at 9:46
















Please do as this answer.
– Tiny
Apr 10 '14 at 9:46




Please do as this answer.
– Tiny
Apr 10 '14 at 9:46












4 Answers
4






active

oldest

votes

















up vote
2
down vote



accepted










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





share|improve this answer

















  • 3




    Stack Overflow is a Q&A site wherein everyone upvotes each other upon agreement. Stack Overflow is not an old fashioned discussion forum wherein everyone repeats each other into an undigestable mess upon agreement.
    – BalusC
    May 12 '14 at 10:24


















up vote
5
down vote













Try this -



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





share|improve this answer























  • @Subharajyoti Majumde : I tried it and the output is : 2014-04-02 instead of the format i want : 02-04-2014
    – Nihad KH
    Apr 10 '14 at 9:21










  • Opps.... In that case - create new Formatter new SimpleDateFormat("dd-MM-yyyy"); and format the parsed date
    – Subhrajyoti Majumder
    Apr 10 '14 at 9:32










  • @Subharajyoti Majumde : I did this: <%String dateStr = request.getParameter("datedebut"); SimpleDateFormat formater2= new SimpleDateFormat("dd-MM-yyyy"); Date result = formater2.parse(dateStr); out.println(result);%> ,I choose the second of april 2014 and I got this output: Wed Oct 05 00:00:00 WET 7
    – Nihad KH
    Apr 10 '14 at 9:37










  • Answer updated...
    – Subhrajyoti Majumder
    Apr 10 '14 at 9:39










  • thank you so muuuuuuch you just save my life yeey wish u all the best
    – Nihad KH
    Apr 10 '14 at 9:48


















up vote
0
down vote













You have converted the input from index.jsp into Date using formater.parse. Now to show it on the output in showdates.jsp, you have to again convert into String using formater.format(result) to yyyy-MM-dd. Actually what you are doing is correct. Whatever you get from input you should first parse and then print because you will be sure that the input which came fits into what you require. Use this.



Date result = formater.parse(dateStr);
SimpleDateFormat formaterForOut = new SimpleDateFormat("dd-MM-yyyy");
String resultString = formaterForOut.format(result);
out.println(resultString);





share|improve this answer























  • I tried your code and I got the same output : Wed Apr 02 00:00:00 WET 2014
    – Nihad KH
    Apr 10 '14 at 9:27


















up vote
0
down vote













<% 

String dateStr = request.getParameter("datedebut");

java.text.SimpleDateFormat oldFormater = new SimpleDateFormat("dd-mm-yyyy");
java.util.Date resultInDate = oldFormater.parse(dateStr);

java.text.SimpleDateFormat newFormater = new SimpleDateFormat("dd-mm-yyyy");
String resultInString = newFormater.format(resultInDate);

out.println(resultInDate); // Wed Apr 02 00:00:00 WET 2014
out.println(resultInString); // 02-04-2014

/* convert String resultInString into java.sql.Date resultInDateSql */
java.sql.Date resultInDateSql = java.sql.Date.valueOf(resultInString);

/* set in query sql */
preparedStatement.setDate(1, resultInDateSql);

%>





share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














     

    draft saved


    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f22983392%2fget-the-dd-mm-yyyy-format-of-date-in-java-jsp%23new-answer', 'question_page');
    }
    );

    Post as a guest
































    4 Answers
    4






    active

    oldest

    votes








    4 Answers
    4






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    2
    down vote



    accepted










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





    share|improve this answer

















    • 3




      Stack Overflow is a Q&A site wherein everyone upvotes each other upon agreement. Stack Overflow is not an old fashioned discussion forum wherein everyone repeats each other into an undigestable mess upon agreement.
      – BalusC
      May 12 '14 at 10:24















    up vote
    2
    down vote



    accepted










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





    share|improve this answer

















    • 3




      Stack Overflow is a Q&A site wherein everyone upvotes each other upon agreement. Stack Overflow is not an old fashioned discussion forum wherein everyone repeats each other into an undigestable mess upon agreement.
      – BalusC
      May 12 '14 at 10:24













    up vote
    2
    down vote



    accepted







    up vote
    2
    down vote



    accepted






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





    share|improve this answer












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






    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Apr 10 '14 at 10:15









    user3091530

    4081619




    4081619








    • 3




      Stack Overflow is a Q&A site wherein everyone upvotes each other upon agreement. Stack Overflow is not an old fashioned discussion forum wherein everyone repeats each other into an undigestable mess upon agreement.
      – BalusC
      May 12 '14 at 10:24














    • 3




      Stack Overflow is a Q&A site wherein everyone upvotes each other upon agreement. Stack Overflow is not an old fashioned discussion forum wherein everyone repeats each other into an undigestable mess upon agreement.
      – BalusC
      May 12 '14 at 10:24








    3




    3




    Stack Overflow is a Q&A site wherein everyone upvotes each other upon agreement. Stack Overflow is not an old fashioned discussion forum wherein everyone repeats each other into an undigestable mess upon agreement.
    – BalusC
    May 12 '14 at 10:24




    Stack Overflow is a Q&A site wherein everyone upvotes each other upon agreement. Stack Overflow is not an old fashioned discussion forum wherein everyone repeats each other into an undigestable mess upon agreement.
    – BalusC
    May 12 '14 at 10:24












    up vote
    5
    down vote













    Try this -



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





    share|improve this answer























    • @Subharajyoti Majumde : I tried it and the output is : 2014-04-02 instead of the format i want : 02-04-2014
      – Nihad KH
      Apr 10 '14 at 9:21










    • Opps.... In that case - create new Formatter new SimpleDateFormat("dd-MM-yyyy"); and format the parsed date
      – Subhrajyoti Majumder
      Apr 10 '14 at 9:32










    • @Subharajyoti Majumde : I did this: <%String dateStr = request.getParameter("datedebut"); SimpleDateFormat formater2= new SimpleDateFormat("dd-MM-yyyy"); Date result = formater2.parse(dateStr); out.println(result);%> ,I choose the second of april 2014 and I got this output: Wed Oct 05 00:00:00 WET 7
      – Nihad KH
      Apr 10 '14 at 9:37










    • Answer updated...
      – Subhrajyoti Majumder
      Apr 10 '14 at 9:39










    • thank you so muuuuuuch you just save my life yeey wish u all the best
      – Nihad KH
      Apr 10 '14 at 9:48















    up vote
    5
    down vote













    Try this -



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





    share|improve this answer























    • @Subharajyoti Majumde : I tried it and the output is : 2014-04-02 instead of the format i want : 02-04-2014
      – Nihad KH
      Apr 10 '14 at 9:21










    • Opps.... In that case - create new Formatter new SimpleDateFormat("dd-MM-yyyy"); and format the parsed date
      – Subhrajyoti Majumder
      Apr 10 '14 at 9:32










    • @Subharajyoti Majumde : I did this: <%String dateStr = request.getParameter("datedebut"); SimpleDateFormat formater2= new SimpleDateFormat("dd-MM-yyyy"); Date result = formater2.parse(dateStr); out.println(result);%> ,I choose the second of april 2014 and I got this output: Wed Oct 05 00:00:00 WET 7
      – Nihad KH
      Apr 10 '14 at 9:37










    • Answer updated...
      – Subhrajyoti Majumder
      Apr 10 '14 at 9:39










    • thank you so muuuuuuch you just save my life yeey wish u all the best
      – Nihad KH
      Apr 10 '14 at 9:48













    up vote
    5
    down vote










    up vote
    5
    down vote









    Try this -



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





    share|improve this answer














    Try this -



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






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Aug 23 '14 at 9:07

























    answered Apr 10 '14 at 9:04









    Subhrajyoti Majumder

    33.1k105790




    33.1k105790












    • @Subharajyoti Majumde : I tried it and the output is : 2014-04-02 instead of the format i want : 02-04-2014
      – Nihad KH
      Apr 10 '14 at 9:21










    • Opps.... In that case - create new Formatter new SimpleDateFormat("dd-MM-yyyy"); and format the parsed date
      – Subhrajyoti Majumder
      Apr 10 '14 at 9:32










    • @Subharajyoti Majumde : I did this: <%String dateStr = request.getParameter("datedebut"); SimpleDateFormat formater2= new SimpleDateFormat("dd-MM-yyyy"); Date result = formater2.parse(dateStr); out.println(result);%> ,I choose the second of april 2014 and I got this output: Wed Oct 05 00:00:00 WET 7
      – Nihad KH
      Apr 10 '14 at 9:37










    • Answer updated...
      – Subhrajyoti Majumder
      Apr 10 '14 at 9:39










    • thank you so muuuuuuch you just save my life yeey wish u all the best
      – Nihad KH
      Apr 10 '14 at 9:48


















    • @Subharajyoti Majumde : I tried it and the output is : 2014-04-02 instead of the format i want : 02-04-2014
      – Nihad KH
      Apr 10 '14 at 9:21










    • Opps.... In that case - create new Formatter new SimpleDateFormat("dd-MM-yyyy"); and format the parsed date
      – Subhrajyoti Majumder
      Apr 10 '14 at 9:32










    • @Subharajyoti Majumde : I did this: <%String dateStr = request.getParameter("datedebut"); SimpleDateFormat formater2= new SimpleDateFormat("dd-MM-yyyy"); Date result = formater2.parse(dateStr); out.println(result);%> ,I choose the second of april 2014 and I got this output: Wed Oct 05 00:00:00 WET 7
      – Nihad KH
      Apr 10 '14 at 9:37










    • Answer updated...
      – Subhrajyoti Majumder
      Apr 10 '14 at 9:39










    • thank you so muuuuuuch you just save my life yeey wish u all the best
      – Nihad KH
      Apr 10 '14 at 9:48
















    @Subharajyoti Majumde : I tried it and the output is : 2014-04-02 instead of the format i want : 02-04-2014
    – Nihad KH
    Apr 10 '14 at 9:21




    @Subharajyoti Majumde : I tried it and the output is : 2014-04-02 instead of the format i want : 02-04-2014
    – Nihad KH
    Apr 10 '14 at 9:21












    Opps.... In that case - create new Formatter new SimpleDateFormat("dd-MM-yyyy"); and format the parsed date
    – Subhrajyoti Majumder
    Apr 10 '14 at 9:32




    Opps.... In that case - create new Formatter new SimpleDateFormat("dd-MM-yyyy"); and format the parsed date
    – Subhrajyoti Majumder
    Apr 10 '14 at 9:32












    @Subharajyoti Majumde : I did this: <%String dateStr = request.getParameter("datedebut"); SimpleDateFormat formater2= new SimpleDateFormat("dd-MM-yyyy"); Date result = formater2.parse(dateStr); out.println(result);%> ,I choose the second of april 2014 and I got this output: Wed Oct 05 00:00:00 WET 7
    – Nihad KH
    Apr 10 '14 at 9:37




    @Subharajyoti Majumde : I did this: <%String dateStr = request.getParameter("datedebut"); SimpleDateFormat formater2= new SimpleDateFormat("dd-MM-yyyy"); Date result = formater2.parse(dateStr); out.println(result);%> ,I choose the second of april 2014 and I got this output: Wed Oct 05 00:00:00 WET 7
    – Nihad KH
    Apr 10 '14 at 9:37












    Answer updated...
    – Subhrajyoti Majumder
    Apr 10 '14 at 9:39




    Answer updated...
    – Subhrajyoti Majumder
    Apr 10 '14 at 9:39












    thank you so muuuuuuch you just save my life yeey wish u all the best
    – Nihad KH
    Apr 10 '14 at 9:48




    thank you so muuuuuuch you just save my life yeey wish u all the best
    – Nihad KH
    Apr 10 '14 at 9:48










    up vote
    0
    down vote













    You have converted the input from index.jsp into Date using formater.parse. Now to show it on the output in showdates.jsp, you have to again convert into String using formater.format(result) to yyyy-MM-dd. Actually what you are doing is correct. Whatever you get from input you should first parse and then print because you will be sure that the input which came fits into what you require. Use this.



    Date result = formater.parse(dateStr);
    SimpleDateFormat formaterForOut = new SimpleDateFormat("dd-MM-yyyy");
    String resultString = formaterForOut.format(result);
    out.println(resultString);





    share|improve this answer























    • I tried your code and I got the same output : Wed Apr 02 00:00:00 WET 2014
      – Nihad KH
      Apr 10 '14 at 9:27















    up vote
    0
    down vote













    You have converted the input from index.jsp into Date using formater.parse. Now to show it on the output in showdates.jsp, you have to again convert into String using formater.format(result) to yyyy-MM-dd. Actually what you are doing is correct. Whatever you get from input you should first parse and then print because you will be sure that the input which came fits into what you require. Use this.



    Date result = formater.parse(dateStr);
    SimpleDateFormat formaterForOut = new SimpleDateFormat("dd-MM-yyyy");
    String resultString = formaterForOut.format(result);
    out.println(resultString);





    share|improve this answer























    • I tried your code and I got the same output : Wed Apr 02 00:00:00 WET 2014
      – Nihad KH
      Apr 10 '14 at 9:27













    up vote
    0
    down vote










    up vote
    0
    down vote









    You have converted the input from index.jsp into Date using formater.parse. Now to show it on the output in showdates.jsp, you have to again convert into String using formater.format(result) to yyyy-MM-dd. Actually what you are doing is correct. Whatever you get from input you should first parse and then print because you will be sure that the input which came fits into what you require. Use this.



    Date result = formater.parse(dateStr);
    SimpleDateFormat formaterForOut = new SimpleDateFormat("dd-MM-yyyy");
    String resultString = formaterForOut.format(result);
    out.println(resultString);





    share|improve this answer














    You have converted the input from index.jsp into Date using formater.parse. Now to show it on the output in showdates.jsp, you have to again convert into String using formater.format(result) to yyyy-MM-dd. Actually what you are doing is correct. Whatever you get from input you should first parse and then print because you will be sure that the input which came fits into what you require. Use this.



    Date result = formater.parse(dateStr);
    SimpleDateFormat formaterForOut = new SimpleDateFormat("dd-MM-yyyy");
    String resultString = formaterForOut.format(result);
    out.println(resultString);






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Apr 10 '14 at 9:48

























    answered Apr 10 '14 at 9:20









    bgth

    368314




    368314












    • I tried your code and I got the same output : Wed Apr 02 00:00:00 WET 2014
      – Nihad KH
      Apr 10 '14 at 9:27


















    • I tried your code and I got the same output : Wed Apr 02 00:00:00 WET 2014
      – Nihad KH
      Apr 10 '14 at 9:27
















    I tried your code and I got the same output : Wed Apr 02 00:00:00 WET 2014
    – Nihad KH
    Apr 10 '14 at 9:27




    I tried your code and I got the same output : Wed Apr 02 00:00:00 WET 2014
    – Nihad KH
    Apr 10 '14 at 9:27










    up vote
    0
    down vote













    <% 

    String dateStr = request.getParameter("datedebut");

    java.text.SimpleDateFormat oldFormater = new SimpleDateFormat("dd-mm-yyyy");
    java.util.Date resultInDate = oldFormater.parse(dateStr);

    java.text.SimpleDateFormat newFormater = new SimpleDateFormat("dd-mm-yyyy");
    String resultInString = newFormater.format(resultInDate);

    out.println(resultInDate); // Wed Apr 02 00:00:00 WET 2014
    out.println(resultInString); // 02-04-2014

    /* convert String resultInString into java.sql.Date resultInDateSql */
    java.sql.Date resultInDateSql = java.sql.Date.valueOf(resultInString);

    /* set in query sql */
    preparedStatement.setDate(1, resultInDateSql);

    %>





    share|improve this answer



























      up vote
      0
      down vote













      <% 

      String dateStr = request.getParameter("datedebut");

      java.text.SimpleDateFormat oldFormater = new SimpleDateFormat("dd-mm-yyyy");
      java.util.Date resultInDate = oldFormater.parse(dateStr);

      java.text.SimpleDateFormat newFormater = new SimpleDateFormat("dd-mm-yyyy");
      String resultInString = newFormater.format(resultInDate);

      out.println(resultInDate); // Wed Apr 02 00:00:00 WET 2014
      out.println(resultInString); // 02-04-2014

      /* convert String resultInString into java.sql.Date resultInDateSql */
      java.sql.Date resultInDateSql = java.sql.Date.valueOf(resultInString);

      /* set in query sql */
      preparedStatement.setDate(1, resultInDateSql);

      %>





      share|improve this answer

























        up vote
        0
        down vote










        up vote
        0
        down vote









        <% 

        String dateStr = request.getParameter("datedebut");

        java.text.SimpleDateFormat oldFormater = new SimpleDateFormat("dd-mm-yyyy");
        java.util.Date resultInDate = oldFormater.parse(dateStr);

        java.text.SimpleDateFormat newFormater = new SimpleDateFormat("dd-mm-yyyy");
        String resultInString = newFormater.format(resultInDate);

        out.println(resultInDate); // Wed Apr 02 00:00:00 WET 2014
        out.println(resultInString); // 02-04-2014

        /* convert String resultInString into java.sql.Date resultInDateSql */
        java.sql.Date resultInDateSql = java.sql.Date.valueOf(resultInString);

        /* set in query sql */
        preparedStatement.setDate(1, resultInDateSql);

        %>





        share|improve this answer














        <% 

        String dateStr = request.getParameter("datedebut");

        java.text.SimpleDateFormat oldFormater = new SimpleDateFormat("dd-mm-yyyy");
        java.util.Date resultInDate = oldFormater.parse(dateStr);

        java.text.SimpleDateFormat newFormater = new SimpleDateFormat("dd-mm-yyyy");
        String resultInString = newFormater.format(resultInDate);

        out.println(resultInDate); // Wed Apr 02 00:00:00 WET 2014
        out.println(resultInString); // 02-04-2014

        /* convert String resultInString into java.sql.Date resultInDateSql */
        java.sql.Date resultInDateSql = java.sql.Date.valueOf(resultInString);

        /* set in query sql */
        preparedStatement.setDate(1, resultInDateSql);

        %>






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited 18 hours ago

























        answered 20 hours ago









        antelove

        62737




        62737






























             

            draft saved


            draft discarded



















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f22983392%2fget-the-dd-mm-yyyy-format-of-date-in-java-jsp%23new-answer', 'question_page');
            }
            );

            Post as a guest




















































































            Popular posts from this blog

            Xamarin.iOS Cant Deploy on Iphone

            Glorious Revolution

            Dulmage-Mendelsohn matrix decomposition in Python