Reputation: 119
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?
Upvotes: 2
Views: 32859
Reputation: 3348
<%
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);
%>
Upvotes: 0
Reputation: 41200
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));
%>
Upvotes: 5
Reputation: 630
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));
Upvotes: 2
Reputation: 460
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);
Upvotes: 0