In: Computer Science
Coding in Java
Examples:
I have implemented these method under a single class Test , so in order to use them(for getting output) i have made them static and used them under our main method.
Since there is no information about input, I have asked user input to enter day, month ,year separately and then perform the tasks.(Please see output screenshots to get clarity).
Please go through the code, test run it for various inputs, Open for suggestions, Thanks!
import java.util.Scanner; //to prompt for user input
public class Test {
public static void main(String[] args) {
Scanner sc = new
Scanner(System.in);// to prompt user input
System.out.print("Enter month:
");
int month = sc.nextInt();// stores
month
System.out.print("Enter day:
");
int day = sc.nextInt();// stores
day
System.out.print("Enter
year:");
int year = sc.nextInt();// stores
year
/*
* method call isDateValid(m,d,y) to
check if the date entered is valid or not
*/
if (isDateValid(month, day, year))
{
System.out.println(month + "/" + day + "/" + year + " Valid
Date");// prints if the date is valid
} else {
System.out.println(month + "/" + day + "/" + year + " Invalid
Date");// prints if the date is invalid
}
sc.close();
}
/*month, day, year are passed as argument to below
function to test whether it forms a valid date or not*/
private static boolean isDateValid(int month, int day,
int year) {
boolean isDateValid =
false;//boolean to validate the date
int lastDayOfMonth =
getLastDayOfMonth(month, year);/*stores the last day of the
month(passed as arg)*/
/*checks if month, day, year are
positive values*/
if (month > 0 && day
> 0 && year > 0) {
/*if month
entered is invalid getLastDayOfMonth returns 0(look for method
implementation)*/
if
(lastDayOfMonth == 0) {
/*returns false here since isValidDate was
initialized as false*/
return isDateValid;
}
/*if its a valid
month then check for entered day as valid or not should be between
1 and lastDayOfMonth*/
if (day >= 1
&& day <= lastDayOfMonth) {
isDateValid = true;
return isDateValid;//returns true here since
date is found valid
}
}
return isDateValid;//returns false
here for negative values of either of day, month, year
}
/*returns the last day of month and year passed as
argument*/
private static int getLastDayOfMonth(int month, int
year) {
/*if month is valid i.e between 1
and 12*/
if ((month >= 1 && month
<= 12)) {
/*For months
January,March,May,July,August,October,December last day is
31st*/
if (month == 1
|| month == 3 || month == 5 || month == 7 || month == 8 || month ==
10 || month == 12) {
return 31;
}
/*For months
September,April,June,November last day is 30th*/
else if (month
== 9 || month == 4 || month == 6 || month == 11) {
return 30;
}
/*For month
February we need to check if its a leap year or not*/
else {
/*checks for year being a leap year or not and
return 29 or 28 accordingly*/
if (isLeapYear(year)) {
return 29;
}
return 28;
}
}
/*returns 0 for invalid month
entered*/
return 0;
}
/*returns true if its a leap year and false if
not*/
private static boolean isLeapYear(int year) {
/*a leap year should be either be
divisible by 400 or divisible by 4 and not divisible by 100*/
if (((year % 4 == 0) &&
(year % 100 != 0)) || (year % 400 == 0)) {
return
true;//returns true for a leap year
}
return false;//returns false if not
leap year
}
}
screenshot of the code:
output when you run above code(multiple runs):
to format your code Ctrl+a then Ctrl+Shift+f(for eclipse IDE)