TransWikia.com

a method that converts a daynumber into a date

Stack Overflow Asked by Leenieke19 on January 6, 2021

A number between 1 and 365 is requested from the user. The number represents the day number of the year. The corresponding date is displayed.

I get stuck on the method calculationDateWithDayNumber

An example: the number 105 should be converted to date 15 April. My result is -31 February.
Where does it go wrong?

public class DateOperations {
    
    private final static String[] MONTHS = {"January", "February", "March", "April", "May", "June", "July", "August", 
            "September", "October", "November", "December"  };
    
    private static final int[] NUMBERDAYS = {31,28,31,30,31,30,31,31,30,31,30,31};
    
    public static String calculationDateWithDayNumber(int day)
    {
        int month = 0;
        for (int i=0; day>=NUMBERDAYS[i] ;i++)  
            {day=-NUMBERDAYS[i];
            ++ month;}
             
            String nameMonth = MONTHS[month];

             
            
        String datum = String.format("%d%s",day, nameMonth);
        
        return datum;
    }

}

4 Answers

I guess you are practicing. Two fixes are needed, see below comments in code

day=-NUMBERDAYS[i] means you are assign value of (-1*NUMBERDAYS[i]) in day. But I think you should minus NUMBERDAYS[i] from day like day - NUMBERDAYS[i] and assign the value in day like day = day - NUMBERDAYS[i] or using shorthand operator like day -= NUMBERDAYS[i]

  public static String calculationDateWithDayNumber(int day) {
    int month = 0;
    for (int i = 0; day > NUMBERDAYS[i]; i++) {  // Fix here, remove equals since day can be last day of month
      day = day - NUMBERDAYS[i]; // Fix here, minus the NUMBERDAYS[i] from day
      ++month;
    }
    String nameMonth = MONTHS[month];
    String datum = String.format("%d %s", day, nameMonth);
    return datum;
  }

Output: 15 April

Correct answer by Eklavya - UpvoteDon'tSayThanks on January 6, 2021

I think you need the below code snippet :

public static void main(String[] args) {
  int dayOfYear = 60;
  int year = 2019;
  Calendar calendar = Calendar.getInstance();
  calendar.set(Calendar.YEAR, year);
  calendar.set(Calendar.DAY_OF_YEAR, dayOfYear);
  System.out.println("Day " + dayOfYear + " of the current year = " + calendar.getTime());

}

You need to set the year, to take of the leap year

Answered by Saurabh Jhunjhunwala on January 6, 2021

Since java.time this task can be done without any custom calculation (which wouldn't be trivial at all).

Basically, you can just take the date of today and adjust the day of year preserving the year. Have a look at this example:

public static String getDateFromDayOfYear(int dayOfYear) {
    // take "today" (basically just to have the current year
    LocalDate localDate = LocalDate.now()
                                    // and adjust the day of year using the argument
                                    .withDayOfYear(dayOfYear);
    // then return a String representation of that date in a desired format (GER/DE here)
    return localDate.format(DateTimeFormatter.ofPattern("dd.MM.uuuu"));
}

Using it in a main method like

public static void main(String[] args) {
    int apr14 = 105;
    System.out.println(getDateFromDayOfYear(apr14));
}

would output

14.04.2020

which is obviously not your desired output because your task doesn't consider leap years
(those with 366 days due to a Februray 29th, like in 2020).

Answered by deHaar on January 6, 2021

You can do this very easily with the java.time API, using ofYearDay. Since you said that the number can be from 1 to 365, you are probably assuming a non-leap year, so you can pass any non-leap year as the first parameter:

public static String getMonthDayFromDayOfYear(int dayOfYear) {
    // 2019 is a non-leap year
    LocalDate date = LocalDate.ofYearDay(2019, dayOfYear);

    // "ddMMMM" seems to be your desired format
    return DateTimeFormatter.ofPattern("ddMMMM").withLocale(Locale.US).format(date);
}

Answered by Sweeper on January 6, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP