0

Hi the following method does not quite work:

public static int dayInYear(int day, int month, int year) {
// Returns the number (1.. 366) of the day on which day/month/year falls
int dayNumber = (month - 1) * 31 + day;
if (month > 2) {
  dayNumber = dayNumber - ((4 * month + 23) / 10);
if (isLeapYear(year)) dayNumber++;
}
 return dayNumber;
} // dayInYear

with

public static boolean isLeapYear(int year)
{
return year % 400 == 0 || (year % 4 == 0 && year % 100 != 0);
}

When I put it into a program it compiles but does not give the right answer I did desk check and the problem is in the dayInYear method

For example:February 5 2009 should yield 36 but I get 122

Any insight to correct method is appreciated...

user1560265
  • 57
  • 1
  • 2
  • 8
  • 1
    Well i just ran through it by hand... and it works... I dont understand what you are passing into the method. it should be.. `dayInYear(5, 2, 2009);` which would return 36 because it would only do the first statement – 3kings Sep 10 '16 at 01:28
  • Now built into Java: `LocalDate::getDayOfYear`. See [the Answer by Hoetger](http://stackoverflow.com/a/39421701/642706). – Basil Bourque Sep 10 '16 at 03:54

2 Answers2

2

java.time

It looks like you're trying to roll your own day-of-year calculator. But if you'd like to leverage the hard work of the developers who came before you, you can use the java.time class LocalDate to do this calculation in a simple one-liner.

public static int dayInYear(int day, int month, int year) {
    return LocalDate.of(year, month, day).getDayOfYear();
}

These java.time classes are built into Java 8 and later, see Oracle Tutorial.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
Jason Hoetger
  • 7,543
  • 2
  • 16
  • 18
  • Indeed, a silly waste of time to roll your own date-time classes now that we have the [java.time](https://docs.oracle.com/javase/8/docs/api/java/time/package-summary.html) classes. These classes supplant the troublesome old date-time classes. Much of the java.time functionality is back-ported to Java 6 & 7 in [ThreeTen-Backport](http://www.threeten.org/threetenbp/) and further adapted to [Android](https://en.wikipedia.org/wiki/Android_(operating_system)) in [ThreeTenABP](https://github.com/JakeWharton/ThreeTenABP) (see [*How to use…*](http://stackoverflow.com/q/38922754/642706)). – Basil Bourque Sep 10 '16 at 03:50
1

Let me run your code:

public static void main(String[] args) {
    System.out.println(dayInYear(5, 2, 2009));
    System.out.println(dayInYear(2, 5, 2009));
}

Output:

36
122

You say you get 122, so you're invoking it as the second line in the above main method: dayInYear(2, 5, 2009)

That corresponds to the date "2 May 2009" - since the order of the arguments in your own method dayInYear is: day, month, year.

You say you want the date "February 5 2009". In that case you should be passing it as dayInYear(5, 2, 2009) (the first line in the above main method). Then the result is 36, as you expected.

So there's nothing wrong with your code, however, you need to invoke it correctly.

Erwin Bolwidt
  • 30,799
  • 15
  • 56
  • 79