0

I have to write a program that checks the date of an invoice to make sure that the date is correct. So far, the code is set up so that if the month entered is >= 1 or <= 12 the month is valid. If the month is > 12 or < 1, the month is automatically set to 0. The day is set up similarly, If the day >= 1 or <= 31, the day is valid. If not, the day = 0. What I need to do now is make the limit for days to be dependent on the month, so that a date like 04/31/2015 can't be valid. Furthermore, the day needs to be set to 0 when the month = 0. I was wondering what the most effective way to improve my code so I can make this happen might be? Do I need to implement the Gregorian Calendar in any way? Anyway, here is my code so far:

     if((day <=31) && (day >= 1))
        dayDue = day;
    else
        day = 0;

    if((month >= 1) && (month <= 12))
        monthDue = month;
    else 
        month = 0;  

    if((year >= 2011) && (year <= 2017))
        yearDue = year;
    else
        year = 0;
  • 2
    [Checking validity of date](http://stackoverflow.com/questions/4528047/checking-the-validity-of-a-date) might help – sam Oct 15 '15 at 22:12
  • your comment put me on the right track. I will answer my own question with the code I used to solve this problem, but I'm sure there is an easier way. – Roberson Terry Oct 15 '15 at 22:53

2 Answers2

0

what I used to fix this problem (there might be a simpler way to do this, but this is what I did):

    if((day <= 31) && (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12))
        dayDue = day;
    else if((day > 31) && (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12))
        dayDue = 31;
    else if((day <= 30) && (month == 4 || month == 6 || month == 9 || month == 11))
        dayDue = day;
    else if((day > 30) && (month == 4 || month == 6 || month == 9 || month == 11))
        dayDue = 30;
    else if((day <= 28) && (month ==2))
        dayDue = day;
    else if((day > 28) && (month ==2))
        dayDue = 28;
   //accounting for leap years
    if ((day == 29) && (month == 2) && (year == 2012 || year == 2016))
        dayDue = day;
    else if((day > 29) && (month == 2) && (year == 2012 || year == 2016))
        dayDue = 29;

    if((month >= 1) && (month <= 12))
        monthDue = month;
    else if(month == 0)
        day = 0;
    else 
        month = 0;  
0

Instead of using multiple if, try this.

//02/29/2016 is the next leap year, other are invalid dates
String[] stringDate = {"04/31/2015", "02/29/2016", "06/40/2015"};
SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy");
dateFormat.setLenient(false);
    
for (String str: stringDate) {
    try {
        if(dateFormat.parse(str, new ParsePosition(0)) != null) {
            System.out.println(str + ": valid");
        } else {
            System.out.println(str + ": Invalid date");
        }
    } catch (Exception e) {
      e.printStackTrace();
    }
}

Output:

04/31/2015: Invalid date

02/29/2016: valid

06/40/2015: Invalid date

We are using SimpleDateFormat for parsing and validating date. It uses parsing operation uses the calendar to produce a Date and returns Null if no date is found.

Demo

Community
  • 1
  • 1
sam
  • 2,033
  • 2
  • 10
  • 13