7 Replies Latest reply on Nov 14, 2012 12:52 PM by Sushant Sharma

    Difference between two time values - URGENT

    rb4777
      Hi, I have a requirement to cal the time difference and save to database.

      String startTime = 14-11-2012 09:47:32;
      String endTime = 14-11-2012 09:47:36;

      I need to get endTime - startTime.

      how to cal this difference.

      Thanks
        • 1. Re: Difference between two time values - URGENT
          Sushant Sharma
          Hi,

          Can you try this:

          String startTime = "14-11-2012 09:47:32";
          String endTime = "14-11-2012 09:47:36";

          SimpleDateFormat format = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");

          Date d1 = null;
          Date d2 = null;

          try {
          d1 = format.parse(startTime);
          d2 = format.parse(endTime);
          } catch (ParseException e) {
          e.printStackTrace();
          }

          long diff = d2.getTime() - d1.getTime();

          --Sushant                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           
          • 2. Re: Difference between two time values - URGENT
            rb4777
            Hi Sushant,

            I have tried your code. The sop is of two dates is coming as 4000.
            I want in seconds. how to convert to seconds.

            Thanks

            Edited by: rbojja on Nov 14, 2012 5:08 PM
            • 3. Re: Difference between two time values - URGENT
              Sushant Sharma
              Hi,

              Try this:

              long diff = d2.getTime() - d1.getTime();
              long difSec = diff / 1000 % 60;

              --Sushant                                                                                                                                                                                                                   
              • 4. Re: Difference between two time values - URGENT
                rb4777
                Hi Sushant,
                For the given code :
                SimpleDateFormat format = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");

                Date d1 = null;
                Date d2 = null;

                try {
                d1 = format.parse(startTime);
                d2 = format.parse(endTime);
                } catch (ParseException e) {
                e.printStackTrace();
                }

                long diff = d2.getTime() - d1.getTime();



                I am getting the following error :
                Error(8181,18): incompatible types; found: java.util.Date, required: oracle.jbo.domain.Date
                Error(8187,16): method getTime not found in class oracle.jbo.domain.Date

                please help me on this, i am trying this code in AM

                Edited by: rbojja on Nov 14, 2012 5:48 PM
                • 5. Re: Difference between two time values - URGENT
                  Sushant Sharma
                  Hi,

                  Which class of Date you are using?

                  In your imports, use only java.util.Date.
                  Remove the other one (oracle.jbo.domain.Date).

                  --Sushant                                                                                                                                                                                                                                                                                                       
                  • 6. Re: Difference between two time values - URGENT
                    rb4777
                    Hi Sushant,

                    oracle.jbo.domain.Date
                    java.util.Date

                    are being used for some reasons, bcoz this code is handled by different developers earlier for their own. Now i cannot remove the imports bcoz of the above reason.

                    can you please give me any other solution to overcome this.

                    DateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
                    long time = System.currentTimeMillis();
                    java.sql.Date date = new java.sql.Date(time);
                    String startTime = dateFormat.format(date);
                    System.out.println("startTime..."+startTime);
                    Date d1 = null;
                    Date d2 = null;
                    try {
                    d1 = dateFormat.parse(startTime);  // error is occuring at this line
                    } catch (ParseException e) {
                    e.printStackTrace();
                    }
                    • 7. Re: Difference between two time values - URGENT
                      Sushant Sharma
                      Hi,

                      Check using this, if the issue is with right class or not using this:

                      java.util.Date d1 = null;
                      java.util.Date d2 = null;

                      --Sushant