Write a program that converts dates from numerical month/day/year format to norm
ID: 3920847 • Letter: W
Question
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.)
Explanation / Answer
here is your files : ---------->>>>>>>>
MonthException.java : -------------
public class MonthException extends Exception{
public MonthException(String str){
super(str);
}
}
DayException.java : --------------
public class DayException extends Exception{
public DayException(String str){
super(str);
}
}
YearException.java : -----------------------
public class YearException extends Exception{
public YearException(String str){
super(str);
}
}
ConvertDates.java : -------------->>>>>>
import java.util.Scanner;
public class ConvertDates{
public static String checkMonth(int month)throws MonthException{
String[] months = {"January","February","March","April","May","June","July","August","September","October","November","December"};
if(month < 1 || month > 12){
throw new MonthException("Not a valid Month !!!");
}
return months[month-1];
}
public static void checkYear(int ye)throws YearException{
if(ye < 1000 || ye > 3000){
throw new YearException("Invalid Year !!! ");
}
}
public static void checkDay(int d,int val)throws DayException{
if(d < 1 || d > val){
throw new DayException("Invalid Day !!! ");
}
}
public static String convert(String dates){
String[] str = dates.split("/");
String mn = null;
int iy = Integer.parseInt(str[2]);
boolean st = true;
Scanner sc = new Scanner(System.in);
while(st){
try{
checkYear(iy);
st = false;
}catch(YearException e){
System.out.println(e);
System.out.println("Enter a valid Year (1000-3000) : ");
iy = sc.nextInt();
st = true;
}catch(Exception e){
System.out.println(e);
System.out.println("Enter a valid year (1000-3000) : ");
iy = sc.nextInt();
st = true;
}
}
int imn = Integer.parseInt(str[0]);
st = true;
while(st){
try{
mn = checkMonth(imn);
st = false;
}catch(MonthException e){
System.out.println(e);
System.out.println("Enter a valid month (1-12) : ");
imn = sc.nextInt();
st = true;
}catch(Exception e){
System.out.println(e);
System.out.println("Enter a valid month (1-12) : ");
imn = sc.nextInt();
st = true;
}
}
int id = Integer.parseInt(str[1]);
int vd = 0;
st = true;
if(imn == 1 || imn == 3 || imn == 5 || imn == 7 || imn == 8 || imn == 10 || imn == 12){
vd = 31;
}else if(imn == 4 || imn == 6 || imn == 9 || imn == 11){
vd = 30;
}else if(imn == 2){
if(iy%100 == 0 && iy % 400 == 0){
vd = 29;
}else if(iy%100 != 0 && iy % 4 == 0){
vd = 29;
}else{
vd = 28;
}
}
while(st){
try{
checkDay(id,vd);
st = false;
}catch(DayException e){
System.out.println(e);
System.out.println("Enter a valid Day (1-"+vd+") : ");
id = sc.nextInt();
st = true;
}catch(Exception e){
System.out.println(e);
System.out.println("Enter a valid day (1-"+vd+") : ");
id = sc.nextInt();
st = true;
}
}
return mn+" "+id+", "+iy;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String date = null;
System.out.println("Enter a date (mm/dd/yyyy) format ");
date = sc.next();
date = convert(date);
System.out.println("Formated Date : "+date);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.