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

Calender in JSP 1

Status
Not open for further replies.

kishorecvr

Programmer
Feb 21, 2002
19
0
0
IN
How to get calender control for date field in JSP

Thanks In Advance.
 
u can use the following methods in a JAva class which needs to be included in the JSp.

//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;
}
 
That is clean way to do it if you are going to only have one field for the date. If you are going to have multiple fields for the dates, I would suggest using the Calendar class. The functionality is greater and it helps with validation. I think it is under the java.util library. Just a thought.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top