Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class Dates
- {
- public static void main(String[] args)
- {
- int month, day, year; //date read in from user
- int daysInMonth; //number of days in month read in
- boolean monthValid, yearValid, dayValid; //true if input from user is valid
- boolean leapYear; //true if user's year is a leap year
- Scanner scan = new Scanner(System.in);
- //Get integer month, day, and year from user
- System.out.println("Enter month");
- month=scan.nextInt();
- System.out.println("Enter day");
- day=scan.nextInt();
- System.out.println("Enter year");
- year=scan.nextInt();
- //Check to see if month is valid
- monthValid=month<13&&month>0;
- //Check to see if year is valid
- yearValid=year>=1000&&year<2000;
- //Determine whether it's a leap year
- leapYear=year%4==0&&year%100!=0||year%400==0;
- //Determine number of days in month
- if(month==1||month==3||month==5||month==7||month==8||month==10||month==12){
- daysInMonth=31;
- }else if(month==4||month==6||month==9||month==11){
- daysInMonth=30;
- }
- else if(!leapYear&&month==2){
- daysInMonth=28;
- }
- else{
- daysInMonth=29;
- }
- //User number of days in month to check to see if day is valid
- dayValid=day<daysInMonth;
- //Determine whether date is valid and print appropriate message
- if(dayValid&&monthValid&&yearValid){
- System.out.println("Valid");
- }else{
- System.out.println("Nope");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement