Wednesday, 10 August 2016

java - Converting String into SQl Date format




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)


Answer



Do like this



java.util.Date date = formatter.parse(dd);
java.sql.Date sqlDate = new java.sql.Date(date.getTime());

No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...