Prashnikaप्रश्निका
Code
About
‹ All Computer Science notes

ISC Practicals and Theory - Date Plus N Days (OOP)

Updated 21 Oct 2026

Adding N days to a date means counting forward from it, moving into the next month and the next year whenever the current one ends. This program reads a date and N and prints the new date as dd-mm-yyyy, or an error message for a bad date or N. This is the OOP version: the date and N go to a parameterised constructor, and the one-job methods isLeap(), yearLength(), monthLength(), isValid(), isNValid(), dayNumber(), addDays() and two() return values, while main reads the input and creates the object.

Definition

The method works with the day number of the date (its position in the year, 1 for 1 January). Add N to it; while the result is more than the length of the year, subtract that length and go to the next year. The remaining number lies inside the final year and is turned back into a month and day by subtracting whole months.

A year is a leap year if it is divisible by 4 and not by 100, or if it is divisible by 400. A leap year has 366 days and 29 days in February; other years have 365 and 28.

For 25 December 2023 and $N = 10$: $359 + 10 = 369$, and $369 - 365 = 4$ in the year 2024, which is 4 January 2024, so the answer is 04-01-2024.

Algorithm

  1. main reads d, m, y and num with a Scanner and creates ob = new DatePlusN(d, m, y, num). The constructor copies them into dd, mm, yyyy and n and fills the array days with 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 (index 0 is January).
  2. isLeap(y) returns true if y is divisible by 4 and not by 100, or divisible by 400. yearLength(y) returns 366 or 365 from it. monthLength(m, y) returns days[m - 1], but 29 for February of a leap year.
  3. main tests ob.isValid() (year 1 to 9999, month 1 to 12, day 1 to monthLength(mm, yyyy)) and prints INVALID DATE if it is false. Otherwise it tests ob.isNValid() (n from 0 to 1000) and prints INVALID NUMBER OF DAYS if false.
  4. Otherwise main calls ob.addDays():
    • doy is dayNumber() + n, where dayNumber() adds dd and the monthLength(i, yyyy) of each month before mm;
    • while doy is more than yearLength(yyyy), that length is subtracted and yyyy increases by 1;
    • if yyyy is above 9999 the method returns false and main prints RESULT IS BEYOND YEAR 9999;
    • mm is set to 1 and, while doy is more than monthLength(mm, yyyy), that length is subtracted and mm increases by 1;
    • dd is set to doy and the method returns true.
  5. main prints the new date using two(), which returns a number as text with a leading 0 if it is below 10.

Program

import java.util.Scanner;

/**
 * class DatePlusN (OOP version)
 * Stores a date and a number of days N given to the constructor and finds the date N days later,
 * using one-job methods that return values.
 */
public class DatePlusN
{
    int dd, mm, yyyy;      // day, month and year of the date (changed to the answer by addDays())
    int n;                 // number of days to add
    int days[];            // days[0..11] = number of days in January..December (February = 28 here)

    /**
     * Parameterised constructor.
     * @param d day, @param m month, @param y year, @param num number of days to add
     */
    DatePlusN(int d, int m, int y, int num)
    {
        dd = d;
        mm = m;
        yyyy = y;
        n = num;
        days = new int[]{31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    }

    /**
     * Leap year test: divisible by 4 and not by 100, or divisible by 400.
     * @param y the year
     * @return true if y is a leap year
     */
    boolean isLeap(int y)
    {
        return (y % 4 == 0 && y % 100 != 0) || (y % 400 == 0);
    }

    /**
     * Number of days in a year.
     * @param y the year
     * @return 366 for a leap year, otherwise 365
     */
    int yearLength(int y)
    {
        if (isLeap(y))
            return 366;
        return 365;
    }

    /**
     * Number of days in a month of a given year.
     * @param m month (1 to 12), @param y year
     * @return days[m - 1], but 29 for February of a leap year
     */
    int monthLength(int m, int y)
    {
        if (m == 2 && isLeap(y))
            return 29;
        return days[m - 1];
    }

    /**
     * Checks the date stored in the object.
     * @return true if year is 1 to 9999, month is 1 to 12 and day fits the month
     */
    boolean isValid()
    {
        if (yyyy < 1 || yyyy > 9999)       // year must be 1 to 9999
            return false;
        if (mm < 1 || mm > 12)             // month must be 1 to 12
            return false;
        return dd >= 1 && dd <= monthLength(mm, yyyy);
    }

    /**
     * Checks the number of days.
     * @return true if n is 0 to 1000 (negative n is rejected)
     */
    boolean isNValid()
    {
        return n >= 0 && n <= 1000;
    }

    /**
     * Finds the day number of the stored date within its year.
     * @return days of all the months before mm, plus dd
     */
    int dayNumber()
    {
        int total = dd;
        for (int i = 1; i < mm; i++)       // complete months before the given month
        {
            total = total + monthLength(i, yyyy);
        }
        return total;
    }

    /**
     * Adds n days: day number + n, then whole years and whole months are removed.
     * The answer is left in dd, mm and yyyy.
     * @return false if the year of the answer is beyond 9999, otherwise true
     */
    boolean addDays()
    {
        int doy = dayNumber() + n;             // day number, possibly beyond the end of the year
        while (doy > yearLength(yyyy))         // roll over into the following years
        {
            doy = doy - yearLength(yyyy);
            yyyy++;
        }
        if (yyyy > 9999)
            return false;
        mm = 1;
        while (doy > monthLength(mm, yyyy))    // roll over into the following months
        {
            doy = doy - monthLength(mm, yyyy);
            mm++;
        }
        dd = doy;                              // what is left is the day of the month
        return true;
    }

    /**
     * Gives a number as text with a leading zero if it is below 10.
     * @param x the number (day or month)
     * @return the text, e.g. "05" or "12"
     */
    String two(int x)
    {
        if (x < 10)
            return "0" + x;
        return "" + x;
    }

    /** main: reads the input, creates the object and prints the result. */
    public static void main(String args[])
    {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter day (dd): ");
        int d = sc.nextInt();
        System.out.print("Enter month (mm): ");
        int m = sc.nextInt();
        System.out.print("Enter year (yyyy): ");
        int y = sc.nextInt();
        System.out.print("Enter number of days to add (N): ");
        int num = sc.nextInt();
        DatePlusN ob = new DatePlusN(d, m, y, num);
        if (!ob.isValid())
            System.out.println("INVALID DATE");
        else if (!ob.isNValid())
            System.out.println("INVALID NUMBER OF DAYS");
        else if (!ob.addDays())
            System.out.println("RESULT IS BEYOND YEAR 9999");
        else
            System.out.println("New date: " + ob.two(ob.dd) + "-" + ob.two(ob.mm) + "-" + ob.yyyy);
    }
}

Variable description table

VariableData TypePurpose
ddintData member: day of the month (the answer's day after addDays())
mmintData member: month (the answer's month after addDays())
yyyyintData member: year (the answer's year after addDays())
nintData member: number of days to add
daysint[]Data member: days in each month, January at index 0, February taken as 28
dintConstructor parameter and main variable: day typed by the user
mintConstructor parameter and main variable: month typed by the user; also the parameter of monthLength(), the month (1 to 12)
yintConstructor parameter and main variable: year typed by the user; also the parameter of isLeap(), yearLength() and monthLength(), the year
numintConstructor parameter and main variable: number of days typed by the user
totalintLocal variable of dayNumber(): running total of days
iintLoop counter in dayNumber(): month being added
doyintLocal variable of addDays(): day number while rolling over
xintParameter of two(): the day or month to convert to text
scScannerReads the date and N typed by the user
obDatePlusNObject of the class created in main
argsString[]Command-line arguments of main (not used)

Sample input and output

Sample 1

Enter day (dd): 28
Enter month (mm): 2
Enter year (yyyy): 2024
Enter number of days to add (N): 2
New date: 01-03-2024

Sample 2

Enter day (dd): 25
Enter month (mm): 12
Enter year (yyyy): 2023
Enter number of days to add (N): 10
New date: 04-01-2024

Sample 3

Enter day (dd): 29
Enter month (mm): 2
Enter year (yyyy): 2023
Enter number of days to add (N): 5
INVALID DATE

Sample 4

Enter day (dd): 1
Enter month (mm): 1
Enter year (yyyy): 2024
Enter number of days to add (N): -5
INVALID NUMBER OF DAYS

One value per line

Dry run

Take the input 25, 12, 2023, 10. The constructor stores dd = 25, mm = 12, yyyy = 2023, n = 10. isValid() and isNValid() both return true. In addDays(), dayNumber() returns $25 + 334 = 359$ (the 334 days of January to November in a non-leap year), so:

StepdoyyyyymmNote
doy = dayNumber() + n369202312359 + 10
369 > 365: subtract, yyyy++42024122023 has 365 days
4 > 366? No4202412year loop ends
mm = 1420241-
4 > monthLength(1, 2024) = 31? No420241month loop does not run
dd = doy420241addDays() returns true

main prints two(4), two(1) and the year, giving 04-01-2024, as in the second sample run.

Variations

  1. Date Difference "Find the number of days between two given dates."
  2. Date minus N days "Print the date N days before the given date."
  3. Day of the week "Print the day of the week for the date obtained after adding N days."