Write a program that converts dates from numerical monthdayy

Write a program that converts dates from numerical “month/day/year” format to normal “month day, year” format (for example, 12/25/2000 corresponds to December 25, 2000). You will define three exception classes, one called MonthException, another called DayException, and a third called YearException. If the user enters anything other than a legal month number (integers from 1 to 12), your program will throw and catch a MonthException and ask the user to reenter the month. Similarly, if the user enters anything other than a valid day number (integers from 1 to either 28, 29, 30, or 31, depending on the month and year), then your program will throw and catch a DayException and ask the user to reenter the day. If the user enters a year that is not in the range 1000 to 3000 (inclusive), then your program will throw and catch a YearException and ask the user to reenter the year. (There is nothing very special about the numbers 1000 and 3000 other than giving a good range of likely dates.) Use the following rule for determining if the year is a leap year: A year is a leap year if it is divisible by 4 but is not divisible by 100, or if it is divisible by 400.

Solution

DateException.java

import java.util.Scanner;

public class DateException {

   public static void main(String[] args) {
       //Declaring variables
       int day, month, year, month_days = 0;
       String date=\"\";
      
       //Create a month_name[] array and initialized
       String month_name[]={\"January\",\"February\",\"March\",\"April\",\"May\",\"June\",\"July\",\"August\",\"September\",\"October\",\"November\",\"December\"};
      
       //Scanner class object is used to read the inputs entered by the user
       Scanner sc = new Scanner(System.in);

      
       //Getting the date entered by the user
       System.out.println(\"Enter Date :\");
       date = sc.next();
      
       //Parsing the date using the delimeter \"/\"
       String dat[] = date.split(\"/\");
      
       //Converting the String type to Integer type variable
       month = Integer.parseInt(dat[0]);
       while (true) {
           try {
               //If the month is within the range then throw Exception and display Error message
               if (month < 1 || month > 12) {
                   throw new MonthException(\"** Month Exception,Must be between 1-12 **\");
                  
               } else
                   break;
           } catch (MonthException e) {
              
               //Getting the Month Entered by the user
               System.out.print(\"Enter Month :\");
               month = sc.nextInt();
               continue;
           }

       }

       //Converting the String to the Integer type
       day = Integer.parseInt(dat[1]);
      
       //Converting the String type to integer type
       year = Integer.parseInt(dat[2]);
      
       //If the year is not within range the throw Exception and Display error
       while (true) {
           if (year < 1000 || year > 3000) {
               try {
                   throw new YearException(\"** Year Exception.Must be between 1000-3000 **\");
               } catch (YearException e) {
                  
                   //Getting the year entered by the user
                   System.out.print(\"Enter Year :\");
                   year = sc.nextInt();
                   continue;
               }
           } else
               break;
       }
//Based on the month entered by the user getting the no of days in the month
       if (month == 1) {
           month_days = 31;
       } else if (month == 2) {
          
           /* Checking whether the year is leap year or not
           * If yes,then no of days is 29
           * if not,then no of days is 28
           */
           if (year % 400 == 0) {
               month_days = 29;
           } else if (year % 100 == 0) {
               month_days = 28;
           } else if (year % 4 == 0) {
               month_days = 29;
           } else {
               month_days = 28;
           }
       } else if (month == 3) {
           month_days = 31;
       } else if (month == 4) {
           month_days = 30;
       } else if (month == 5) {
           month_days = 31;
       } else if (month == 6) {
           month_days = 30;
       } else if (month == 7) {
           month_days = 31;
       } else if (month == 8) {
           month_days = 31;
       } else if (month == 9) {
           month_days = 30;
       } else if (month == 10) {
           month_days = 31;
       } else if (month == 11) {
           month_days = 30;
       } else if (month == 12) {
           month_days = 31;
       }

       while (true) {
           /* Checking the user entered day is valid or not
           * If not,then throw Exception and Display the error message
           * Prompts again to enter
           */
           if (day < 1 || day > month_days) {
               try {
                   throw new DayException(\"** Day Exception.Must be between 1-\"+month_days+\"**\");
               } catch (DayException e) {
                   System.out.print(\"Enter Day :\");
                   day = sc.nextInt();
                   continue;
               }

           } else
               break;
       }

       //Displaying the final Output
       System.out.print(\"\ Date :\"+month_name[month-1]+\" \"+day+\",\"+year);
   }

}

________________________

DayException.java

public class DayException extends Exception {

   public DayException(String string) {
       System.out.println(string);
   }

}

_______________________

MonthException.java

public class MonthException extends Exception {

   public MonthException(String string) {
       System.out.println(string);
   }

}

___________________________

YearException.java

public class YearException extends Exception {

   public YearException(String string) {
       System.out.println(string);
   }

}

_____________________________

Output1:

Enter Date :
13/32/3001
** Month Exception,Must be between 1-12 **
Enter Month :12
** Year Exception.Must be between 1000-3000 **
Enter Year :2016
** Day Exception.Must be between 1-31**
Enter Day :31
December 31,2016

________________________

output2:

Enter Date :
05/06/1984

Date :May 6,1984

__________Thank You

Write a program that converts dates from numerical “month/day/year” format to normal “month day, year” format (for example, 12/25/2000 corresponds to December 2
Write a program that converts dates from numerical “month/day/year” format to normal “month day, year” format (for example, 12/25/2000 corresponds to December 2
Write a program that converts dates from numerical “month/day/year” format to normal “month day, year” format (for example, 12/25/2000 corresponds to December 2
Write a program that converts dates from numerical “month/day/year” format to normal “month day, year” format (for example, 12/25/2000 corresponds to December 2

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site