Issue
In this project, I would like to get a date from data that user just input using form using JSP. After that, I would like to store the input to MySQL.
This is my JSP file:
<form:form id="regForm" modelAttribute="assignment" action="successAddAssignment" method="post">
<table align="center">
<tr>
<td>
<form:label path="date">Date</form:label>
</td>
<td>
<form:input path="date" name="date" type="date" id="date" pattern="yyyy/MM/dd"/> <span class="inst">(YYYY-MM-DD)</span>
</td>
</tr>
<tr>
<td></td>
<td>
<form:button id="addAssignment" name="addAssignment">Submit</form:button>
</td>
</tr>
<tr></tr>
</table>
</form:form>
This is my Dao file :
public void addAssignment(Assignment assignment){
String sql = "insert into assignment values(?,?,?,?,?,?)";
jdbcTemplate.update(sql, assignment.getId(), assignment.getDate(), assignment.getTime(), assignment.getCode_module(), assignment.getName_module(), assignment.getDescription());
}
But, when I run my program, the result is like this: 1
I got an error when I want to submit the data "date". My question is How do I create the date format from "yyyy/MM/dd" to "dd/MM/yyyy"?
Solution
There is no pattern tag in JSP. You probably need to convert a string into a Date object with JS or in the controller.
Something like this may work in the controller:
String dateReceived = "2018-04-24";
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
Date dateFormatted = dateFormat.parse(dateReceived);
You may want to check SimpleDateFormat
Answered By - Natalia Gutiérrez
Answer Checked By - Dawn Plyler (JavaFixing Volunteer)