java程序的日期验证问题

时间:2016-09-21 22:17:38

标签: java date

我一直致力于一个程序,告诉我这个格式的日期是否有效" mm / dd / yyyy"

但我的节目无论什么说每个日期都有效。 有人可以帮助我,告诉我我的问题在哪里,如果是这样解释什么是错的。必须与闰年有关。并且必须提供一条错误消息,我认为我可以弄清楚。

这是我的代码所以请帮助我。我是初学者,所以感谢你的批评

 /**
 * Created by MacOSX on 9/18/2016.
 */
import java.util.Scanner;

public class ASS4ID1773 {

    public static void main(String[] args) {
    }

    {

        System.out.println("Enter any date in ''mm/dd/yyyy'' format. ");
        String date = "";
        Scanner keyboard = new Scanner(System.in);

        int mm = 00;
        int dd = 00;
        int yyyy = 0000;
        date = keyboard.nextLine();
        boolean LeapYear;
        mm = 0;
        dd = 0;
        yyyy = 0;
        LeapYear = false;
        if (yyyy % 4 == 0 && (!(yyyy % 100 == 0) || yyyy % 400 == 0)) {
            LeapYear = true;
        }

        /**
         * month restrictions.
         */
        if ((mm < 12) & (mm > 1)) {
            System.out.println("You have entered an invalid month. Please try entering a month that exists.");
        }

        /**
         * Day Restriction
         */
        if ((dd > 31 && dd < 1)) {
            System.out.println("You have entered an invalid day. Please try entering a day that exists.");
        }

        /**
         * Months with 31 days
         */
        if ((mm == 9 & mm == 4 & mm == 6 & mm == 11) & !(dd == 31)) {
            System.out.println("For the month you have entered, you have entered an incorrect day.");
        }

        /**
         * February month
         */
        if ((mm == 2 && !(dd < 29)) && LeapYear == false) {
            System.out.println("You have entered a day that does not exist in the month of February.");
        }

        /**
         * Leap Year for February but with incorrect day.
         */
        if ((mm == 2 & (dd < 30)) & LeapYear == true) {
            System.out.println("You have entered an invalid day for the month of February.");
        } else {
            System.out.println("You have entered a valid date in the correct format.");
        }

        /**
         * Leap Year for February but with correct day.
         */
        if (LeapYear) {
            if ((mm == 2 & (dd == 29)) & LeapYear == true) {
                System.out.println(date + " is a valid date.");
            }
        } else {
            System.out.println(date + "is not valid month must have 29 days or less.");
        }
        if ((mm == 2) && (dd <= 28)) {
            System.out.println(date + "  is a valid date.");
        }

    }
}

3 个答案:

答案 0 :(得分:0)

根据我上面的评论:正如您的程序目前一样,您在date中阅读,但您没有对此做任何事情。

此外,您是否有意尝试手动解析日期?做

这样的事情是不够的
try {
    DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT);
    Date date = df.parse(dateString);
} catch (ParseException e) {
    // incorrect date
}

编辑(参见评论部分):

try {
    SimpleDateFormat df = new SimpleDateFormat("MM/dd/yyyy");
    Date date = df.parse(dateString);
} catch (...) {
    ...
}

答案 1 :(得分:0)

您的值永远不会设置为您要求的用户输入。您需要从用户那里获取输入,然后将其解析成片段,然后您可以调用Integer.parseInt on将String表示形式转换为Integer。

尝试一下:

//check to see if the input recieved is ##/##/####
if(date.matches("\\d\\d/\\d\\d/\\d\\d\\d\\d"))
{
    //now split the string around / to get the 
    //numbers you need for your checks
    String[] pieces = date.split("/");
    mm = Integer.parseInt(pieces[0]);
    dd = Integer.parseInt(pieces[1]);
    yyyy = Integer.parseInt(pieces[2]);
    //now do your logic checks based on the numbers that the user typed

}
//the user did not enter the correct format of input
else
{
    System.out.println("Sorry Incorrect Input Format")
}

答案 2 :(得分:0)

Scanner keyboard = new Scanner(System.in).useDelimiter("/");

int mm = keyboard.nextInt();
int dd = keyboard.nextInt();
int yyyy = keyboard.nextInt();

// etc