Algorithm




1. Start

2. Input the first date (day1, month1, year1)
   - Prompt the user to enter day1
   - Prompt the user to enter month1
   - Prompt the user to enter year1

3. Input the second date (day2, month2, year2)
   - Prompt the user to enter day2
   - Prompt the user to enter month2
   - Prompt the user to enter year2

4. Calculate the sum of days, months, and years
   4.1. Set totalDays = day1 + day2
   4.2. Set totalMonths = month1 + month2
   4.3. Set totalYears = year1 + year2

5. Adjust the totalDays, totalMonths, and totalYears for overflow
   5.1. If totalDays > maximum days in month
        - Subtract maximum days in month from totalDays
        - Increment totalMonths by 1
   5.2. If totalMonths > 12
        - Subtract 12 from totalMonths
        - Increment totalYears by 1

6. Display the result
   - Print "The sum of the two dates is: totalDays / totalMonths / totalYears"

7. Stop

Code Examples

#1 Code Example- Java program to add two dates

Code - Java Programming

import java.util.Calendar;

public class AddDates {

    public static void main(String[] args) {

        Calendar c1 = Calendar.getInstance();
        Calendar c2 = Calendar.getInstance();
        Calendar cTotal = (Calendar) c1.clone();

        cTotal.add(Calendar.YEAR, c2.get(Calendar.YEAR));
        cTotal.add(Calendar.MONTH, c2.get(Calendar.MONTH) + 1); // Zero-based months
        cTotal.add(Calendar.DATE, c2.get(Calendar.DATE));
        cTotal.add(Calendar.HOUR_OF_DAY, c2.get(Calendar.HOUR_OF_DAY));
        cTotal.add(Calendar.MINUTE, c2.get(Calendar.MINUTE));
        cTotal.add(Calendar.SECOND, c2.get(Calendar.SECOND));
        cTotal.add(Calendar.MILLISECOND, c2.get(Calendar.MILLISECOND));

        System.out.format("%s + %s = %s", c1.getTime(), c2.getTime(), cTotal.getTime());

    }
}
Copy The Code & Try With Live Editor

Output

x
+
cmd
Tue Aug 08 10:20:56 NPT 2023+ Tue Aug 08 10:20:56 NPT 2023= Mon Apr 16 20:41:53 NPT 4035
Advertisements

Demonstration


Java Programing Example to Add Two Dates-DevsEnv