Results 1 to 1 of 1
Thread: Date Validation Script
-
Date Validation Script
The code snippet below can be used for data validation.
Java Code:public static boolean validateDate(String dateStr, boolean allowPast, String formatStr) { if (formatStr == null) return false; // or throw some kinda exception, possibly a InvalidArgumentException SimpleDateFormat df = new SimpleDateFormat(formatStr); Date testDate = null; try { testDate = df.parse(dateStr); } catch (ParseException e) { // invalid date format return false; } if (!allowPast) { // initialise the calendar to midnight to prevent // the current day from being rejected Calendar cal = Calendar.getInstance(); cal.set(Calendar.HOUR_OF_DAY, 0); cal.set(Calendar.MINUTE, 0); cal.set(Calendar.SECOND, 0); cal.set(Calendar.MILLISECOND, 0); if (cal.getTime().after(testDate)) return false; } // now test for legal values of parameters if (!df.format(testDate).equals(dateStr)) return false; return true; }
Similar Threads
-
Struts validation
By Jack in forum Web FrameworksReplies: 3Last Post: 10-06-2008, 12:54 AM -
Difference between current date and anothe date
By vijay balusamy in forum New To JavaReplies: 1Last Post: 04-16-2008, 04:15 PM -
Swing validation
By ppayal in forum New To JavaReplies: 0Last Post: 02-09-2008, 08:59 AM -
java validation?
By lockmac in forum New To JavaReplies: 3Last Post: 08-14-2007, 04:34 PM -
Another Query On Validation
By Albert in forum Web FrameworksReplies: 1Last Post: 07-05-2007, 06:47 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks