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
- Read the day
dd, the monthmm, the yearyyyyand the number of days to add,n. - Create the array
dayswith 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 setdays[1] = 29if it is. - 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 ifnis not from 0 to 1000; otherwise 0. - If
statusis 0, find the day number of the date in its year:doy = ddplus the days of all the months beforemm. Then addn. - Roll over into the following years: while
doyis more than the length of the year (365 or 366), subtract that length and add 1 toyyyy, working out again whether the new year is a leap year. Ifyyyygoes beyond 9999, setstatus = 3. - Otherwise set
days[1]for the final year, start withmm = 1, and roll over into the following months: whiledoy > days[mm - 1], subtract that month's days and add 1 tomm. What is left is the day of the month,dd = doy. - Print the message for
status1, 2 or 3, or the new date asdd-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
| Variable | Data Type | Purpose |
|---|---|---|
args | String[] | Command line arguments of main; not used |
sc | Scanner | Reads the numbers typed by the user |
dd | int | Day of the date; holds the day of the answer at the end |
mm | int | Month of the date; holds the month of the answer at the end |
yyyy | int | Year of the date; holds the year of the answer at the end |
n | int | Number of days to add (0 to 1000) |
days | int[] | days[0] to days[11] hold the days in January to December |
leap | boolean | True if the year being used is a leap year |
status | int | 0 = fine, 1 = invalid date, 2 = invalid N, 3 = result beyond year 9999 |
doy | int | Day number within the year, used while rolling over |
i | int | Loop counter over the months before the given month |
len | int | Length 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-2024Sample 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-2024Sample 3
Enter day (dd): 29
Enter month (mm): 2
Enter year (yyyy): 2023
Enter number of days to add (N): 5
INVALID DATESample 4
Enter day (dd): 1
Enter month (mm): 1
Enter year (yyyy): 2024
Enter number of days to add (N): -5
INVALID NUMBER OF DAYSDry 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:
| Step | doy | yyyy | mm | Note |
|---|---|---|---|---|
doy = dd | 25 | 2023 | 12 | - |
Loop i = 0 to 10 | 359 | 2023 | 12 | adds the 334 days of January to November |
doy = doy + n | 369 | 2023 | 12 | - |
369 > 365: subtract, yyyy++ | 4 | 2024 | 12 | 2023 has 365 days |
days[1] = 29, mm = 1 | 4 | 2024 | 1 | 2024 is a leap year |
4 > days[0]? No | 4 | 2024 | 1 | loop does not run |
dd = doy | 4 | 2024 | 1 | dd = 4 |
The program prints 04-01-2024, as in the second sample run.
Variations
- Date Difference "Find the number of days between two given dates."
- Date minus N days "Print the date N days before the given date."
- Day of the week "Print the day of the week for the date obtained after adding N days."