cancel
Showing results for 
Search instead for 
Did you mean: 

webdynpro coding

Former Member
0 Kudos

Hi,

I am stuck in the following code:

// Check if date is valid

if (!DateCalculator.isValid(usrDate))

Description of the Error is The method isValid(Date) is undefined for the type StartView.

Please reply.

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

Hi Prithish,

isValid method defined for the DataCaculator , Can you please chekc that ?

Regards

Pankaj Prasoon

Former Member
0 Kudos

Actually i am trying to validate teh date field & i am not sure about DateCalculator.

How i can validate the date field?

Former Member
0 Kudos

Implement you own method isValidDate with this code instead of the DateCalculator class:


public boolean isValidDate(String date)
{
   // set date format, this can be changed to whatever format
   // you want, MM-dd-yyyy, MM.dd.yyyy, dd.MM.yyyy etc.
  
   SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
   
   Date testDate = null;

   // we will now try to parse the string into date form
   try
   {
     testDate = sdf.parse(date);
   }
  catch (ParseException e)
   {
     errorMessage = "the date you provided is in an invalid date" +
                             " format.";
     return false;
   }



   if (!sdf.format(testDate).equals(date))
   {
     errorMessage = "The date that you provided is invalid.";
     return false;
   }
   
   // if we make it to here without getting an error it is assumed that
   // the date was a valid one and that it's in the proper format

   return true;

} 

Regards,

Martin

Former Member
0 Kudos

Hi Pritish,

I am sorry but if you can tel what validation are you looking for. You can use the following code if ur checking for the date formt.

public boolean isValidDate(String date)

{

// set date format, this can be changed to whatever format

// you want, MM-dd-yyyy, MM.dd.yyyy, dd.MM.yyyy etc.

SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");

// declare and initialize testDate variable, this is what will hold

// our converted string

Date testDate = null;

// we will now try to parse the string into date form

try

{

testDate = sdf.parse(date);

}

// if the format of the string provided doesn't match the format we

// declared in SimpleDateFormat() we will get an exception

catch (ParseException e)

{

errorMessage = "the date you provided is in an invalid date" +

" format.";

return false;

}

// dateformat.parse will accept any date as long as it's in the format

// you defined, it simply rolls dates over, for example, december 32

// becomes jan 1 and december 0 becomes november 30

// This statement will make sure that once the string

// has been checked for proper formatting that the date is still the

// date that was entered, if it's not, we assume that the date is invalid

if (!sdf.format(testDate).equals(date))

{

errorMessage = "The date that you provided is invalid.";

return false;

}

// if we make it to here without getting an error it is assumed that

// the date was a valid one and that it's in the proper format

return true;

} // end isValidDate

Regards

Pankaj Prasoon

vmadhuvarshi_
Contributor
0 Kudos

Prithwish,

If you are using an input field to enter date value, you do not need to define any validation routine. Binding a Context attribute of type date to your inputfield is all that it takes. If you are trying to validate a Date field received from ABAP side, you may validate it with code below that I found [here|http://www.dreamincode.net/forums/showtopic14886.htm].


// date validation using SimpleDateFormat
// it will take a string and make sure it's in the proper
// format as defined by you, and it will also make sure that
// it's a legal date

public boolean isValidDate(String date)
{
   // set date format, this can be changed to whatever format
   // you want, MM-dd-yyyy, MM.dd.yyyy, dd.MM.yyyy etc.
   // you can read more about it here:
   // http://java.sun.com/j2se/1.4.2/docs/api/index.html
   
   SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
   
   // declare and initialize testDate variable, this is what will hold
   // our converted string
   
   Date testDate = null;

   // we will now try to parse the string into date form
   try
   {
     testDate = sdf.parse(date);
   }

   // if the format of the string provided doesn't match the format we
   // declared in SimpleDateFormat() we will get an exception

   catch (ParseException e)
   {
     errorMessage = "the date you provided is in an invalid date" +
                             " format.";
     return false;
   }

   // dateformat.parse will accept any date as long as it's in the format
   // you defined, it simply rolls dates over, for example, december 32
   // becomes jan 1 and december 0 becomes november 30
   // This statement will make sure that once the string
   // has been checked for proper formatting that the date is still the
   // date that was entered, if it's not, we assume that the date is invalid

   if (!sdf.format(testDate).equals(date))
   {
     errorMessage = "The date that you provided is invalid.";
     return false;
   }
   
   // if we make it to here without getting an error it is assumed that
   // the date was a valid one and that it's in the proper format

   return true;

} // end isValidDate

Hope it helps.

Vishwas.

Answers (0)