This week's book giveaway is in the Java in General forum.
We're giving away four copies of Think Java: How to Think Like a Computer Scientist and have Allen B. Downey & Chris Mayfield on-line!
See this thread for details.
Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Dates help

 
Sarah Gaikwad
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
i have a date in the format "24/12/2004 19:10:11". This date has to be put into the database. the java.sql.date seems to be taking only the part "24/12/2004" and not he time part.

Plz help
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
store the milliseconds into the db (don*t use Date but Long e.g.) and
when you retrieve your objects create new DateObjects based on the milliseconds.
Maybe this solves your prob
 
Blake Minghelli
Ranch Hand
Posts: 331
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1. Is the field in the database table declared as a "datetime" or just "date"?
2. Can you show the code you are using to construct the java.sql.Date object?
 
Wayne L Johnson
Ranch Hand
Posts: 399
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you want to use a "java.sql.Timestamp" instance, which will preserve both the date and time portion. Most databases will ignore the time portion of a "java.sql.Date" instance.
 
Sarah Gaikwad
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I have a stored procedure that inserts values into the table. A callable statement is used to put these values into the table.
payment_dt datetime.//in the db
//in java
java.sql.Date p_dt
cstmt.setDate(10,p_dt);
setDate always takes the 2nd parameter as an objectof type java.sql.Date.
Can u plz tell me if java.sql.Timestamp will help and how i can use it
Thanks
Sharun
 
Avi Abrami
Ranch Hand
Posts: 1141
1
Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sharun,

java.sql.Timestamp ... how i can use it

You can use the "setTimestamp()" method in the CallableStatement interface. However, I get the impression that you are just hoping that this will solve your problem. Perhaps if you gave some more details about your problem, you may get a better answer. I suggest starting with the following details:
  • DBMS you are using
  • stored procedure code
  • java code used to invoke the stored procedure
  • complete error message and stack trace you are getting (if any)


  • Good Luck,
    Avi.
     
    Sarah Gaikwad
    Ranch Hand
    Posts: 35
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I managed to create a Timestamp object, the code of which is below.
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss ");
    Calendar calendar =Calendar.getInstance();
    calendar.set(Integer.parseInt((cpdt.substring(cpdt.lastIndexOf("/")+1,cpdt.indexOf(":")-2)).trim()),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)));
    calendar.set(Calendar.AM_PM,1);
    class_variable_pdt = Timestamp.valueOf(sdf.format(calendar.getTime()));

    I am able to add the same to the db. The problem i am now facing is that if i store the date as 05/05/2002 19:33:50 ,it stores the same as 2004-05-05 7:33:50 am,this is incorrect.
    if i set the simpledateformat object to "yyyy-MM-dd hh:mm:ss a", the timestamp gives an error.
    plz help
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic