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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

custmisable date bean 1

Status
Not open for further replies.

kedarhukeri

Programmer
Dec 30, 2002
4
0
0
DE
Friends,
I want to use a date bean in my jsp page with few rules.
User can enter the date
in DDMMYY
or DD.MM.YY
or DD.MM.YYYY
and enter it in database in YYYYMMDD format.
also user should select the date only less than 30,60, or x days ahead and back of the current date.
else the current date will be entered.
------------------------------------
If anyone has done this pls help me.
I think javascript wont be good option as diff. browsers behave abruptly with it.
Any comments tips for the DATE bean are most wellcomed.

regards
kedar
 
It is better that u validate the date in Java class & include that class in the JSP.
For my apllication the date format was MM/DD,YYYY

I will just give u the methods used by me while validating the date.

//Method to check the date format
public static boolean checkFormat(String date)
{
char ch1,ch2;
date=date.trim();
int len=date.length();
if (len!=10) {
return false;
}

ch1=date.charAt(2);
ch2=date.charAt(5);

if(ch1=='/' && ch2==',') {
return true;
}
else return false;
}

//Method to check if the input date is after the current date
public static boolean after(String inputDate)
{
SimpleDateFormat df= new SimpleDateFormat("MM/dd,yyyy");
Date date=null;
try{
date= df.parse(inputDate);
}
catch(ParseException e) {
return false;
}
Date currentDate=new Date();
boolean result=date.after(currentDate);
return result;
}

//Method to compare the input dates
public static int compare(String fromDate,String toDate)
{
SimpleDateFormat df1= new SimpleDateFormat("MM/dd,yyyy");
SimpleDateFormat df2= new SimpleDateFormat("MM/dd,yyyy");
Date date1=null,date2=null;
try{
date1= df1.parse(fromDate);
date2= df2.parse(toDate);
} catch(ParseException e) {
}

int result=date1.compareTo(date2);
return result;
}

//Method to convert the date format
public static String convertDateFormat(String inputDate)
{
String year=inputDate.substring(0,4);
String month=inputDate.substring(5,7);
String day=inputDate.substring(8,10);
String outputDate=month+"/"+day+","+year;
return outputDate;
}

//Method to validate the date
public static boolean validateDate(String inputDate)
{
java.util.Date dNormalDate;
boolean res=true;
try
{
SimpleDateFormat sdfFormat;
sdfFormat = new SimpleDateFormat("M/d,yyyy");
dNormalDate = sdfFormat.parse(inputDate, new ParsePosition(0) );

int dt_l = inputDate.length();
Calendar c = new GregorianCalendar();
c.setTime(dNormalDate);
int cDate = c.get(Calendar.DAY_OF_MONTH);
int cMonth = c.get(Calendar.MONTH) + 1;
int cYear = c.get(Calendar.YEAR);

int iSeparatorA = inputDate.indexOf("/");
int iSeparatorB = inputDate.indexOf(",");
int iMonth = Integer.parseInt(inputDate.substring(0,iSeparatorA));
int iDate = Integer.parseInt(inputDate.substring(iSeparatorA+1, iSeparatorB));
int iYear = Integer.parseInt(inputDate.substring(iSeparatorB+1, dt_l));

if(cDate != iDate || cMonth != iMonth
|| cYear != iYear || iYear < 1753 || iYear > 9999)
{
res = false;
}
} catch(Exception e)
{
res = false;
}
return res;
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top