0

For a homework assignment, I want to calculate the nth number of the day a date is in a year in a Jave program.

So, a user gives a date, and then it tells what number of day this is in a year. So 1st of January 2019, is day 1. I already have a function that gives the number of days in a month. This function also considers leap years. So I only need a function that returns the number of the day in the year. What I thought, I had to do, but it does not work because I can't decrement a month:

    static int dayNumberInYear(int day, Month month, int year)
    {
      while(month!=Month.JANUARY)
      {
        int dayNumberInYear=dayNumberInYear+numberOfDaysInMonth(year,month);
        Month month = month(-1);
      }
    return dayNumberInYear(day,month,year)+day;
    }

I know this is not right, so I hope someone can help me. I think a for-loop is better, but I do not know how. And the first line, static int dayNumberInYear(int day, Month month, int year), I am not allowed to change that, so it needs to be this first line. I am not allowed to use the java JRE date manipulating classes like Calendar, Date etc!

I am a beginner, so please I hope that someone can help me. Here is the code I have so far:

    package ;

    import java.util.Scanner;

    public class Easter
    {
      public static void main(String[] arguments)
      {
        Scanner scanner=new Scanner(System.in);
        System.out.println("Enter the day month and year with spaces in between:");
        int day=scanner.nextInt();
        int monthNumber=scanner.nextInt();
        Month month=Month.month(monthNumber);
        int year=scanner.nextInt();
        System.out.println("The month "+month+" has "+numberOfDaysInMonth(year,month)+" days in year "+year);

        System.out.println("The number of this date in a year:"+dayNumberInYear(day,month,year));

        scanner.close();
      }
      static boolean isLeapYear(int year)
      {
        if(year%100==0)
          year=year/100;
        if(year%4==0)
          return true;
        return false;
      }

      static int numberOfDaysInMonth(int year, Month month)
      {
        switch(month)
        {
          case APRIL:
          case JUNE:
          case SEPTEMBER:
          case NOVEMBER:
            return 30;
          case FEBRUARY:
            if(isLeapYear(year))
              return 29;
            return 28;
          default:
            return 31;
        }
      }

      static int dayNumberInYear(int day, Month month, int year)
      {
        while(month!=Month.JANUARY)
        {
          int dayNumberInYear=dayNumberInYear+numberOfDaysInMonth(year,month);
          Month month = month(-1);
        }
        return dayNumberInYear(day,month,year)+day;
      }
    }

There is already a premade class Month.java: package ;

    public enum Month {
      JANUARY, FEBRUARY, MARCH, APRIL, MAY, JUNE, JULY, AUGUST, SEPTEMBER, OCTOBER, NOVEMBER, DECEMBER;

      public int number()
      {
        return ordinal()+1;
      }

      public static Month month(int number)
      {
        return values()[number-1];
      }
    }
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
mathew
  • 61
  • 7
  • 3
    Looked into `LocalDate.of(year, month, day).getDayOfYear()`? Or must you reinvent the wheel? – ernest_k Sep 27 '19 at 17:23
  • @ernest_k I have added some new information. But I am not allowed to use the Date classes etc. So it has to happen with a for-loop I guess.. – mathew Sep 27 '19 at 17:32
  • you need to find number of the day given a date in full format i.e. dd-mm-yy? – singhV Sep 27 '19 at 17:43
  • 1
    @singhV Yes, so for example the number of the first of January is always 1, and the number of the first of March in a leap year is 61, and it is 60 in non-leap years. But I already know when it is a leap year or not. I just need a for-loop that counts the number of days. I hope I explain it well enough – mathew Sep 27 '19 at 17:47
  • please specify what you are allowed to use so we can answer according to that. – SSP Sep 27 '19 at 18:05
  • Using the ordinal value for your enum is really not good practice. Ordinal relies on, well, order. If the months should get rearranged then things get messed up. Best to pass the month number as an argument to the `enum` constructor for each type. – WJS Sep 27 '19 at 18:54
  • For your purpose the answers below are fine. For production code one would not write the logic and math oneself, but would simply use [`LocalDate.ofYearDay()`](https://docs.oracle.com/javase/10/docs/api/java/time/LocalDate.html#ofYearDay(int,int)). – Ole V.V. Sep 27 '19 at 19:52
  • When asking about a homework assignment, make that clear up front in your Question. – Basil Bourque Sep 27 '19 at 21:44

3 Answers3

1

It looks like the idea you're going for is a recursive approach that decrements by month.

static int dayNumberInYear(int day, Month month, int year)
    {
      if(!month.equals(Month.JANUARY)) {
        return day + dayNumberInYear(numberOfDaysInMonth(month.minus(1), year), month.minus(1), year);
      } else {
        return day;
      }
}

Note that in this recursive approach, the base case is that it's January and we simply return the current day in January. Otherwise we add the day of the current month and then all the days in every prior month.

It could also be done as a for-loop.

static int dayNumberInYearByLoop(int day, Month month, int year) {
  int totalDays = day;
  for (int i = month.getValue();i>1;i--) {
    totalDays += numberOfDaysInMonth(Month.of(i), year);
  }
  return totalDays;
}

You can mess around with my code here: https://repl.it/repls/ConcreteDarkgreenSequence

RaceYouAnytime
  • 709
  • 5
  • 16
  • Yes, I also thought it had to be an int , but for a reason my professor made it Month month. Is it an idea to put the whole code here? – mathew Sep 27 '19 at 18:08
  • @mathew probably your professor wants you to use the Java Enum for Month, see here: https://docs.oracle.com/javase/8/docs/api/java/time/Month.html – RaceYouAnytime Sep 27 '19 at 18:10
  • 1
    @mathew but the idea is the same, just in the places where I have "month-1" you would replace with "month.minus(1)" – RaceYouAnytime Sep 27 '19 at 18:11
  • So something like: for (int i = month.minus(1);i>=1;i--). But I get an error. – mathew Sep 27 '19 at 18:22
  • I have added the Month class, predefined by professor, in my first question.. Plus my whole code, I find this part very hard.. – mathew Sep 27 '19 at 18:24
  • @mathew I will update my answer to reflect using the Month class – RaceYouAnytime Sep 27 '19 at 18:26
  • @mathew try using the static methods in this answer now, I updated them to use the Month class. – RaceYouAnytime Sep 27 '19 at 18:32
  • The code does not recognize .getValue() and .of(1), or the .minus(1), and I do not think I am allowed to change the Month class. – mathew Sep 27 '19 at 18:36
  • Sorry, didn't realize you had your own custom enum. In this case, change "Month.of(1)" to "Month.month(1)" and ".value()" to ".number()" and instead of "month.minus(1)" use "month = Month.month(month.number()-1);" – RaceYouAnytime Sep 27 '19 at 18:40
0
package SO;

import java.time.Month;

public class test {
    //below array contain number of total days in that month(non leap year).
    static int arrTotal[] = { 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 };

    public static void main(String[] args) {

        System.out.println(dayNumberInYear(3, Month.MARCH, 2019));
    }

    static int dayNumberInYear(int day, Month month, int year) {
        int dayNumberInYear = day + numberOfDaysInMonth(year, month);

        return dayNumberInYear;
    }

    private static int numberOfDaysInMonth(int year, Month month) {
        int add = 0;

        if (year % 4 == 0)
            add++;

        int a = month.getValue();
        return arrTotal[(a - 2)] + add;
    }
}
SSP
  • 2,650
  • 5
  • 31
  • 49
  • check this solution. if any issue, comment here. – SSP Sep 27 '19 at 17:54
  • Super thank you! But I am also not allowed to use arrays... Yes I am still in class, so I have to learn these things in the next weeks, so I am not allowed to use it already. But very thank you that you have taken time for it!! I really think it has to be a for-loop, or a while-loop. I also added my function for the number of days in a month in comment above. I hope you can still help me, despite some limitations! – mathew Sep 27 '19 at 18:00
0

As simple as it is.

public static String dayNumberInYear(String sDate) {
        //sDate formate 2023-02-20, you can change local date format
        LocalDate localDate = LocalDate.parse(sDate);
        int dayOfYear = localDate.getDayOfYear();
        return dayOfYear > 99 ? "" + dayOfYear : dayOfYear > 9 ? "0" + dayOfYear : "00" + dayOfYear;

    }