Had an assigment making a bankaccount, and validating the account owners age, this is just a bit of the code.....

public boolean checkDate(String myDate) // my date should be in the format 20111224 
    {
        int year = Integer.parseInt(myDate.substring(0, 4));// convert the year 2011 to int
        int month = Integer.parseInt(myDate.substring(4,6)); //convert the month 12 to int
        int day = Integer.parseInt(myDate.substring(6)); // convert the day 24 to int

        if (year >=1900 && year < 2012)  //test of the year
        {
            System.out.println("Year is ok");                    

            if (month >= 1 && month <= 12)
            {
                system.out.println("Month is OK");

            }            
            if ((month = 1)&&(day >= 1 && day <=31))
            {
                System.out.println("Day is OK");
            }
            if ((month = 2)&&(day >= 1 && day <=29))
            {
                System.out.println("Day is OK");
            }
            if ((month = 3)&&(day >= 1 && day <=31)) 
            {
                System.out.println("Day is OK");                             
            }
            if ((month = 4)&&(day >= 1 && day <=30))
            {
                System.out.println("Day is OK");
            }
            if ((month = 5)&&(day >= 1 && day <=31))
            {
                System.out.println("Day is OK");
            }
            if ((month = 6)&&(day >= 1 && day <=30))
            {
                System.out.println("Day is OK");
            }
            if ((month = 7)&&(day >= 1 && day <=31))
            {
                System.out.println("Day is OK");
            }
            if ((month = 8)&&(day >= 1 && day <=31))
            {
                System.out.println("Day is OK");
            }
            if ((month = 9)&&(day >= 1 && day <=30))
            {
                System.out.println("Day is OK");
            }
            if ((month = 10)&&(day >= 1 && day <=31))
            {
                System.out.println("Day is OK");
            }
            if ((month = 11)&&(day >= 1 && day <=30))
            {
                System.out.println("Day is OK");
            }
            if ((month = 12)&&(day >= 1 && day <=31))
            {
                System.out.println("Day is OK");
                return true;
            }

Question: there must be a easyer way to validate that month and day match. Examble Febuary 29 or June 30 is true and june 31 and febuary 31 is false. Leape year not countet.
Only been programming for 8weeks, so pls dont yell at me, if i seem like an airhead for not understanding this. It just seem strange that i had to make 12 'ifs' there must be a simple way, an algoritme for this problem.

i was thinking something like:

if (month == 1,3,5,7,8,10,12) && (day >=1 && day<=31); return true;
an so on..... Why not?? remember im a noob

Edited 5 Years Ago by peter_budo: Keep It Clear - Do wrap your programming code blocks within [code] ... [/code] tags

if (month == 1 || month == 3 || ...
or you could do an array of months holding the number of days...
int[] daysInMonth = (31, 29, 31, 30 ...
then look up the number of days using the month number as index.
Either way, you'll have to special-case Feb!

There are many ways some better some worst for checking date validity and you will learn them later.
James already showed two possibilities (second is much better, considering that you are taking month from 1 to 12, where array starts from 0 to size -1. Just keep that in mind)

This article has been dead for over six months. Start a new discussion instead.