4 Replies Latest reply on Jan 31, 2008 9:25 PM by 807603

    Validating date and time...I'm almost there

    807603
      Hi Guys

      Couldn't find an appropriate forum for this problem so thought I would post here.

      I have been struggling with this one for a while and thought that I had solved it. Basically I have two text fields one for date and one for time.

      I would like to display a message to the user whenever an inappropriate date or time is entered. Unfortunately date such as 44-23-2007 and time like 32:62 are slipping through the net.

      Any advice on this would be greatly appreciated, my code is shown below.

      Thanks
      GB
      boolean dateOK = true;
              DateFormat sdf1 = new SimpleDateFormat("dd-mm-yyyy");
              DateFormat sdf2 = new SimpleDateFormat("HH:mm:ss");
              
              AppointmentClass app = new AppointmentClass();
              
              try
              {
                  app.setAppDate(sdf1.parse(txtDate.getText()));  //we need to parse the date before saving
              }
              catch (ParseException ex)
              {            
                  //ex.printStackTrace();
                  dateOK = false;
                  OKDialog ok = new OKDialog(new javax.swing.JFrame(), true);
                  ok.message("Format of dates must be: dd-mm-yyyy");
                  ok.setVisible(true);
              }
              
              if(dateOK)
              {
                  try
                  {
                      app.setAppTime(sdf2.parse(txtTime.getText() + ":00"));
                      
                      app.setClientNumber(Integer.valueOf(txtClientNumber.getText()));
                      SaveDetails sd = new SaveDetails();
                      sd.insertAppointment(app);  //Add the appointment to the database
                      
                      dispose();
                  }
                  catch (ParseException ex)
                  {
                      //ex.printStackTrace();
                      OKDialog ok = new OKDialog(new javax.swing.JFrame(), true);
                      ok.message("Format of times must be: ##:##");
                      ok.setVisible(true);
                  }
              }
        • 1. Re: Validating date and time...I'm almost there
          807603
          simpleDateFormat.setLenient(false);
          You may find this useful as well: http://balusc.blogspot.com/2007/09/dateutil.html
          • 2. Re: Validating date and time...I'm almost there
            796254
            boolean dateOK = true;
                    DateFormat sdf1 = new SimpleDateFormat("dd-mm-yyyy");
                    sdf1.setLenient(false);  // this will eliminate erroneous values;
                    DateFormat sdf2 = new SimpleDateFormat("HH:mm:ss");
                    sdf2.setLenient(false);  // same here.        
                    AppointmentClass app = new AppointmentClass();
                    
                    try
                    {
                        app.setAppDate(sdf1.parse(txtDate.getText()));  //we need to parse the date before saving
                    }
                    catch (ParseException ex)
                    {            
                        //ex.printStackTrace();
                        dateOK = false;
                        OKDialog ok = new OKDialog(new javax.swing.JFrame(), true);
                        ok.message("Format of dates must be: dd-mm-yyyy");
                        ok.setVisible(true);
                    }
                    
                    if(dateOK)
                    {
                        try
                        {
                            app.setAppTime(sdf2.parse(txtTime.getText() + ":00"));
                            
                            app.setClientNumber(Integer.valueOf(txtClientNumber.getText()));
                            SaveDetails sd = new SaveDetails();
                            sd.insertAppointment(app);  //Add the appointment to the database
                            
                            dispose();
                        }
                        catch (ParseException ex)
                        {
                            //ex.printStackTrace();
                            OKDialog ok = new OKDialog(new javax.swing.JFrame(), true);
                            ok.message("Format of times must be: ##:##");
                            ok.setVisible(true);
                        }
                    }
            check the javadocs to make sure I spelled it correctly

            %
            • 3. Re: Validating date and time...I'm almost there
              807603
              Thanks for taking the time to reply guys.

              I tried the setLenient(false) and it almost works.
              It has sorted out the problem with the time, however it only catches days in the date and not months.
              For example this date 33-11-2000 will be caught, but this date 26-15-2000 won't.

              Thanks in advance
              GB
              • 4. Re: Validating date and time...I'm almost there
                807603
                Update

                Altered the format to dd-MM-yyyy and that's it !!!
                I have of course kept the setLenient(false).

                Many Thanks
                GB