# Determine the Date by the Day Number in Java

## The challenge

What date corresponds to the `n`th day of the year?
The answer depends on whether the year is a leap year or not.

Write a function that will help you determine the date if you know the number of the day in the year, as well as whether the year is a leap year or not.
The function accepts the day number and a boolean value `isLeap` as arguments, and returns the corresponding date of the year as a string `"Month, day"`.
Only valid combinations of a day number and `isLeap` will be tested.

#### Examples:

``````//  41st day of non-leap year is February, 10
getDay(41, false)   =>  "February, 10"

//  60th day of non-leap year is March, 1
getDay(60, false)   =>  "March, 1"

//  60th day of leap year is February, 29
getDay(60, true)    =>  "February, 29"

//  365th day of non-leap year is December, 31
getDay(365, false)  =>  "December, 31"

//  366th day of leap year is December, 31
getDay(366, true)   =>  "December, 31"
``````

## The solution in Java code

Option 1:

``````import java.time.LocalDate;
import java.time.DateTimeException;
import java.time.format.DateTimeFormatter;

public class Solution {

private static final DateTimeFormatter format = DateTimeFormatter.ofPattern("LLLL, d");

public static String getDay(int day, boolean isLeap) {
return format.format(LocalDate.ofYearDay(isLeap ? 2000 : 1999, day));
}

}
``````

Option 2:

``````public class Solution {

public static String getDay(int day, boolean isLeap) {
int[] days = {31, isLeap ? 29 : 28, 31,30,31,30,31,31,30,31,30,31};
String[] months = {"January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December"};
int i;
for(i=0; i<12; i++){
if(day <= days[i])
break;
day -= days[i];
}
return months[i] + ", " + day;
}

}
``````

Option 3:

``````import static java.time.LocalDate.ofYearDay;
import static java.time.format.DateTimeFormatter.ofPattern;

interface Solution {

static String getDay(int day, boolean isLeap) {
return ofPattern("LLLL, d").format(ofYearDay(isLeap ? 2020 : 2021, day));
}

}
``````

## Test cases to validate our solution

``````import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;

class SolutionTest {
@Test
void basicTest() {
assertEquals("January, 15", Solution.getDay(15, false));
assertEquals("February, 10", Solution.getDay(41, false));
assertEquals("February, 28", Solution.getDay(59, false));
assertEquals("March, 1", Solution.getDay(60, false));
assertEquals("February, 29", Solution.getDay(60, true));
assertEquals("December, 31", Solution.getDay(365, false));
assertEquals("December, 31", Solution.getDay(366, true));
}
}
``````