validating a date (with leap year)

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ejrtree15
    New Member
    • Feb 2010
    • 1

    validating a date (with leap year)

    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.prin t("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.prin tln("Invalid Month");
    break;
    }


    //Display results
    System.out.prin tln();

    }

    }
  • ThatThatGuy
    Recognized Expert Contributor
    • Jul 2009
    • 453

    #2
    Writing such long codes for a simple is invariable...


    you could just check this

    Code:
    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........... ..

    Comment

    Working...