Future Date Program in Java and Python
Future date program with algorithm, explanation, Java solution and simple Python solution for ISC students.
Question:
Write a program to accept a date in dd/mm/yyyy format and a number of days. If the date is valid, calculate and print the future date after adding the given number of days.
Algorithm:
Step 1: Start.
Step 2: Accept the date string and the number of days to add.
Step 3: Find the positions of the slash characters and extract day, month and year.
Step 4: Store month lengths in an array and update February to 29 if the year is a leap year.
Step 5: Validate that the month is from 1 to 12 and the day is within the allowed days of that month.
Step 6: Copy day, month and year into new variables for the future date.
Step 7: Repeat exactly add times, increasing the day by 1 in each iteration.
Step 8: After each increment, update February according to the current year.
Step 9: If day crosses the month limit, set day to 1 and increment month.
Step 10: If month crosses 12, set month to 1 and increment year.
Step 11: Display the original date and the future date.
Step 12: Stop.
Explanation:
The program first separates the date string into day, month and year using the positions of the slash characters. This keeps the input format close to the way dates are written in the question.
The date is validated using the month-days array. Leap year handling is important because February may have either 28 or 29 days depending on the year.
After validation, the program adds the required number of days one day at a time. This is simple to trace: the day is increased first, and then the program checks whether the day has crossed the limit of the current month.
If the day crosses the month limit, the day becomes 1 and the month is increased. If the month becomes 13, the month is reset to 1 and the year is increased. This handles month-end and year-end changes clearly.
The loop that adds days updates the date in small steps. This is less compact than using a formula, but it makes the month limit, February leap-year update and year rollover visible to students.
Finding a future date requires adding a number of days while respecting month lengths and leap years. The program does not simply add to the day field because months have different numbers of days. It repeatedly checks whether the added days exceed the remaining days of the current month. If they do, it moves to the next month and adjusts the remaining count. When the month crosses December, the year increases. Leap-year handling is essential for February because it changes from 28 to 29 days.
Java Program:
/**
* The class FutureDate inputs a date and finds the future date after some given days
* @author : www.guideforschool.com
* @Program Type : BlueJ Program - Java
*/
import java.util.Scanner;
class FutureDate
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
int month[]={0,31,28,31,30,31,30,31,31,30,31,30,31};
System.out.print("Enter the date in (dd/mm/yyyy) format: ");
String date=sc.nextLine().trim();
int p,q,count=0;
p=date.indexOf("/");
int d=Integer.parseInt(date.substring(0,p));
q=date.lastIndexOf("/");
int m=Integer.parseInt(date.substring(p+1,q));
int y=Integer.parseInt(date.substring(q+1));
System.out.println("Entered Date: "+date);
if((y%400==0) || ((y%100!=0)&&(y%4==0))) // Checking for leap year
month[2]=29;
if(m<0 || m>12 || d<0 || d>month[m] || y<0 || y>9999) // Performing Date Validation
{
System.out.println("Invalid Date");
}
else
{
System.out.print("Enter number of days after which future date is to be found: ");
int days=sc.nextInt();
while(count<days)
{
d++;
count++;
/* If day exceeds the maximum days of a month then day should start from 1
and month should increase */
if(d>month[m])
{
d=1;
m++;
}
/* If month exceeds 12 then month should start from 1
and year should increase */
if(m>12)
{
m=1;
y++;
if((y%400==0) || ((y%100!=0)&&(y%4==0)))
month[2]=29;
else
month[2]=28;
}
}
System.out.println("Future Date : "+d+"/"+m+"/"+y);
}
}
}Equivalent Python Program:
# Read the date values and separate day, month and year for calculation.
# Helper functions keep repeated calculations separate from the main logic.
# Month lengths and leap-year checks control valid date movement/counting.
# Display the calculated date/day result after all updates are complete.
def is_leap(y):
return y % 400 == 0 or (y % 100 != 0 and y % 4 == 0)
month = [0,31,28,31,30,31,30,31,31,30,31,30,31]
date = input("Enter the date in dd/mm/yyyy format: ")
add = int(input("Enter number of days after: "))
p = date.find('/')
q = date.rfind('/')
d = int(date[0:p])
m = int(date[p + 1:q])
y = int(date[q + 1:])
if is_leap(y):
month[2] = 29
if m < 1 or m > 12 or d < 1 or d > month[m]:
print("Invalid Date")
else:
nd = d
nm = m
ny = y
for i in range(1, add + 1):
nd = nd + 1
if is_leap(ny):
month[2] = 29
else:
month[2] = 28
if nd > month[nm]:
nd = 1
nm = nm + 1
if nm > 12:
nm = 1
ny = ny + 1
print("Entered Date:", date)
print("Future Date: " + str(nd) + "/" + str(nm) + "/" + str(ny))Output:
Leave a Reply
Your email address will not be published. Comments are reviewed before appearing publicly.