-1

I am trying to send a date value from my java program into an oracle sql database. But I keep getting the error: java.text.parseexception: unparseable date.

I set the date format as:

SimpleDateFormat df = new SimpleDateFormat("yyyy/MM/dd", Locale.ENGLISH);
        java.sql.Date date = (java.sql.Date)df.parse(dob_text.getText());

I have set my database with the same date format. And try to send the date through a prepared statement like so:

ps.setDate(3, date); 

I am entering a date 1994-09-09. That's the correct date format for the one I declared right? Is there something wrong with my java formation code? Has anyone else had this problem? Any help would be much appreciated

4
  • 2
    Your format uses front slashes (/), but your date has dashes (-). Commented Dec 12, 2015 at 22:40
  • Are you getting a classcastexception? Commented Dec 12, 2015 at 22:48
  • The first element of the Question is also a duplicate of this and many many others. Please search StackOverflow.com before posting. Commented Dec 13, 2015 at 4:38
  • FYI, the terribly troublesome old date-time classes such as java.util.Date, java.sql.Date, and java.text.SimpleDateFormat are now legacy, supplanted by the java.time classes built into Java 8 and later. See Tutorial by Oracle. Commented Dec 31, 2018 at 2:33

3 Answers 3

1

This should work, I corrected 2 errors :

  • First of all, the format should have been yyyy-MM-dd since that's the format of your input.

  • Then, you can not implicitely cast java.util.Date to java.sql.Date, you need to use the java.sql.Date constructor and java.util.Date#getTime(). See here


Solution

SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH);
java.sql.Date SQLDate = new java.sql.Date(df.parse(dob_text.getText()).getTime());
Sign up to request clarification or add additional context in comments.

6 Comments

You sir.. are a genius! Thank you!
@Cillin Which I was:p You're welcome :)
and I don't feel this question should be marked down. It wasn't really an obvious answer for a noob..
@Cillin Indeed it was not, but that happens, nothing to worry about.
@Cillín No, not an obvious answer. But both elements of the question have been covered many times already on StackOverflow.
|
1

Your first try probably threw the exception you mentioned because of the wrong format as Josh pointed out. After correcting this the next problem occurs: A java.sql.Date is NOT a java.util.Date. So you cannot just typecast the outcome of the df.parse, which is a java.util.Date. And third: If you provide the pattern to the SimpleDateFormat you can omit the locale. Following code runs without errors:

    String input = "1994-09-09";
    SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
    Date date = df.parse(input);
    System.out.println( date);
    java.sql.Date sqlDate = new java.sql.Date( date.getTime() );
    System.out.println( sqlDate);

Comments

0

Change your format to yyyy-MM-dd.

I just wrote this program and it works fine. Make sure you aren't getting some other error now.

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Locale;

public class DateFormatDemo
{
    public static void main(String[] args) throws ParseException
    {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH);
        System.out.println(df.parse("1994-09-09"));
    }
}

7 Comments

Didn't do the trick..
Good deal. Please accept the answer. Just hitting the up arrow doesn't accept it as being the correct answer.
I can't accept it if it didn't work. I got the error java.lang.classcastexception java.util.date cannot be cast to java.sql.date
@JoshChappelle It did NOT do the trick :)
@Cillín Then use new java.sql.Date(df.parse(...).getTime());
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.