-7

How can i find the day of the month using the day of the year?

For example day 16 is January 16. Day 35 is February 4

My program prompts for a day in the year (1-365) and year.

So input can be day 252 and year 2017

I already initialized a boolean for leap Years.

I already divided this into 12 months (Jan-Dec. EX: if the day input is 1-31 its January) using an if statement.

if(day >=1 && day <=31)
{
   month = 1;
   monthName = "January";
}

My program is almost done, this is my last step but I have no idea how to go about doing this.

How can I find the day in a month using day in a year WITHOUT using an API or imports?.

Survivorr
  • 83
  • 3
  • 10
  • 3
    Without using imports? By sitting down and writing code. Obviously this is **homework** then. And homework ... means: you doing work at home. Not: you dumping your assignment on other people. – GhostCat Mar 01 '17 at 14:41
  • where is your code then? – Youcef LAIDANI Mar 01 '17 at 14:41
  • What about dayNumber/(sumOfDaysInMonthUntilThisMonth) ? – Adonis Mar 01 '17 at 14:42
  • Well if you can't use imports or anything else for that matter. I would just code the days in the month "hard coded" and go from there.. but also don't forget leap years. – 3kings Mar 01 '17 at 14:42
  • Wrongway to address a problem.... is your 60th day Feb 29th or March 1st???? – ΦXocę 웃 Пepeúpa ツ Mar 01 '17 at 14:48
  • @ΦXocę웃Пepeúpaツ I have a boolean for leap year as i mentioned. The day is an input value, I have no control over what it is. – Survivorr Mar 01 '17 at 14:52
  • 2
    So you asked the same question here http://stackoverflow.com/questions/42521669/what-day-of-the-month-is-it, got answers to that and now closed it as a dupe of this question? What is the point of this question here if you already got an answer with your constraints? – Tom Mar 01 '17 at 15:00
  • Rather than mention that you have a boolean show it. Rather than mock someone because you didn't put too much effort into your question, improve it. – Lexi Mar 01 '17 at 15:42

1 Answers1

2

In a very basic approach, create an int array containing the number of days in each month.

final int[] daysInMonth = new int[] { 31, 28, 31, 30, ... };

Then check if it is a leap year, do:

daysInMonth[1]++;  // add an extra day in February

Create an int variable (called remainingDays) that initially will contain your input. And your code should loop through the created array, first checking if the remainingDays - daysInMonth[i] (the numbers of days in the month) is greater than zero. If it is, then subtract the number of days of that month from the remainder of days and go to the next month. if it is not, then your answer is in the remainingDays variable.

(as you asked a walkthrough not the code that answers that)

Boschi
  • 622
  • 3
  • 10