Pergunta

I am storing date in my DB, before I am taking date in MM/DD/YYYY format, I am taking data from user using textbox. But now I want time as well so I am taking date from user and time automatically attaching with user inputted date.

I am getting date + time like :

05/07/2014 8:55:3.522

I have used following code to parse this date and store it into DB, I want to store date in following format :

2014-05-07 8:55:3.522 // here format is YYYY-MM-DD and time

Code :

SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss.SSS");
Date parsedDate = dateFormat.parse(getCheckOutDate());
Timestamp timestamp = new Timestamp(parsedDate.getTime());

I am confused that how to parse received date and change it's format and save into DB.

When I try above code it doesn't parse date.

Any guidance on it. Thank You.

Foi útil?

Solução

parseDate takes the date to be parsed as a string in the format specified. Your format is for the destination, which doesn't matter, as JDBC will take care of that for you.. What you need is something like (untested):

SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy k:mm:ss.SSSS");
Date parsedDate = dateFormat.parse(getCheckOutDate());
Timestamp timestamp = new Timestamp(parsedDate.getTime());

I do hope that helps.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top