在Java中确定有效日期

时间:2012-02-13 00:48:32

标签: java

我有一个家庭作业,要求用户以(mm / dd / yyyy)格式输入Java中的日期,然后确定输入的日期是否有效。除了二月之外,我每个月都能成功地做到这一点,因为你必须考虑到闰年。

我有这段代码:

import java.util.Scanner;

/**
 * 
 * @author Andrew De Forest 
 * @version v1.0
 * 
 */
public class exc6
{
    public static void main (String[] args)
    {  
        //Initialize a string
        String getInput;
        //Initialize some integers
        int month, day, year;
        //Make a boolean
        boolean validDate;
        //Set the date to false
        validDate = false;
        //Ask for input
        System.out.println("Enter a date (mm/dd/yyyy)");
        //Initialize the scanner
        Scanner keyboard = new Scanner (System.in);
        //Get input & use a delimiter
        keyboard.useDelimiter("[/\n]");
        month = keyboard.nextInt();
        day = keyboard.nextInt();
        year = keyboard.nextInt();

        if((month >= 1 && month <= 12) && (day >= 1 && day <= 31))
        {
            //For months with 30 days
            if((month == 4 || month == 6 || month == 9 || month == 11) && (day <= 30))
            {
                validDate = true;
            }

            //For months with 31 days
            if((month == 1 || month == 2 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) && (day <= 31))
            {
                validDate = true;
            }

            //For February
            if((month == 2) && (day < 30))
            {
                //Boolean for valid leap year
                boolean validLeapYear = false;

                //A leap year is any year that is divisible by 4 but not divisible by 100 unless it is also divisible by 400
                if((year % 400 == 0) || ((year % 4 == 0) && (year %100 !=0)))
                {
                    validLeapYear = true;
                }

                if (validLeapYear == true && day <= 29)
                {
                    validDate = true;
                }

                else if (validLeapYear == false && day <= 28)
                {
                    validDate = true;
                }
            }
        }

        //If the date is valid
        if(validDate == true)
        {
            System.out.println(month + "/" + day + "/" + year + " is a valid date.");
        }

        else
        {
            System.out.println("Invalid date!");
        }

    }
}

我最关心的部分是:

    //For February
    if((month == 2) && (day < 30))
    {
        //Boolean for valid leap year
        boolean validLeapYear = false;

        //A leap year is any year that is divisible by 4 but not divisible by 100 unless it is also divisible by 400
        if((year % 400 == 0) || ((year % 4 == 0) && (year %100 !=0)))
        {
            validLeapYear = true;
        }

        if (validLeapYear == true && day <= 29)
        {
            validDate = true;
        }

        else if (validLeapYear == false && day <= 28)
        {
            validDate = true;
        }
    }
}

据我所知,它看起来是正确的。但是,当我输入类似2011年2月29日的内容时,它会返回一个有效日期(它不应该,因为2011年不是闰年)。为什么是这样?我错过或遗漏了什么导致错误日期返回有效?

3 个答案:

答案 0 :(得分:4)

if((month == 1 || month == 2 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) && (day <= 31))

这条线已经捕获二月。

答案 1 :(得分:2)

DateFormat dateFormat = new SimpleDateFormat("yyyy-MMM-dd");
dateFormat.setLenient(false);
String dateAsString = "2011-Feb-29";
Date date = dateFormat.parse(dateAsString);  // throws an exception; invalid date

答案 2 :(得分:0)

首先,您将validDate设置为true,因为month为2。

接下来,您将validLeapYear设置为false,因为它不是闰年。

(validLeapYear == true && day <= 29)不是真的。

(validLeapYear == false && day <= 28)也不是真的。

因此,validDate仍然是正确的。