5 Replies Latest reply: Jan 30, 2014 12:21 AM by Tshifhiwa RSS

    date validation

    Tshifhiwa

      hi how can i validate the date to be DDDCCYYnnnnnnn and CC Must not be less than current year

        • 1. Re: date validation
          PhHein

          What are CC and n supposed to be? They are not part of the formats listed: SimpleDateFormat (Java Platform SE 7 )

          • 2. Re: date validation
            Tshifhiwa

            CC IS the centuary and nnnn is a numeric sequatial counter starting at 0000001

            • 3. Re: date validation
              PhHein

              So CCYY is currently 2014? If so, get those four digits from your input and convert them to an Integer and compare them with the current year.

              • 4. Re: date validation
                TPD-Opitz

                In the other (closed) thread you say you need to report duplicates.

                To achief this you need to remember already checked dates. I'd suggest a HashSet. like this (made some changes to make it compile):

                 

                 

                public class UniquenessTest {
                    public static void main(String[] args) {
                        UniquenessTest uniquenessTest = new UniquenessTest();
                
                        List<String> validNumbers = Arrays.asList("000201401030234509",
                                "000201401030234508", "000201401030234507",
                                "000201401030234506", "000201401030234505");
                        for (String validNumber : validNumbers) {
                            uniquenessTest.testDate(validNumber);
                            System.out.println(validNumber + " is OK");
                        }
                        List<String> invalidNumbers = Arrays.asList("", "A00201401030234505",
                                "000201401030234509");
                        for (String invalidNumber : invalidNumbers) {
                            try {
                                uniquenessTest.testDate(invalidNumber);
                                throw new RuntimeException("expected exception not thown!");
                            } catch (RuntimeException ex) {
                                System.out.println("got expected exception for "
                                        + invalidNumber + ": " + ex.toString());
                            }
                        }
                    }
                
                    private final Set<String> seenCodes = new HashSet<String>();
                
                    private void testDate(String s_Ref) {
                        s_Ref = s_Ref.trim();
                        // this check first
                        if ("".equals(s_Ref)) {
                            // better thow exceptions, saves indentations...
                            throw new RuntimeException("Reference" + 102);
                
                        }
                
                        if (!s_Ref.matches("[0-9]{18}")) {
                            throw new RuntimeException("Reference" + 533);
                
                        }
                
                        // length already checkd by pattern match
                        // if(s_Ref.length() != 14)
                        // {
                        // em.setS_errorMessage("Reference", 201,cc);
                        // }else {
                
                        // get the adcode and validate
                
                        String s_adCode = s_Ref.substring(0, 3);
                
                        // transactionFile.setsAdCode(s_adCode);
                        // transactionFile.sAdCode = s_adCode;
                
                        s_adCode = (Integer.parseInt(s_adCode)) + "";
                
                        // if(!isMemberOf(s_adCode, cc.al_codes))
                        // {
                        // em.setS_errorMessage("Reference", 202,cc);
                        //
                        // }else
                        // {
                
                        // get the year and validate
                
                        String s_year = s_Ref.substring(3, 7);
                        Calendar cal = Calendar.getInstance();
                        String s_currYear = cal.get(Calendar.YEAR) + "";
                        // if(!isEqualTo(s_year.substring(0, 2), "20") ||
                        // !isEqualTo(s_year.substring(2,4), s_currYear.substring(2, 4)))
                        if (!s_year.substring(0, 4).equals(s_currYear.substring(0, 4))) {
                            throw new RuntimeException("Reference" + 201);
                        }
                
                        if (seenCodes.contains(s_Ref)) {
                            throw new RuntimeException("Reference" + 105);
                        }
                        seenCodes.add(s_Ref);
                    }
                }
                

                 

                bye

                TPD

                • 5. Re: date validation
                  Tshifhiwa

                  thanks got it

                  public void checkduplicate(){

                      String s_Ref = this.transactionFile.getsReference();

                    

                      String[] arrayOfRef = { s_Ref };

                        

                          Arrays.sort(arrayOfRef);

                           for(int i = 1; i < arrayOfRef.length; i++) {

                           if(arrayOfRef[i] == arrayOfRef[i - 1]) {

                          System.out.println("Duplicate: " + arrayOfRef[i]);

                        

                        

                        

                      }