permaculture playing cards
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

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Dates help" Watch "Dates help" New topic

Dates help

Sarah Gaikwad
Ranch Hand

Joined: Feb 26, 2004
Posts: 35
i have a date in the format "24/12/2004 19:10:11". This date has to be put into the database. the seems to be taking only the part "24/12/2004" and not he time part.

Plz help
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
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
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
Avi Abrami
Ranch Hand

Joined: Oct 11, 2000
Posts: 1141


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,
    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(Calendar.HOUR ,Integer.parseInt(cpdt.substring(cpdt.indexOf(":")-2,cpdt.indexOf(":")).trim()));
    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:
    subject: Dates help
    It's not a secret anymore!