|
1. DATE VALIDATION. |
The code given in Table 1 describes a class which can be used to validate dates input using the DDMMYYYY format. For example the date 1-11-200 would be written as --- 1112000. To obtain the "day-month-year" elements from a number N of this format we proceed as follows:
Example, given N=1112000 (1-11-200): day = 1112000/1000000 = 1 month = (1112000%1000000)/10000 = 112000/10000 = 11 year = 1112000%10000 = 2000 |
Note that, from the code presented in Table 1, the cases in a switch statement do not have to be ordered in any way but can be grouped together in anyway as dictated by the application. The rule for identifying a leap year is as follows: This can be expressed as follows: (((yearNumber%4 == 0) && (yearNumber%100 != 0)) || (yearNumber%400 == 0)) A application program that makes use of the DataValidation class is presented in Table 2. (Note that this example is based on a similar example presented by Holmes, p105.) |
// DATE VALIDATION // Frans Coenen // Tuesday 16 July 1999 // The University of Liverpool, UK class DateValidation { // ------------------ FIELDS ------------------------ static private final int MIN_MONTH_NUMBER = 1; static private final int MAX_MONTH_NUMBER = 12; static private final int MIN_DAY_NUMBER = 1; private int dayNumber; private int monthNumber; private int yearNumber; // ------------------ CONSTRUCTORS ------------------------ /* ------ DATE VALIDATION ------ */ public DateValidation(int dateCode) { dayNumber = dateCode/1000000; monthNumber = (dateCode%1000000)/10000; yearNumber = dateCode%10000; } // ------------------ METHODS ------------------------ /* ------- CHECK MONTH ------ */ public boolean monthNumberWithinRange() { if ((monthNumber < MIN_MONTH_NUMBER) || (monthNumber > MAX_MONTH_NUMBER)) { System.out.println("ERROR: " + monthNumber + " is an invalid month number"); return(false); } else return(true); } public boolean dayNumberWithinRange() { if ((dayNumber < MIN_DAY_NUMBER) || (dayNumber > getNumDaysInMonth())) { System.out.println("ERROR: " + dayNumber + " is an invalid day number"); return(false); } else return(true); } /* ------ NUMBER OF DAYS IN MONTH ------ */ private int getNumDaysInMonth() { int numDaysInMonth=0; switch(monthNumber) { case 1: case 3: case 5: case 7: case 8: case 10: case 12: numDaysInMonth = 31; break; case 4: case 6: case 9: case 11: numDaysInMonth = 30; break; case 2: // Test for leap year if (((yearNumber%4 == 0) && (yearNumber%100 != 0)) || (yearNumber%400 == 0)) numDaysInMonth = 29; else numDaysInMonth = 28; break; default: System.out.println("ERROR: Cause unknown"); } // Return return(numDaysInMonth); } } |
Table 1: Date validation class implementation
// DATE VALIDATION APPLICATION // Frans Coenen // Wednesday 3 March 1999 // Modified: Saturady 17 September 2005 // University of Liverpool import java.io.*; class DateValidationApp { // ------------------- FIELDS ------------------------ // Create Scanner class instance public static Scanner keyboardInput = new Scanner(System.in); // ------------------ METHODS ------------------------ /* Main method */ public static void main(String argv[]) { int date; DateValidation newDateValidation; // Input date System.out.println("Input a date in DDMMYYYY format"); date = keyboardInput.nextInt(); // Create an instance of the class DataValidation newDateValidation = new DateValidation(date); // Check if (newDateValidation.monthNumberWithinRange()) { if (newDateValidation.dayNumberWithinRange()) System.out.println("Date format OK\n"); else System.out.println("Date format incorrect\n"); } else System.out.println("Date format incorrect\n"); } } |
Table 2: Date validation application program
Created and maintained by Frans Coenen. Last updated 10 February 2015