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
mainreadsd,m,yandnumwith aScannerand createsob = new DatePlusN(d, m, y, num). The constructor copies them intodd,mm,yyyyandnand fills the arraydayswith 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 (index 0 is January).isLeap(y)returnstrueifyis divisible by 4 and not by 100, or divisible by 400.yearLength(y)returns 366 or 365 from it.monthLength(m, y)returnsdays[m - 1], but 29 for February of a leap year.maintestsob.isValid()(year 1 to 9999, month 1 to 12, day 1 tomonthLength(mm, yyyy)) and printsINVALID DATEif it is false. Otherwise it testsob.isNValid()(nfrom 0 to 1000) and printsINVALID NUMBER OF DAYSif false.- Otherwise
maincallsob.addDays():doyisdayNumber() + n, wheredayNumber()addsddand themonthLength(i, yyyy)of each month beforemm;- while
doyis more thanyearLength(yyyy), that length is subtracted andyyyyincreases by 1; - if
yyyyis above 9999 the method returnsfalseandmainprintsRESULT IS BEYOND YEAR 9999; mmis set to 1 and, whiledoyis more thanmonthLength(mm, yyyy), that length is subtracted andmmincreases by 1;ddis set todoyand the method returnstrue.
mainprints the new date usingtwo(), 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
| Variable | Data Type | Purpose |
|---|---|---|
dd | int | Data member: day of the month (the answer's day after addDays()) |
mm | int | Data member: month (the answer's month after addDays()) |
yyyy | int | Data member: year (the answer's year after addDays()) |
n | int | Data member: number of days to add |
days | int[] | Data member: days in each month, January at index 0, February taken as 28 |
d | int | Constructor parameter and main variable: day typed by the user |
m | int | Constructor parameter and main variable: month typed by the user; also the parameter of monthLength(), the month (1 to 12) |
y | int | Constructor parameter and main variable: year typed by the user; also the parameter of isLeap(), yearLength() and monthLength(), the year |
num | int | Constructor parameter and main variable: number of days typed by the user |
total | int | Local variable of dayNumber(): running total of days |
i | int | Loop counter in dayNumber(): month being added |
doy | int | Local variable of addDays(): day number while rolling over |
x | int | Parameter of two(): the day or month to convert to text |
sc | Scanner | Reads the date and N typed by the user |
ob | DatePlusN | Object of the class created in main |
args | String[] | 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-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. 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:
| Step | doy | yyyy | mm | Note |
|---|---|---|---|---|
doy = dayNumber() + n | 369 | 2023 | 12 | 359 + 10 |
369 > 365: subtract, yyyy++ | 4 | 2024 | 12 | 2023 has 365 days |
4 > 366? No | 4 | 2024 | 12 | year loop ends |
mm = 1 | 4 | 2024 | 1 | - |
4 > monthLength(1, 2024) = 31? No | 4 | 2024 | 1 | month loop does not run |
dd = doy | 4 | 2024 | 1 | addDays() returns true |
main prints two(4), two(1) and the year, giving 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."