-6

In the below Java class, I need to write the logic of converting an int to Date as explained below. And this program is not related to adding number of days to current date.

If endDay value is 1, then date should print as Jan 1st 2020.
If endDay value is 28, then date should print as Jan 28th 2020.
If endDay value is 35, then date should print as Feb 4th 2020.
If endDay value is 60, then date should print as Feb 29th 2020.
If endDay value is 70, then date should print as March 10th 2020.

Note: value of endDay (1) always starts from January 1st of every year.

import java.util.Date;

public class TestDate
{
    public void startEndDate(int endDay)
    {
         Date date=new Date();             
         //logic to print the date here
         System.out.println("For the given end day of "+endDay+" the date returned is : "+date);
    }

    public static void main(String[] args)
    {
       startEndDate(35);
       startEndDate(49);
       startEndDate(70);
    }
}

Can any one suggest me the ideas on how to write the logic for above one ?

Kamal
  • 155
  • 2
  • 2
  • 11
  • 6
    Start by not using the obsolete Date class. Use java.time.LocalDate. Read its javadoc. It's pretty straightforward. https://docs.oracle.com/javase/8/docs/api/java/time/LocalDate.html – JB Nizet Dec 13 '19 at 07:23
  • 2
    Have a look at the JavaDocs for the Calendar class - https://docs.oracle.com/javase/8/docs/api/java/util/Calendar.html#DAY_OF_YEAR or for the LocalDate class - https://docs.oracle.com/javase/8/docs/api/java/time/LocalDate.html#getDayOfYear--. – Riaan Nel Dec 13 '19 at 07:24
  • 2
    `LocalDate.of(2019, 12, 31).plusDays(endDay).format(DateTimeFormatter.ofPattern("MMMM d uuuu"))` – Andreas Dec 13 '19 at 07:27
  • @Andreas why don't you post it as an answer instead of a comment? – Abra Dec 13 '19 at 07:31
  • @Abra Because the answer is in the related question linked to at the top. That question is about adding days to current date, but that's not different from adding days to a particular date, which is what this question wants, so this question is still a duplicate. I just added the comment to show example for how to do it with fixed date and to get the right format. – Andreas Dec 13 '19 at 16:44

1 Answers1

1

java.time and Year.atDay()

public static void startEndDate(int endDay)
{
    LocalDate date = Year.of(2020).atDay(endDay);
    System.out.println("For the given end day of " + endDay
                        + " the date returned is : " + date);
}

Let’s try it out:

    startEndDate(35);
    startEndDate(49);
    startEndDate(70);

Output is:

For the given end day of 35 the date returned is : 2020-02-04
For the given end day of 49 the date returned is : 2020-02-18
For the given end day of 70 the date returned is : 2020-03-10

The documentation of Year.atDay() explains:

Combines this year with a day-of-year to create a LocalDate.

Please fill in your desired year where I put 2020.

I recommend you don’t use Date. That class is poorly designed and long outdated. Instead I am using Year and LocalDate, both from java.time, the modern Java date and time API.

A comment suggested regarding your question as a question of adding a number of days to December 31 of the previous year. IMO regarding it as finding a date from the number of the day-of-year gives a somewhat more elegant solution.

Links

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
Ole V.V.
  • 81,772
  • 15
  • 137
  • 161