문제

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.

도움이 되었습니까?

해결책

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top