• Post Reply Bookmark Topic Watch Topic
  • New Topic

util.Date to sql.Date  RSS feed

 
Sarah Gaikwad
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I have a String in the format of "25/12/2004 19:11:19".
this string needs to be converted to an sql.Date for the purpose of entering into the database through a Callable Statement. Below i am pasting the code,this does not work. I use a util date and then convert it to a sql date .I get an Illegalargument exception..the code is
Calendar calendar =Calendar.getInstance();
calendar.set(Integer.parseInt(cpdt.substring(cpdt.lastIndexOf("/")+1,cpdt.indexOf(":")-2)),Integer.parseInt(cpdt.substring(cpdt.indexOf("/")+1,cpdt.lastIndexOf("/")))-1,Integer.parseInt(cpdt.substring(0,cpdt.indexOf("/"))));
calendar.set(Calendar.HOUR ,Integer.parseInt(cpdt.substring(cpdt.indexOf(":")-2,cpdt.indexOf(":")).trim()));
calendar.set(Calendar.MINUTE,Integer.parseInt(cpdt.substring(cpdt.lastIndexOf(":")-2,cpdt.lastIndexOf(":"))));
calendar.set(Calendar.SECOND,Integer.parseInt(cpdt.substring(cpdt.lastIndexOf(":")+1)));
java.util.Date d = calendar.getTime();

cust_payment_dte =java.sql.Date.valueOf(d.toString());
//this is where the error is thrown
plz help
sharun
 
Raj Chila
Ranch Hand
Posts: 128
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Sharun,
first of all, there is a SimpleDateFormat class which takes a String as a Default format, and then you can pass the String to this Object through the parse(...) method. you can avoid that String parsing all on your own.
and as util.Date is a super class of sql.date there should not be any problem when you set the Util.Date as the parameter.
 
Blake Minghelli
Ranch Hand
Posts: 331
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is an example of a pretty easy way of converting "25/12/2004 19:11:19" into a java.sql.Date object:

I didn't actually try compiling that and the SimpleDateFormat.parse() method throws a ParseException so you need to enclose it in a try/catch block or delare the method as throwing it, etc.
 
syidah mk
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
is SimpleDateFormat is a method in which we have to do it ourselves?
or just need to import it??
 
James Carman
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Import it! It's in the java.text package.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!