-2

I want to convert user entered date 31-12-2012 into PostgreSQL date format. i am appending date 'day-month-year' (31-12-2012)

Vhasure Prashant
  • 188
  • 5
  • 15

2 Answers2

2

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 :(

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
1

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.

shriguru nayak
  • 310
  • 1
  • 3
  • 21