/* Java application that prompts the user to enter a string intended to ** represent a calendar date. This input is then passed to six methods ** in the DateConverters class, each of which attempts to recognize that ** string as being a calendar date in one of these formats: ** ** Format Example ** --------------- --------------- ** mm/dd/yyyy 10/08/2019 ** dd-mm-yyyy 08-10-2019 ** yyyy-mm-dd 2019-10-08 ** dd-Mon-yyyy 08-Oct-2019 ** Month day, year October 08, 2019 ** day Month year 08 October 2019 ** ** Each method attempts to convert the given string into the corresponding ** date in the yyyymmdd format (e.g., "20191008"), and the results are ** printed for the user to see. ** ** This process repeats as long as the user enters a non-empty string, ** at which point the program ends. ** ** Author: P.M. Jackowitz & R. McCloskey ** Date: October 2019 */ import java.util.Scanner; public class DateConversionDriver { // Global constants public static final char QUOTES = '"'; public static final char SLASH = '/'; public static final char BLANK = ' '; // Scanner object through which input data is read from the keyboard. public static Scanner keyboard = new Scanner(System.in); public static void main(String[] args) { String givenDate; do { // Prompt user to enter a calendar date value. System.out.print("\nEnter a calendar date:>"); givenDate = keyboard.nextLine().trim(); // Check for loop terminating condition if(givenDate.length() > 0) { doConversions(givenDate); } } while (givenDate.length() > 0); System.out.println("Done!"); } /* Applies each of the conversion methods in the DateConverters class ** to the given string, reporting each result. (If a conversion method ** throws an exception, the reported result is the empty string.) */ private static void doConversions(String date) { final String EXCEP_MESS = "Exception!!!"; // Convert assuming givenDate is in the mm/dd/yyyy form System.out.print(Pad.padRight("mm/dd/yyyy",16,BLANK)); try { String yyyymmddDate = DateConverters.convertMMDDYYYY(date); System.out.println(quoted(yyyymmddDate)); } catch (Exception e) { //yyyymmddDate = ""; System.out.println(EXCEP_MESS); } // Convert assuming date is in the dd-mm-yyyy form System.out.print(Pad.padRight("dd-mm-yyyy",16,BLANK)); try { String yyyymmddDate = DateConverters.convertDDMMYYYY(date); System.out.println(quoted(yyyymmddDate)); } catch (Exception e) { //yyyymmddDate = ""; System.out.println(EXCEP_MESS); } // Convert assuming date is in the yyyy-mm-dd form System.out.print(Pad.padRight("yyyy-mm-dd",16,BLANK)); try { String yyyymmddDate = DateConverters.convertYYYYMMDD(date); System.out.println(quoted(yyyymmddDate)); } catch (Exception e) { //yyyymmddDate = ""; System.out.println(EXCEP_MESS); } // Convert assuming date is in the dd-Mon-yyyy form System.out.print(Pad.padRight("dd-Mon-yyyy",16,BLANK)); try { String yyyymmddDate = DateConverters.convertDDMonYYYY(date); System.out.println(quoted(yyyymmddDate)); } catch (Exception e) { //yyyymmddDate = ""; System.out.println(EXCEP_MESS); } // Convert assuming date is in the "Month day, year" form System.out.print(Pad.padRight("Month day, year",16,BLANK)); try { String yyyymmddDate = DateConverters.convertMonthDayYear(date); System.out.println(quoted(yyyymmddDate)); } catch (Exception e) { //yyyymmddDate = ""; System.out.println(EXCEP_MESS); } // Convert assuming givenDate is in the "day Month year" form System.out.print(Pad.padRight("day Month year",16,BLANK)); try { String yyyymmddDate = DateConverters.convertDayMonthYear(date); System.out.println(quoted(yyyymmddDate)); } catch (Exception e) { //yyyymmddDate = ""; System.out.println(EXCEP_MESS); } } // utility method // -------------- /* Functional method that merely returns the given string surrounded ** by quotes. */ public static String quoted(String s) { return QUOTES + s + QUOTES; } }