In: Computer Science
Write a program that prompts the user to enter a date in the
format mm/dd/yyyy
where mm is the month , dd is the day and yyyy is a 4 digit
year
Check if the date is valid by seeing month is between 1 and 12, day
is between 1 to 31 and year is between 1800 to 3000. Also check
that if month is 2, day is between 1 to 29
If the date is valid then display the date back in following format
dd month year. Use of SimpleDateFormat Java Class is NOT allowed to
do this assignment. You need to use String functions from Chapter 4
to do this assignment.
Following is a sample run:
Enter a date in the format mm/dd/yyyy:
02/23/1999
23 Feb 1999
Following is another sample run:
Enter a date in the format mm/dd/yyyy:
02021999
Input String Length should be 10
Following is another sample run:
Enter a date in the format mm/dd/yyyy:
02-23-2000
Invalid Date Format
Following is another sample run:
Enter a date in the format mm/dd/yyyy:
23/02/2000
Invalid Date
import java.util.Scanner;
public class StringDate {
public static void main(String[] args) {
String arr[] = { " ", "Jan", "Feb",
"Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep",
"Oct", "Nov", "Dec" };
System.out.println("Enter a date in
the format ");
Scanner sc = new
Scanner(System.in);
String date = sc.next();
//checking if it has invalid
length
if (date.length() < 10) {
System.out.println("Input String Length should be 10");
return;
}
//splitting the string to get
day,month,year
String splt[] =
date.split("/");
int day =
Integer.parseInt(splt[1]);
int month =
Integer.parseInt(splt[0]);
int year =
Integer.parseInt(splt[2]);
//checking if it valid year and
month
if (year < 1800 || year >
3000 || month < 1 || month > 12) {
System.out.println("Invalid Date format");
return;
}
if(!(day>=1 &&
day<getDays(month))){
System.out.println("Invalid Date format");
return;
}
System.out.println(day+"
"+arr[month]+" "+year);
}
private static int getDays(int mm) {
int res = -1;
switch (mm) {
case 1:
res = 31;
break;
case 2:
res = 29;
break;
case 3:
res = 31;
break;
case 4:
res = 30;
break;
case 5:
res = 31;
break;
case 6:
res = 30;
break;
case 7:
res = 31;
break;
case 8:
res = 31;
break;
case 9:
res = 30;
break;
case 10:
res = 31;
break;
case 11:
res = 30;
break;
case 12:
res = 31;
break;
}
return res;
}
}

Note : Please comment below if you have concerns. I am here to help you
If you like my answer please rate and help me it is very Imp for me