Mel
Mel

Reputation: 33

How to calculate the remaining days in a year from user input

I have a homework question that reads as such: Write a program that prompts the user to enter a date (in the form of three numbers: year month day), and use the getDays() method to help count the number of days until New Year from the given date. Here are some sample runs:

This is the code I have so far:

public class Lab9Question4 {

public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    System.out.println("Please enter a date: ");
    int year = input.nextInt();
    int month = input.nextInt();
    int days = input.nextInt();
    input.close();
    long diff = getDays(year, month) - days;
    long days_diff = diff + 1;                                              
    System.out.println("There are " + days_diff + " day(s) until New Year.");
}

public static int getDays(int year, int month) {
    int number_Of_DaysInMonth = 0;
    
    switch (month) {
    case 1:
        number_Of_DaysInMonth = 31;
        break;
    case 2:
        if ((year % 400 == 0) || ((year % 4 == 0) && (year % 100 != 0))) {
            number_Of_DaysInMonth = 29;
        } else {
            number_Of_DaysInMonth = 28;
        }
        break;
    case 3:
        number_Of_DaysInMonth = 31;
        break;
    case 4:
        number_Of_DaysInMonth = 30;
        break;
    case 5:
        number_Of_DaysInMonth = 31;
        break;
    case 6:
        number_Of_DaysInMonth = 30;
        break;
    case 7:
        number_Of_DaysInMonth = 31;
        break;
    case 8:
        number_Of_DaysInMonth = 31;
        break;
    case 9:
        number_Of_DaysInMonth = 30;
        break;
    case 10:
        number_Of_DaysInMonth = 31;
        break;
    case 11:
        number_Of_DaysInMonth = 30;
        break;
    case 12:
        number_Of_DaysInMonth = 31;

    }
    return number_Of_DaysInMonth;
    }
}

I understand I need to use a loop somewhere in order to get the remaining amount of days after the user input and add them to the difference of dates to get the days until the new year, but I am not sure where to insert the loop and what should be inside it. Any help is appreciated :)

UPDATE: This code works perfectly now thanks to everyone who answered super quickly! New code is below for anyone else who might need it in the future:

public class Lab9Question4 {

public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    System.out.println("Please enter a date: ");
    int year = input.nextInt();
    int month = input.nextInt();
    int days = input.nextInt();
    input.close();
    int remainingDays = getDays(month, year) - days + 1;
    for (int currentMonth = month; currentMonth <= 12; currentMonth++) {
        remainingDays += getDays(year, currentMonth);
    }

    System.out.println("There are " + remainingDays + " day(s) until New Year.");
}

public static int getDays(int year, int month) {
    int number_Of_DaysInMonth = 0;

    switch (month) {
    case 1:
        number_Of_DaysInMonth = 31;
        break;
    case 2:
        if ((year % 400 == 0) || ((year % 4 == 0) && (year % 100 != 0))) {
            number_Of_DaysInMonth = 29;
        } else {
            number_Of_DaysInMonth = 28;
        }
        break;
    case 3:
        number_Of_DaysInMonth = 31;
        break;
    case 4:
        number_Of_DaysInMonth = 30;
        break;
    case 5:
        number_Of_DaysInMonth = 31;
        break;
    case 6:
        number_Of_DaysInMonth = 30;
        break;
    case 7:
        number_Of_DaysInMonth = 31;
        break;
    case 8:
        number_Of_DaysInMonth = 31;
        break;
    case 9:
        number_Of_DaysInMonth = 30;
        break;
    case 10:
        number_Of_DaysInMonth = 31;
        break;
    case 11:
        number_Of_DaysInMonth = 30;
        break;
    case 12:
        number_Of_DaysInMonth = 31;

    }
    return number_Of_DaysInMonth;
}

}

Upvotes: 1

Views: 1713

Answers (5)

AlirezaAsadi
AlirezaAsadi

Reputation: 803

Your question can be a duplicate of "How to calculate the number of days in a period?" here: How to calculate the number of days in a period? You can use Period.between(date, newYearDate) and convert it to days or ChronoUnit.DAYS.between(date, newYearDate) to acheive what you want.

Upvotes: 1

forpas
forpas

Reputation: 164194

I think it's easier by using LocalDate and ChronoUnit.DAYS.between()

LocalDate date = LocalDate.of(year, month, days);
LocalDate newYearsDay = LocalDate.of(year + 1, 1,  1);
long dif = ChronoUnit.DAYS.between(date, newYearsDay);

System.out.println("There are " + dif + " day(s) until New Year.");

Upvotes: 1

ᴇʟᴇvᴀтᴇ
ᴇʟᴇvᴀтᴇ

Reputation: 12781

I suggest you pull out well-named helper methods for each part of the problem. It will make your code easier to understand and work with.

For example:

public static boolean isLeapYear(int year) {
    return (year % 400 == 0) || (year % 4 == 0 && year % 100 != 0);
}

public static int daysInMonth(int year, int monthNum) {
    int[] days = {9999, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    if (monthNum == 2 && isLeapYear(year)) {
        return 29;
    }
    return days[monthNum];
}

By the way, the reason I called the parameter monthNum instead of month was to try to make it clearer that it's not zero-based, with January = 0 etc. I also put 9999 in the array for element 0 to ensure that if we do make this error it will stand out in the calculations.

For the loop you need to go from the specified month to the end of the year for (int m = monthNum; m <= 12; m++) { ... }, keeping the sum of the number of days in each of those months and then subtract the specified day of the current month.

Upvotes: 0

NiVeR
NiVeR

Reputation: 9806

You should use date aware classes for this purpose, but just for fun and more importantly for educational purposes, you can probably do it in the way you started like this:

int remainingDays = getDays(month, year) - days + 1;
for(int currentMonth = month + 1; currentMonth <= 12; currentMonth++) {
   remainingDays += getDays(year, currentMonth);
}

Upvotes: 0

LunaticJape
LunaticJape

Reputation: 1584

I recommend you to check the documentation of LocalDate class.

The .until() method should work well in your case. You do not have to calculate manually.

Upvotes: 2

Related Questions