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

ISC Practicals and Theory - Date Plus N Days

Updated 21 Oct 2026

Adding N days to a date means counting forward N days from it, moving into the next month and the next year whenever the current one runs out. This program reads a date and N, rejects an impossible date or an unacceptable N, and prints the new date as dd-mm-yyyy. This is the Main version: everything is written in the main method.

Definition

The method works with the day number of the date (its position in the year, 1 for 1 January). Add N to it; if the result is more than the length of the year, subtract the year's length and move to the next year, and repeat. What is left is a day number inside the final year, which is then turned back into a month and a 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$: the day number is $359$, and $359 + 10 = 369$. The year 2023 has 365 days, so $369 - 365 = 4$ and the year becomes 2024. Day 4 of 2024 is 4 January, so the answer is 04-01-2024.

Algorithm

  1. Read the day dd, the month mm, the year yyyy and the number of days to add, n.
  2. Create the array days with the days of January to December, taking February as 28. Find whether the year is a leap year (divisible by 4 and not by 100, or divisible by 400) and set days[1] = 29 if it is.
  3. Set status: 1 if the year is not from 1 to 9999, the month not from 1 to 12 or the day does not fit the month; 2 if n is not from 0 to 1000; otherwise 0.
  4. If status is 0, find the day number of the date in its year: doy = dd plus the days of all the months before mm. Then add n.
  5. Roll over into the following years: while doy is more than the length of the year (365 or 366), subtract that length and add 1 to yyyy, working out again whether the new year is a leap year. If yyyy goes beyond 9999, set status = 3.
  6. Otherwise set days[1] for the final year, start with mm = 1, and roll over into the following months: while doy > days[mm - 1], subtract that month's days and add 1 to mm. What is left is the day of the month, dd = doy.
  7. Print the message for status 1, 2 or 3, or the new date as dd-mm-yyyy, with a leading zero for a day or month below 10.

Program

Everything is written inside main: the validation, the two roll-over loops (years, then months) and the printing of the date with leading zeros.

import java.util.Scanner;
public class DatePlusN
{
    public static void main(String args[])
    {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter day (dd): ");
        int dd = sc.nextInt();                 // day of the date (it holds the answer after the days are added)
        System.out.print("Enter month (mm): ");
        int mm = sc.nextInt();                 // month of the date
        System.out.print("Enter year (yyyy): ");
        int yyyy = sc.nextInt();               // year of the date
        System.out.print("Enter number of days to add (N): ");
        int n = sc.nextInt();                  // number of days to add (0 to 1000)
        int days[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};   // days[0..11] = days in January..December (February = 28 for now)
        boolean leap = (yyyy % 4 == 0 && yyyy % 100 != 0) || (yyyy % 400 == 0);
        if (leap)
            days[1] = 29;
        else
            days[1] = 28;
        int status = 0;                        // 0 = fine, 1 = invalid date, 2 = invalid N, 3 = result beyond year 9999
        if (yyyy < 1 || yyyy > 9999)           // year must be 1 to 9999
            status = 1;
        else if (mm < 1 || mm > 12)            // month must be 1 to 12
            status = 1;
        else if (dd < 1 || dd > days[mm - 1])  // day must fit the month
            status = 1;
        else if (n < 0 || n > 1000)            // N must be 0 to 1000 (negative N is rejected)
            status = 2;
        if (status == 0)
        {
            int doy = dd;                      // day number within the year, used while rolling over
            for (int i = 0; i < mm - 1; i++)   // day number of the given date
            {
                doy = doy + days[i];
            }
            doy = doy + n;
            int len = 365;                     // length of the year yyyy
            if (leap)
                len = 366;
            while (doy > len)                  // roll over into the following years
            {
                doy = doy - len;
                yyyy++;
                leap = (yyyy % 4 == 0 && yyyy % 100 != 0) || (yyyy % 400 == 0);
                if (leap)
                    len = 366;
                else
                    len = 365;
            }
            if (yyyy > 9999)                   // result is outside the allowed years
            {
                status = 3;
            }
            else
            {
                if (leap)                      // February of the final year
                    days[1] = 29;
                else
                    days[1] = 28;
                mm = 1;
                while (doy > days[mm - 1])     // roll over into the following months
                {
                    doy = doy - days[mm - 1];
                    mm++;
                }
                dd = doy;                      // what is left is the day of the month
            }
        }
        if (status == 1)
            System.out.println("INVALID DATE");
        else if (status == 2)
            System.out.println("INVALID NUMBER OF DAYS");
        else if (status == 3)
            System.out.println("RESULT IS BEYOND YEAR 9999");
        else
        {
            System.out.print("New date: ");
            if (dd < 10)
                System.out.print("0");
            System.out.print(dd);
            System.out.print("-");
            if (mm < 10)
                System.out.print("0");
            System.out.print(mm);
            System.out.println("-" + yyyy);
        }
    }
}

Variable description table

VariableData TypePurpose
argsString[]Command line arguments of main; not used
scScannerReads the numbers typed by the user
ddintDay of the date; holds the day of the answer at the end
mmintMonth of the date; holds the month of the answer at the end
yyyyintYear of the date; holds the year of the answer at the end
nintNumber of days to add (0 to 1000)
daysint[]days[0] to days[11] hold the days in January to December
leapbooleanTrue if the year being used is a leap year
statusint0 = fine, 1 = invalid date, 2 = invalid N, 3 = result beyond year 9999
doyintDay number within the year, used while rolling over
iintLoop counter over the months before the given month
lenintLength of the year being used, 365 or 366

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. days[1] is 28 (2023 is not a leap year) and everything is valid, so status is 0. Then the days are added:

StepdoyyyyymmNote
doy = dd25202312-
Loop i = 0 to 10359202312adds the 334 days of January to November
doy = doy + n369202312-
369 > 365: subtract, yyyy++42024122023 has 365 days
days[1] = 29, mm = 14202412024 is a leap year
4 > days[0]? No420241loop does not run
dd = doy420241dd = 4

The program prints 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."