Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

3 qns regarding string, date, validating date, etc.. Help.

Status
Not open for further replies.

flyclassic22

Technical User
Oct 1, 2002
54
SG
hi, i'm doing a project with java updating a msaccess database. before going into updating database with sql , i need to validate the string input by user,
i need some help on how do i valid a string input by a user(jtextfield). The string is suppose to be a date to be entered. and following format DD/MM/YY .
First: How do i check to see if user enter in the correct format and not having characters like "A, B, C" and following the format is state.?using substring? i need examples to let me learn how to do it....
2nd: Check if the date is valid if the string is in correct format
I've read examples of SimpleDateFormat, but not very sure how to use it validate , i've an example but it has time in it, but i do not want the format to have time, i just want the date.


3rd: Once i get the date in string, can anyone tell me the concept of going about doing taking the "now" date and compare with the date user has enter(DOB) and use it to calculate the age of the user.

Anyone who have tutorials, sample advice or anything that can help any of my three question please kindly help me!
I'm newbie to JAVA. I appreciate every tip received. THANKS in advance.
 
I have attached a JS Function which will validate a date in teh format u have specified. You can do it in the front end itself rather than going to the middler Layer for this purpose.

Hope this helps...

/* Function to validate the date */
function validateDate(strDate)
{
var strFormatError = "The date you have specified is not in the proper format. Please enter the Date in the format MM/DD/YYYY";
var strInvalidMonthError = "The month of the Field you have specified is not valid. Please enter a valid month.";
var strInvalidDayError = "The date you have specified is invalid. Please enter a valid value.";
var arrData = new Array(3);
arrData = strDate.split('/');

if(arrData.length != 3)
{
alert(strFormatError);
return false;
}
if(isNaN(arrData[0]))
{
alert(strFormatError);
return false;
}
if (parseInt(arrData[0], 10) < 1)
{
alert(strInvalidDayError);
return false;
}
if(isNaN(arrData[1]))
{
alert(strFormatError);
return false;
}
if (parseInt(arrData[1], 10) < 1)
{
alert(strInvalidDayError);
return false;
}
if(isNaN(arrData[2]))
{
alert(strFormatError);
return false;
}
if(arrData[2].length != 4)
{
alert(strFormatError);
return false;
}
if(parseInt(arrData[2], 10) <= 0)
{
alert(&quot;The year you have specified is not valid. Please enter a valid year&quot;);
return false;
}

var intMonth = parseInt(arrData[0], 10);
var intDay = parseInt(arrData[1], 10);
var intYear = parseInt(arrData[2], 10);

if (intMonth < 1 || intMonth > 12)
{
alert(strFormatError);
return false;
}

var strMonth = arrMonth[intMonth-1];

if ((strMonth == 'January' || strMonth == 'March' || strMonth == 'May' || strMonth == 'July' ||
strMonth == 'August' || strMonth == 'October' || strMonth == 'December') && (intDay > 31))
{
alert(intDay + ' is not a valid day for Month ' + strMonth);
return false;
}
if ((strMonth == 'April' || strMonth == 'June' || strMonth == 'September' || strMonth == 'November') && (intDay > 30))
{
alert(intDay + ' is not a valid day for Month ' + strMonth);
return false;
}
if ((strMonth.toUpperCase()) == 'FEBRUARY')
{
if ((intDay > 28) && (intYear%4 != 0))
{
alert(intDay + ' is not a valid day for Month ' + strMonth+ ' of Year ' + intYear);
return false;
}
if ((intDay > 29) && (intYear%4 == 0))
{
alert(intDay + ' is not a valid day for Month ' + strMonth+ ' of Year ' + intYear);
return false;
}
}
return true;
}
 
Here is the general idea. Look at the SDK for more details.
Code:
class DateParser{
	static void rnd( String sdate) throws Exception{
		
		java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat(&quot;MM/dd/yyyy&quot;);
		java.util.Date dt = sdf.parse(sdate);
		System.out.println(&quot;Date: &quot; + dt.toLocaleString());
	}
}

-pete
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top