0

I need to load data from Postgres to Oracle. While reading data from Postgres I got the date in below format "2013-02-13 00:30:22.402" now I need to load this date into oracle db I tried but getting exception

String dd = "2013-02-13 00:30:22.402";
DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
java.sql.Date dateform=(Date) formatter.parse(dd);
System.out.println(dateform);

Throws this exception:

Exception in thread "main" java.lang.ClassCastException: java.util.Date cannot be cast to java.sql.Date
    at ghg.main(ghg.java:26)
Roman C
  • 49,761
  • 33
  • 66
  • 176
Navyah
  • 1,660
  • 10
  • 33
  • 58
  • [how-to-convert-java-util-date-to-java-sql-date](http://stackoverflow.com/questions/530012/how-to-convert-java-util-date-to-java-sql-date) – Suresh Atta Jan 27 '14 at 13:14
  • Why do you use Strings at all? If you do a `getTimestamp` on one side and then a `setTimestamp()` on the other side, you don't need any conversion at all –  Jan 27 '14 at 13:23
  • 1
    I want to store the data in exact format along with time and seconds – Navyah Jan 27 '14 at 13:34

3 Answers3

0

parse returns a java.util.Date, so you need to construct a new sql Date object using the constructor new Date(long l) :

java.sql.Date dateform= new java.sql.Date(formatter.parse(dd).getTime());
user2336315
  • 15,697
  • 10
  • 46
  • 64
0

Do like this

java.util.Date date = formatter.parse(dd);
java.sql.Date sqlDate = new java.sql.Date(date.getTime());  
Prabhakaran Ramaswamy
  • 25,706
  • 10
  • 57
  • 64
0

by default parse function return a Date object. so you can not cast return value.

Date parse(String source) Parses text from the beginning of the given string to produce a date.

             String dd = "2013-02-13 00:30:22.402";
             DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
             java.sql.Date dateform=formatter.parse(dd);
java seeker
  • 1,246
  • 10
  • 13