1 2 Previous Next 18 Replies Latest reply: Feb 10, 2010 1:36 PM by 625622 RSS

    String to Timestamp for Oracle

    625622
      Hi all Can someone please tell me how to convert a String time into Timestamp I'm inserting it into Oracle database. I'm fetching it from another page like so;
           
      String p_start_time = req.getParameter("START_TIME");        
      String p_end_time = req.getParameter("END_TIME"); 
      and now I need to convert it to Timestamp I've tried quite a few examples from net but nothing seem to work.

      Need some help please
      Thanks
      Zed
        • 1. Re: String to Timestamp for Oracle
          843789
          What examples did you try and how didn't they work? What's the format of p_state_time?

          You can convert a String to a Date with SimpleDateFormat.parse, then construct a Timestamp from Date.getTime.
          • 2. Re: String to Timestamp for Oracle
            625622
            Thanks for your reply, I've managed to insert into the Database but it's NOT the date and times of these strings
            String p_event_date = req.getParameter("EVENT_DATE");
            String p_start_time = req.getParameter("START_TIME");   
            String p_end_time = req.getParameter("END_TIME");
            what I'm getting is Todays date and Current time. So basiclly it's not getting converted.
            Any thoughts please ...
            Thanks
            Zed
            /*****************************************  To format the Event date  ***********************/
                Format formatter;
                Date date = new Date();
            
                formatter = new SimpleDateFormat("dd-MMM-yy");
                p_event_date = formatter.format(date);
                System.out.println(p_event_date);
                out.println(p_event_date);
                
                /****************************************  To format the Time *********************************/
              
                java.sql.Timestamp stime = new java.sql.Timestamp(new java.util.Date().getTime());
                p_start_time = new SimpleDateFormat("dd-MMM-yy , h:mm:ss").format(stime);
                System.out.println(p_start_time);
                out.println(p_start_time);
                
                java.sql.Timestamp etime = new java.sql.Timestamp(new java.util.Date().getTime());
                p_end_time = new SimpleDateFormat("dd-MMM-yy , h:mm:ss").format(etime);
                System.out.println(p_end_time);
                out.println(p_end_time);
            • 3. Re: String to Timestamp for Oracle
              625622
              Ok I have sorted out the p_event_date , instead of using the above if I use the code below it works fine, converts to the desire date ;
              SimpleDateFormat formatter = new SimpleDateFormat("dd-MMM-yy");
                  java.sql.Date date = null;
                  try {
                        date = (java.sql.Date) new java.sql.Date(formatter.parse(p_event_date).getTime());
                        out.println("Event date After Format" + date + "<br>");
                   } catch (ParseException e1) {
                        e1.printStackTrace();
                   }
              But the p_start_time and p_end_time still problematic. These two string are NOT converting to Timestamp . I need some help please ...
              Zed
              • 4. Re: String to Timestamp for Oracle
                843789
                Can you try something similar to the p_event_date code? The first example you posted does formatting instead of parsing, and you don't pass anything to the constructor of Date so it would be the current time. Do you want a java.sql.Timestamp object or a formatted String as the result?

                What are you trying for p_start_time and p_end_time? What output does that give?

                Edited by: fgb on 06-Feb-2010 21:44
                • 5. Re: String to Timestamp for Oracle
                  625622
                  Thanks for your reply, this is what I'm trying for the Times;
                  Timestamp stime = null;
                  DateFormat df = DateFormat.getDateInstance();
                  SimpleDateFormat sdf = new SimpleDateFormat("hh:mm:ss,dd-MMM-yy"); 
                  try
                  { 
                  stime = new Timestamp (sdf.parse(p_start_time).getTime()); 
                  System.out.println(stime);
                   }catch(Exception ex){ ex.printStackTrace(); } 
                  
                  Timestamp etime = null; 
                  DateFormat df2 = DateFormat.getDateInstance(); 
                  SimpleDateFormat sdf2 = new SimpleDateFormat("hh:mm:ss,dd-MMM-yy"); 
                  try
                  { 
                  etime = new Timestamp (sdf2.parse(p_end_time).getTime()); 
                  System.out.println(etime); }catch(Exception ex){ ex.printStackTrace(); }
                  And this is the error its greeting me with
                  java.text.ParseException: Unparseable date: "12:15" at java.text.DateFormat.parse(Unknown Source) ... 
                  java.text.ParseException: Unparseable date: "12:30" at java.text.DateFormat.parse(Unknown Source) ... 
                  I think the problem is my Time strings they are two JavaScript dropdown boxs populated like this e.g. 01:00, 01:15 ... I'm not quite sure thou. The Date is also from a JS calendar but the format of it is e.g. 07-Feb-10. In Oracle database times columns it appears as "null".
                  ps. I have tried this way still no joy SimpleDateFormat("dd-MMM-yy, hh:mm:ss");
                  Anymore thoughts Please
                  Zed

                  Edited by: Zed420 on Feb 7, 2010 5:54 AM
                  • 6. Re: String to Timestamp for Oracle
                    843789
                    I think the problem is my Time strings they are two JavaScript dropdown boxs populated like this e.g. 01:00, 01:15
                    If the strings are in this format, then the SimpleDateFormat has to match that:
                    new SimpleDateFormat("hh:mm");
                    If they are times without a date part, then stime and etime would be an instance of Time instead of Timestamp.
                    • 7. Re: String to Timestamp for Oracle
                      625622
                      If they are times without a date part, then stime and etime would be an instance of Time instead of Timestamp. 
                      Thanks fgb for your reply, I'm not quite sure what you mean that? Can you just expand on little bit please.
                      Thanks
                      Zed
                      • 8. Re: String to Timestamp for Oracle
                        843789
                        Check the properties of these:
                        [http://java.sun.com/javase/6/docs/api/java/sql/Timestamp.html]
                        [http://java.sun.com/javase/6/docs/api/java/sql/Time.html]

                        Timestamp has a year, month, date, but the input "01:00" doesn't. You can swap Timestamp with Time if this is right.
                        • 9. Re: String to Timestamp for Oracle
                          625622
                          fgb Thanks a million that was a great help. My final working code;
                          java.sql.Time sTime = null;
                               SimpleDateFormat formatter1 = new SimpleDateFormat ("h:mm"); 
                               try {
                                    sTime = new java.sql.Time(formatter1.parse( p_start_time ).getTime());
                               } catch (ParseException e1) {
                                    // TODO Auto-generated catch block
                                    e1.printStackTrace();
                               } 
                          
                               java.sql.Time eTime = null;
                               SimpleDateFormat formatter2 = new SimpleDateFormat ("h:mm"); 
                               try {
                                    eTime = new java.sql.Time(formatter2.parse( p_end_time ).getTime());
                               } catch (ParseException e1) {
                                    // TODO Auto-generated catch block
                                    e1.printStackTrace();
                               } 
                          Zed
                          • 10. Re: String to Timestamp for Oracle
                            jschellSomeoneStoleMyAlias
                            Zed420 wrote:
                            Hi all Can someone please tell me how to convert a String time into Timestamp I'm inserting it into Oracle database.
                            1. There is a JDBC forum.
                            2. You should be using a prepared statement. Except for special circumstances all of the above is wrong.
                            • 11. Re: String to Timestamp for Oracle
                              625622
                              Thanks jschell you've been a great help.

                              Zed
                              • 12. Re: String to Timestamp for Oracle
                                843789
                                You should be using a prepared statement. Except for special circumstances all of the above is wrong.
                                How are these related? He would still need to parse the http parameters.
                                • 13. Re: String to Timestamp for Oracle
                                  jschellSomeoneStoleMyAlias
                                  fgb wrote:
                                  You should be using a prepared statement. Except for special circumstances all of the above is wrong.
                                  How are these related? He would still need to parse the http parameters.
                                  You are correct. On re-reading except for the first post none of the discussion has anything to do with databases unlike the what the subject or first post says.
                                  • 14. Re: String to Timestamp for Oracle
                                    625622
                                    I'm slightly puzzled by last two posts,

                                    1. p_event_date have been converted to Date format
                                    2. p_start_time and p_end_time have been converted to Time format
                                    and all three insert into Oracle database by store procedure as date, sTime and eTime.

                                    I'm sure there must have been a better way of doing it but this worked for me eventually after battling with it for 5 very long days. If some can fined a better way please share it with me as I'm only a learner.

                                    Regards
                                    Zed
                                    1 2 Previous Next