I want to convert user entered date 31-12-2012 into PostgreSQL date format. i am appending date 'day-month-year' (31-12-2012)
2 Answers
Ignore the database part of it - you shouldn't be providing it to the database as a string anyway.
Instead, use java.text.SimpleDateFormat with the right pattern ("dd-MM-yyyy") to parse it into a Date (making sure you use the right time zone, which will be important later). Then to use this in a SQL statement, use a PreparedStatement and call setDate. You'll need to create a java.sql.Date from the java.util.Date that you get out of SimpleDateFormat.parse though.
The time zone part is slightly messy - the documentation for java.sql.Date suggests that the JVM's default time zone is used to interpret a "millis since the epoch" value as a date. That would suggest you should use the default time zone when parsing, too - but it's far from elegant :(
- 1,421,763
 - 867
 - 9,128
 - 9,194
 
- 
                    I was taking look to other of your answers... http://stackoverflow.com/a/7662674/980472 I think it can be helpful here – jddsantaella Oct 24 '12 at 07:52
 - 
                    @jddsantaella: I don't think so, to be honest. – Jon Skeet Oct 24 '12 at 07:52
 
This is pretty simple. I think you dint even try to google. Anyway, here is a sample code.
String User_date = "31-12-2012";
java.sql.Date sqlDate = java.sql.Date.valueOf(User_date);
This shall solve your problem.
- 310
 - 1
 - 3
 - 21