Retreiving Date/Time from a Date field in Oracle database

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • renisha
    New Member
    • Sep 2007
    • 5

    #1

    Retreiving Date/Time from a Date field in Oracle database

    I am trying to retreive a string in this format dd-MM-yyyy hh:mm:ss a from Oracle database from the column of type Date which has value 10/09/2007 07:11:16

    Date strfinaldate = new java.util.Date( rs.getTimestamp (10).getDate()+ rs.getTimestamp (10).getTime()) ;

    SimpleDateForma t sf = new SimpleDateForma t ("dd-MM-yyyy hh:mm:ss a");
    String formatteddate = sf.format(strfi naldate );

    But I am getting the o/p as 10-09-2007 12:00:00 am

    Please tell me how do I get the exact date/time

    Thanks
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by renisha
    I am trying to retreive a string in this format dd-MM-yyyy hh:mm:ss a from Oracle database from the column of type Date which has value 10/09/2007 07:11:16

    Date strfinaldate = new java.util.Date( rs.getTimestamp (10).getDate()+ rs.getTimestamp (10).getTime()) ;

    SimpleDateForma t sf = new SimpleDateForma t ("dd-MM-yyyy hh:mm:ss a");
    String formatteddate = sf.format(strfi naldate );

    But I am getting the o/p as 10-09-2007 12:00:00 am

    Please tell me how do I get the exact date/time

    Thanks
    You can pass a Timestamp to the format method of the SimpleDateForma t class. Try that and see if you get the correct thing.

    Comment

    • renisha
      New Member
      • Sep 2007
      • 5

      #3
      I tried doing with passing the timestamp object but still getting the same o/p.
      Also I tried to print this value :

      String cal = ""+tsdate.getHo urs()+tsdate.ge tMinutes()+tsda te.getSeconds() ;
      where tsdate is the timestamp object.

      Its showing as 000

      Can anyone tell me why I getting this output though in databse it is showing as

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        Originally posted by renisha
        I tried doing with passing the timestamp object but still getting the same o/p.
        Also I tried to print this value :

        String cal = ""+tsdate.getHo urs()+tsdate.ge tMinutes()+tsda te.getSeconds() ;
        where tsdate is the timestamp object.

        Its showing as 000

        Can anyone tell me why I getting this output though in databse it is showing as
        If the column data type is Date, then you should be using rs.getDate to extract the date.

        Comment

        • renisha
          New Member
          • Sep 2007
          • 5

          #5
          Originally posted by r035198x
          If the column data type is Date, then you should be using rs.getDate to extract the date.
          I have even tried this way -
          Date strfinaldate =rs.getDate(10) ;

          SimpleDateForma t sf = new SimpleDateForma t ("dd-MM-yyyy hh:mm:ss a");
          String formatteddate = sf.format(strfi naldate );


          But still getting the same o/p as before

          Comment

          • r035198x
            MVP
            • Sep 2006
            • 13225

            #6
            Originally posted by renisha
            I have even tried this way -
            Date strfinaldate =rs.getDate(10) ;

            SimpleDateForma t sf = new SimpleDateForma t ("dd-MM-yyyy hh:mm:ss a");
            String formatteddate = sf.format(strfi naldate );


            But still getting the same o/p as before
            Did you try to retrieve the date using overloaded getDate method that takes a Calendar as well?
            Perhaps your driver is returning the wrong date then

            Comment

            • r035198x
              MVP
              • Sep 2006
              • 13225

              #7
              Just thought about it a bit more.
              You have an Oracle Date column that's storing both date and time.
              You want to retrieve both the date and time fields.

              So in your sql that retrieves the date, cast the Date to Timestamp using Oracle's TO_TIMESTAMP function then use rs.getTimeStamp to get both the date and time values. Tell us how that goes.

              Comment

              • renisha
                New Member
                • Sep 2007
                • 5

                #8
                Originally posted by r035198x
                Just thought about it a bit more.
                You have an Oracle Date column that's storing both date and time.
                You want to retrieve both the date and time fields.

                So in your sql that retrieves the date, cast the Date to Timestamp using Oracle's TO_TIMESTAMP function then use rs.getTimeStamp to get both the date and time values. Tell us how that goes.

                Thanks a lot !!! I tried with oracle's to_char function in the query .
                I could then get the exact value .

                Comment

                • r035198x
                  MVP
                  • Sep 2006
                  • 13225

                  #9
                  Originally posted by renisha
                  Thanks a lot !!! I tried with oracle's to_char function in the query .
                  I could then get the exact value .
                  So the TO_TIMESTAMP function didn't work but the TO_CHAR function worked?

                  Comment

                  • renisha
                    New Member
                    • Sep 2007
                    • 5

                    #10
                    Originally posted by r035198x
                    So the TO_TIMESTAMP function didn't work but the TO_CHAR function worked?
                    I didn't try with TO_TIMESTAMP .

                    Comment

                    Working...