-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

Cillín
  • 187
  • 1
  • 13
  • 2
    Your format uses front slashes (`/`), but your date has dashes (`-`). – sstan Dec 12 '15 at 22:40
  • Are you getting a classcastexception? – Yassin Hajaj Dec 12 '15 at 22:48
  • The first element of the Question is also a duplicate of [this](http://stackoverflow.com/q/23569950/642706) and many [many others](http://stackoverflow.com/search?q=unparseable+java). Please search StackOverflow.com before posting. – Basil Bourque Dec 13 '15 at 04:38
  • FYI, the terribly troublesome old date-time classes such as [`java.util.Date`](https://docs.oracle.com/javase/10/docs/api/java/util/Date.html), `java.sql.Date`, and `java.text.SimpleDateFormat` are now [legacy](https://en.wikipedia.org/wiki/Legacy_system), supplanted by the [*java.time*](https://docs.oracle.com/javase/10/docs/api/java/time/package-summary.html) classes built into Java 8 and later. See [*Tutorial* by Oracle](https://docs.oracle.com/javase/tutorial/datetime/TOC.html). – Basil Bourque Dec 31 '18 at 02:33

3 Answers3

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());
Community
  • 1
  • 1
Yassin Hajaj
  • 21,337
  • 9
  • 51
  • 89
  • You sir.. are a genius! Thank you! – Cillín Dec 12 '15 at 22:58
  • @Cillin Which I was:p You're welcome :) – Yassin Hajaj Dec 12 '15 at 23:00
  • and I don't feel this question should be marked down. It wasn't really an obvious answer for a noob.. – Cillín Dec 12 '15 at 23:20
  • @Cillin Indeed it was not, but that happens, nothing to worry about. – Yassin Hajaj Dec 12 '15 at 23:21
  • @Cillín No, not an obvious answer. But both elements of the question have been covered *many* times already on StackOverflow. – Basil Bourque Dec 13 '15 at 04:32
  • FYI, the terribly troublesome old date-time classes such as [`java.util.Date`](https://docs.oracle.com/javase/10/docs/api/java/util/Date.html), `java.sql.Date`, and `java.text.SimpleDateFormat` are now [legacy](https://en.wikipedia.org/wiki/Legacy_system), supplanted by the [*java.time*](https://docs.oracle.com/javase/10/docs/api/java/time/package-summary.html) classes built into Java 8 and later. See [*Tutorial* by Oracle](https://docs.oracle.com/javase/tutorial/datetime/TOC.html). – Basil Bourque Dec 31 '18 at 02:33
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);
Heri
  • 4,368
  • 1
  • 31
  • 51
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"));
    }
}
Josh Chappelle
  • 1,558
  • 2
  • 15
  • 37