SUSH SUSH - 3 months ago 16
MySQL Question

Inserting date field into database using jsp with different date formats

I am trying to save the date field into mysql table.
The date field in my jsp form is in format MM/DD/YYY and in database as YYYY/MM/DD.
How can I retrieve the date from form and insert into the table with YYYY/MM/DD format ?

Answer
String publishdt = request.getParameter("publish");
if(publishdt == null)
{   
    publishdt = "";
}
else
{
    try 
        {
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MM/dd/yyyy");
            LocalDate date = LocalDate.parse(publishdt, formatter);
            publish = String.format("%s", date);
        }
    catch (DateTimeParseException exc) 
        {
            out.println("%s is not parsable!%n"+publish);
            throw exc;      // Rethrow the exception.
        }
        // 'date' has been successfully parsed
}   
Comments