aspose file tools*
The moose likes Java in General and the fly likes Dates help Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Dates help" Watch "Dates help" New topic
Author

Dates help

Sarah Gaikwad
Ranch Hand

Joined: Feb 26, 2004
Posts: 35
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

Joined: Nov 22, 2008
Posts: 18944
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

Joined: Sep 13, 2002
Posts: 331
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?


Blake Minghelli<br />SCWCD<br /> <br />"I'd put a quote here but I'm a non-conformist"
Wayne L Johnson
Ranch Hand

Joined: Sep 03, 2003
Posts: 399
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

Joined: Feb 26, 2004
Posts: 35
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

Joined: Oct 11, 2000
Posts: 1124

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

    Joined: Feb 26, 2004
    Posts: 35
    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
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Dates help