Write a program to accept a date in the format DD/MM/YYYY. Validate the date entered, considering correct number of days in each month and leap year rules.
If the date is valid, perform the following tasks:
a. Display the number of days remaining in that month.
b. Find the sum of all digits of DD, MM and YYYY.
c. If the digit sum is a perfect square, display: SPECIAL DATE otherwise display: REGULAR DATE. If the date entered is invalid, display: INVALID DATE.
Example 1:
INPUT: 15/08/2023
OUTPUT:
DAYS REMAINING: 16
DIGIT SUM: 21
REGULAR DATE
Example 2:
INPUT: 13/03/2000
OUTPUT:
DAYS REMAINING: 18
DIGIT SUM: 9
SPECIAL DATE
Example 3:
INPUT: 30/02/2023
OUTPUT:
INVALID DATE
import java.util.Scanner;
import java.util.StringTokenizer;
class SpecialDate{
public static void main(String args[]){
Scanner in = new Scanner(System.in);
System.out.print("Enter date in DD/MM/YYYY format: ");
String date = in.nextLine();
if(!valid(date)){
System.out.println("INVALID DATE");
return;
}
int d = Integer.parseInt(date.substring(0, 2));
int m = Integer.parseInt(date.substring(3, 5));
int y = Integer.parseInt(date.substring(6));
if(d == 29 && m == 2 && !leap(y)){
System.out.println("INVALID DATE");
return;
}
int days[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
if(leap(y))
days[1] = 29;
if(d > days[m - 1]){
System.out.println("INVALID DATE");
return;
}
int rem = days[m - 1] - d;
System.out.println("DAYS REMAINING: " + rem);
int sum = 0;
int n = Integer.parseInt(d + "" + m + "" + y);
while(n > 0){
sum += n % 10;
n /= 10;
}
System.out.println("DIGIT SUM: " + sum);
double root = Math.sqrt(sum);
if(root == (int)root)
System.out.println("SPECIAL DATE");
else
System.out.println("REGULAR DATE");
}
public static boolean valid(String date){
if(date.length() != 10)
return false;
StringTokenizer st = new StringTokenizer(date, "/");
int count = st.countTokens();
if(count != 3)
return false;
return true;
}
public static boolean leap(int y){
return (y % 400 == 0 || (y % 4 == 0 && y % 100 != 0));
}
}