-1

I have a program that converts dates from numerical month/day/year format to normal format (e.g. December 31, 2010).

I don't want to import anything, or use a date object to go about what I'm doing.

Before moving on to finding the max number of days by year, I'm trying to do it by month. I'm trying to match up the month with the number of days (omitting Feb since it depends on if it's Leap Year).

This isn't working for me. I think it's because of the format.

     try {
        if (m == 1 && (d > 31 || d < 1)) {         // jan
        } else if (m == 3 && (d > 31 || d < 1)) {  // mar
        } else if (m == 4 && (d > 30 || d < 1)) {  // apr
        } else if (m == 5 && (d > 31 || d < 1)) {  // may
        } else if (m == 6 && (d > 30 || d < 1)) {  // june
        } else if (m == 7 && (d > 31 || d < 1)) {  // july
        } else if (m == 8 && (d > 31 || d < 1)) {  // aug
        } else if (m == 9 && (d > 30 || d < 1)) {  // sept
        } else if (m == 10 && (d > 30 || d < 1)) { // oct
        } else if (m == 11 && (d > 30 || d < 1)) { // nov
        } else if (m == 12 && (d > 31 || d < 1))   // dec
            throw new DayException(day);
    } catch (Exception e) {
        throw new DayException(day);
    }

Is there an easier way to validate the day?

l33tspeak
  • 95
  • 3
  • 13
  • `if (m == 1 && (d > 31 || d < 1)) { // jan` - There is no 0 day or 32 day in January. You want to do `d <= 31 && d > 1`. – Maroun Feb 19 '14 at 08:13
  • 1
    That's really not how `if` and `else if` work. – Dawood ibn Kareem Feb 19 '14 at 08:13
  • 2
    *Why* do you not want to use other libraries? Is this just an academic exercise? I'd also *strongly* advise you to revisit your exception handling and general structure... and *please* take more time to format your questions in future. See http://tinyurl.com/so-list – Jon Skeet Feb 19 '14 at 08:17
  • This would be improved by using `"or"` ( || ) on your conditions rather than building an `else if` ladder – DaveH Feb 19 '14 at 08:17
  • The formatting is hiding whatever errors there are. Since this is, I assume, just for practice (you should import and use standard libraries in preference to re-inventing the wheel), I suggest this is a good place to learn about the `switch` statement, which will be physically easier to read. – Andrew Lazarus Feb 19 '14 at 08:18
  • It's simply because my professors don't want us to. They want us to employ these complicated if/else functions and whatnot. I know what I have is really inefficient! – l33tspeak Feb 19 '14 at 08:23
  • 1
    I think this is a classic case case for polymorphism - use an `enum` of months and implement custom error checking logic in each one. You could even return a `Predicate` or some such - this would allow you to reuse the code for months with 31 and months with 30 days. [DRY](http://en.wikipedia.org/wiki/Don%27t_repeat_yourself)! – Boris the Spider Feb 19 '14 at 08:30
  • 1
    @BoristheSpider You say that with such a straight face! – Dawood ibn Kareem Feb 19 '14 at 08:31
  • @DavidWallace copy-paste makes me cry a little... – Boris the Spider Feb 19 '14 at 08:35
  • and there was me thinking that `February` should be an abstract class, so you can have `LeapYearFebruary extends February` and `StandardFebruary extends February`. – Dawood ibn Kareem Feb 19 '14 at 08:36
  • @BoristheSpider I chose to add that in myself, but yes, we are learning polymorphism. Any tips would be appreciated, as I am taking far too many programming classes! JQuery, Javascript/HTML/CSS and Assembly on top of Java! – l33tspeak Feb 19 '14 at 08:38
  • Ah, yes, JQuery and Assembly. They must complement each other nicely! :-) :-) `$.native(#mov 0100)` and all that! – Dawood ibn Kareem Feb 19 '14 at 08:39
  • 1
    Thank you for being understanding of my noobiness. Stack Overflow is the best! – l33tspeak Feb 19 '14 at 08:42
  • @DavidWallace you can _still_ have `LeapYearFebuaryPredicate extends FebuaryPredicate` and `StandardFebuaryPredicate extends FebuaryPredicate` whilst `FebuaryPredicate implemnents Predicate`... I'm sure we can over-engineer this until it makes toast too, up for a challenge? – Boris the Spider Feb 19 '14 at 08:57
  • And what you mean by that, @BoristheSpider, is "good luck, l33tspeak, with your classes on polymorphism." – Dawood ibn Kareem Feb 19 '14 at 08:59
  • Why not use a built-in class? `input > Month.MARCH.maxLength()` Or use `YearMonth` to test February and leap year properly. See [my Answer](https://stackoverflow.com/a/44668849/642706) to a similar Question. – Basil Bourque Jun 21 '17 at 07:10

2 Answers2

5

Assuming for the moment you don't care about leap years, you could do something like this.

int[] daysInMonth = new int[] { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
if (day < 1 || day > daysInMonth[month]){
    throw new DayException(day);
}
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
Dawood ibn Kareem
  • 77,785
  • 15
  • 98
  • 110
2

It should be if (m == 1 && (d <= 31 && d >= 1)). It is Jan only if the month is 1 and day is between 1 and 31(both inclusive).

Adarsh
  • 3,613
  • 2
  • 21
  • 37
  • `d <= 31 || d >= 1` ? Really? That's true for _all_ integers `d`. – Dawood ibn Kareem Feb 19 '14 at 08:16
  • Despite the fact that your avatar scares me, `||` should be `&&`. – Maroun Feb 19 '14 at 08:16
  • Am I not supposed to put what it should not be? I did the same for the month and it worked like a charm: try { if (m != 1 && m != 2 && m != 3 && m != 4 && m != 5 && m != 6 && m != 7 && m != 8 && m != 9 && m != 10 && m != 11 && m != 12) { throw new MonthException(month); } month = Months.values()[m - 1].toString(); // note the m - 1 } catch (Exception e) { throw new MonthException(month); } – l33tspeak Feb 19 '14 at 08:17
  • @l33tspeak It depends on what exactly it is that you are going to do inside the if and else-if blocks. If the month is 1 and days are between 1 and 31, it is jan. Or if month is 1 and the day is less than 1 or greater than 31, it is not jan. – Adarsh Feb 19 '14 at 08:23
  • Look at December in the original question. OP is after the condition under which she _should_ throw the exception, not the condition under which she _shouldn't_. – Dawood ibn Kareem Feb 19 '14 at 08:25