5 Replies Latest reply: May 15, 2014 4:15 AM by Tshifhiwa RSS

    compare date with compareTo

    Tshifhiwa

      hi i what to compare date on the following request

      the value date transction must be within the Active date(>= greater than or equal) and inactive date(<= less than) date range

       

      this is the code i come with,if i check both date which is date transction must be within the Active date(= greater than or equal) and date transction must be within the Active date(> greater than or equal),it only validate the date which  is = the other file fail

       

       

        • 1. Re: compare date with compareTo
          gimbal2

          Blegh, my head automatically bangs on the desk when I try to read that; I can very much understand that you can't solve problems in it when you have to invest all your brainpower even trying to read what is currently there.

           

          Here is a suggestion: clean up the code. Use more logical variable names to begin with, make it more readable.

          • 2. Re: compare date with compareTo
            Tshifhiwa

            what do you mean Use more logical variable names to begin with

             

            i did make it more readbale

            • 3. Re: compare date with compareTo
              gimbal2

              You say that, yet you come here because you can't figure out your own code.

              • 4. Re: compare date with compareTo
                jschellSomeoneStoleMyAlias

                > the value date transction must be within the Active date(>= greater than or equal) and inactive date(<= less than) date range

                 

                Bad idea.  Range checks should always be inclusive on one end and exclusive on the other otherwise the same data could show up in two 'different' ranges.

                 

                Code should look basically like the following with assurance that startDate is less than endDate

                Date txnDate = ...

                Date startDate = ...

                Date endDate = ...

                 

                if ((startDate.compareTo(txnDate) <= 0) && (endDate.compareTo(txnDate) > 0))  { in range}

                • 5. Re: compare date with compareTo
                  Tshifhiwa

                  hi this is my code

                  try

                                  {

                                      String s_ActivDate = getValueFromTable("S","INDUSTRIAL_CLASSIFICATION", "IND_CLASS_CODE", s_indClass, "IND_CLASS_DATE_ACTIVE");

                                      String s_InactiveDate = getValueFromTable("S","INDUSTRIAL_CLASSIFICATION", "IND_CLASS_CODE", s_indClass, "IND_CLASS_DATE_INACTIVE");

                   

                                      if(!isBlank(s_ActivDate))

                                      {

                                          if (!isActive(sDate, s_InactiveDate, s_ActivDate))

                                          {

                                              em.setS_errorMessage("ResidentCustomerAccountHolder - IndustrialClassificationInactive2", 313, cc);

                                         }

                                         

                                      }

                                  } catch (SQLException ex)

                                  {

                                      throw new SQLException(ex);

                                  }

                   

                              }

                   

                  public boolean isActive(String sValueDate, String sInactive, String sActive) throws SQLException

                      {

                          Date i_date;

                          Date a_date;

                      

                          System.out.println("PasseddateTrans "+sValueDate);

                          try

                          {

                              System.out.println("comingdate "+sActive);

                              System.out.println("comingdateTrans "+sValueDate);

                              System.out.println("comingdatesInactive "+sInactive);

                              if(!isBlank(sActive))

                              {

                              if(!isBlank(sValueDate))

                              {

                              if(!isBlank(sInactive))

                              { 

                              a_date = new SimpleDateFormat("yyyy-MM-dd").parse(sActive);

                              Date dt_valueDate = new SimpleDateFormat("yyyy-MM-dd").parse(sValueDate);

                          

                              System.out.println("comingdateAfter "+a_date);

                              System.out.println("comingdateDateTransAfter "+dt_valueDate);

                          

                              if(!isBlank(sInactive))

                              {

                                  i_date = new SimpleDateFormat("yyyy-MM-dd").parse(sInactive);

                                  if (dt_valueDate.compareTo(i_date) < 0 && dt_valueDate.compareTo(a_date) >= 0)

                                  //    if(dt_valueDate.compareTo(i_date) <= 0)                 

                                      System.out.println("Comapredate*///*****"+ dt_valueDate +"<"+ i_date + "<"+ dt_valueDate +">"+ a_date);

                                  {

                                      System.out.println("Comapredate dt_valueDate "+ dt_valueDate );

                                      System.out.println("Comapredatei_date "+ i_date );

                                      System.out.println("*******************&&&&"  );

                                      System.out.println("dt_valueDate****** "+dt_valueDate);

                                       System.out.println("a_date******"+a_date);

                                      return true;

                                  }

                              }else

                              {

                                  if (dt_valueDate.compareTo(a_date) >= 0)

                                  {

                                      System.out.println("Comapredate dt_valueDate+++++ "+ dt_valueDate );

                                      System.out.println("Comapredatei_date------ "+ a_date );

                                 

                                      return true;

                                  }

                              }

                              }

                              }

                              }

                          } catch (ParseException ex)

                          {

                              java.util.logging.Logger.getLogger(Validate.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);

                          }catch(NullPointerException npe)

                          {

                              return false;

                          }

                      

                          return false;

                      }