-3

I have written a code that adds and subtracts days from dates, but it is not working effectively. I am having a hard time making my addition and subtraction of dates also account for leap years. How can I make my addition and subtraction effective?

Addition:

public void additionOfDays(int days) {
    year += days / 365;
    days %= 365;
    month += days / 30;
    days %= 30;
    day += days;
    if (isLeapYear() && month == 2 && day > 28) {
        day -= 28;
        month++;
    } else if (ODD_MONTHS.contains(month) && day > 31) {
        day -= 31;
        month++;
    } else if (day > 30) {
        day -= 30;
        month++;
    }
}

Subtraction:

public void subtractionOfDays(int days) {
        year -= days / 365;
        days %= 365;
        month -= days / 30;
        days %= 30;
        day -= days;
        if (isLeapYear() && month == 2 && day > 28) {
            day = 28;
            month--;
        } else if (ODD_MONTHS.contains(month) && day > 31) {
            day += 31;
            month--;
        } else if (day > 30) {
            day += 30;
            month--;
        }

Month Names and Odd Months:

public String[] MONTH_NAMES = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
    public List<Integer> ODD_MONTHS = Arrays.asList(1, 3, 5, 7, 8, 10, 12); 
  • 2
    What do you mean "effective"? – azurefrog Feb 24 '20 at 19:58
  • 3
    Have you read abot the java.time API?https://docs.oracle.com/javase/8/docs/api/java/time/package-summary.html – Jens Feb 24 '20 at 20:01
  • 1
    ^ what @Jens said: [Introduction to the Java 8 Date/Time API](https://www.baeldung.com/java-8-date-time-intro) – hooknc Feb 24 '20 at 20:01
  • Right now, my code is not accounting for leap years. How can I solve it/fix it? – John Ferrier Feb 24 '20 at 20:02
  • You can either use a standard date API, or look up the rules for handling leap years and add the code to implement them. P.S. this is not as simple as you think, especially if you have to worry about locale and historical dates. – azurefrog Feb 24 '20 at 20:05
  • I am not allowed to use API standard date or LocalDate. The code that I have written is as "manual" as it can get. – John Ferrier Feb 24 '20 at 20:09
  • Another tack you might consider is simply storing the actual day as simply a day (e.g. a julian day or some such), and then writing one general purpose day-to-calendar-date converter. Then addition and subtraction become trivial, and you only have to put the difficult logic in one place. – azurefrog Feb 24 '20 at 20:17
  • 1
    You initial premise that `month += days / 30` is so flawed you need to **throw away** that code, take a step back and **rethink** what you're doing, because a **month is not 30 days** long. E.g. if `days` is more than 90, then you're guaranteed to cross at least 2 months with 31 days, so that calculation can never be correct. – Andreas Feb 24 '20 at 20:57
  • I am really struggling with this. I know why my code is failing and I know that a month is not always 30 days, that is why I am asking for a bit of help. Some code to get me started. Thank you! – John Ferrier Feb 25 '20 at 01:05
  • possible duplicate of https://stackoverflow.com/questions/11882926/how-to-subtract-x-day-from-a-date-object-in-java – Pranjali Feb 25 '20 at 05:18

1 Answers1

0

check out the add methond in Calender Class https://docs.oracle.com/javase/7/docs/api/java/util/Calendar.html

Edit:

The LocalDate Class is a better option as the comments point out https://docs.oracle.com/javase/8/docs/api/java/time/LocalDate.html

MT756
  • 629
  • 3
  • 10
  • 2
    It's no longer recommended to use the Calendar class. It's better to use the [LocalDate](https://docs.oracle.com/javase/8/docs/api/java/time/LocalDate.html) class instead. – Tim Hunter Feb 24 '20 at 20:02
  • 1
    Do not suggest the old calebdar API – Jens Feb 24 '20 at 20:02
  • @TimHunter got it – MT756 Feb 24 '20 at 20:03
  • I am not allowed to use API standard date or LocalDate. The code that I have written is as "manual" as it can get. – John Ferrier Feb 24 '20 at 20:09
  • @JohnFerrier in that case, you should check if the year is a leap year in the beginning of your function, then you add the years. For month, you shouldn't just add days/30 since most months don't have 30 days. – MT756 Feb 24 '20 at 20:16
  • @JohnFerrier I suggest you first convert the date to the day of the year, then perform the addition/subtraction of days (considering if leap years are included in the process). Last you convert the resulting day of the year to a date. – MT756 Feb 24 '20 at 20:20