1

I have a String which contains date string eg 12-28-18 of format MM-dd-yy. I wish to pass this parameter to a stored procedure in Oracle database.

Hence, I am trying to convert this String to java.sql.Date. Following is the method I am following to convert:

DateFormat df = new SimpleDateFormat("MM-dd-yy");
java.util.Date date1 = df.parse(valueobject.getDate());
java.sql.Date date2 = new java.sql.Date(date1.getTime())

However , it leads to ParseException. Not able to figure out why it fails during parsing. Any help on this would be great!

6
  • 3
    new SimpleDateFormat("MM-dd-yy", Locale.English);? Commented Jul 15, 2013 at 5:42
  • stackoverflow.com/questions/2980583/… Commented Jul 15, 2013 at 5:42
  • @johnchen902 Adding the locale does not work! Commented Jul 15, 2013 at 5:50
  • Tried this: new SimpleDateFormat("MM-dd-yy", Locale.ENGLISH).parse("12-28-18"); but no exception thrown. Commented Jul 15, 2013 at 5:54
  • 2
    Is valueobject.getDate() a String? Just to double check. I've seen someone accidentally try to parse a date into a date. Commented Jul 15, 2013 at 5:56

3 Answers 3

6

Using valueOf():

String date = "2000-11-01"; // YYYY-MM-DD
java.sql.Date sqlDate = java.sql.Date.valueOf(date);
Sign up to request clarification or add additional context in comments.

Comments

0

Try this

    DateFormat df = new SimpleDateFormat("EEE MM-dd-yy hh:mm:ss");
    Date d=new Date();
    String date1 = df.format(d);
    java.sql.Date date2 = new java.sql.Date(d.getTime());
    System.out.println(date1);
    System.out.println(df.format(date2));

Or

    DateFormat df = new SimpleDateFormat("MM-dd-yy");
    Date d=new Date();
    String date1 = df.format(d);
    java.sql.Date date2 = new java.sql.Date(d.getTime());
    System.out.println(date1);
    System.out.println(df.format(date2));

Comments

0

This work for me, maybe yourvalueobject.getDate() doesn't return a String. Please check!!

SimpleDateFormat format = new SimpleDateFormat("MM-dd-yyyy");
    Date parsed = format.parse("07-15-2013");
    java.sql.Date sql = new java.sql.Date(parsed.getTime());

Similar with your code and work very well!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.