• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

util.Date to sql.Date

 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 128
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 331
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
is SimpleDateFormat is a method in which we have to do it ourselves?
or just need to import it??
 
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Import it! It's in the java.text package.
 
reply
    Bookmark Topic Watch Topic
  • New Topic