By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,246 Members | 1,747 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,246 IT Pros & Developers. It's quick & easy.

validating a date (with leap year)

P: 1
i am new to java and programming all together. any help would be appreciated.

i need to get it to input the date as so: mm/dd/yyyy and then output the date as so: February 29, 2004 is a date in a leap year. and if the date was before the year 1582 it leaves the date as it was entered: mm/dd/yyyy

i know that i need to use a switch statement. but i am lost right now. im not sure if i have the variables set right or if i even need the variables like that.

and if there is an easier way to post code please tell me how as well :D


package project_2app;

import java.util.*;

public class ValidateDates
{

public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
System.out.print("Enter a date: ");
int year = input.nextInt();

//Check if the year is a leap year
boolean leapYear =
(year % 4 == 0 && year % 100 != 0) || (year % 400 == 0) && !(year <= 1582);

int jan = 1;
int feb = 2;
int mar = 3;
int april = 4;
int may = 5;
int june = 6;
int july = 7;
int aug = 8;
int sept = 9;
int oct = 10;
int nov = 11;
int dec = 12;
int month =



switch (month)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
break;
case 4:
case 6:
case 9:
case 11:
case 2:
break;
default:
System.out.println("Invalid Month");
break;
}


//Display results
System.out.println();

}

}
Feb 22 '10 #1
Share this Question
Share on Google+
1 Reply


ThatThatGuy
Expert 100+
P: 449
Writing such long codes for a simple is invariable...


you could just check this

Expand|Select|Wrap|Line Numbers
  1. if(year%4==0&&year>=1582)
that's for you to check for a leap year......

rest of what are you doing is going out of my head.............
Feb 23 '10 #2

Post your reply

Sign in to post your reply or Sign up for a free account.