Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Date Exceptions Objective: Write a class called DateConverter which has a static

ID: 3766322 • Letter: D

Question

Date Exceptions

Objective:  

Write a class called DateConverter which has a static method that takes in a date as a string (such as 01/21 or 1/21) and then returns a string with the date formatted with the alphanumeric month (such as January 21). This method throws two different exceptions DayException and MonthException, which you need to write yourself. The MonthException is thrown if the numeric month is not 1-12, and the DateException is thrown if the day is less than 0 or greater than the max possible day for that month. In other words:

·     January 31 days

·     February 29 days

·     March 31 days

·     April 30 days

·     May 31 days

·     June 30 days

·     July 31 days

·     August 31 days

·     September 30 days

·     October 31 days

·     November 30 days

·     December 31 days

Finally write a driver DateConverterDriver that prompts the user for a date and then using the DateConverter either converts it or returns the exception message. Keep running this until they choose to quit.

Example Dialog:

Welcome to the date converter!

Enter a numeric date formatted as month/day or “Exit” to quit.

1/21

The date is January 21

Enter a numeric date formatted as month/day or “Exit” to quit.

-1/30

Month Exception: Months must between 1 and 12 inclusively.

Enter a numeric date formatted as month/day or “Exit” to quit.

4/31

Day Exception: This day is in the wrong range for the month provided.

Enter a numeric date formatted as month/day or “Exit” to quit.

Quit

Good bye

Explanation / Answer

public class DateConverter {

public static void main(String args[]) throws InterruptedException {

//creating instances of java.util.Date which represents today's date and time
java.util.Date now = new java.util.Date();
System.out.println("Value of java.util.Date : " + now);

//converting java.util.Date to java.sql.Date in Java
java.sql.Date sqlDate = new java.sql.Date(now.getTime());
System.out.println("Converted value of java.sql.Date : " + sqlDate);

//converting java.sql.Date to java.util.Date back
java.util.Date utilDate = new java.util.Date(sqlDate.getTime());
System.out.println("Converted value of java.util.Date : " + utilDate);
}

}


Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote