Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Write a java program to prompt the user for 2 dates consisting of a month and a year and display the number of years and

Write a java program to prompt the user for 2 dates consisting of a month and a year and display the

number of years and months between these 2 dates. Either date could be the earlier date.

(Your sample runs must follow the exact format.)

Sample Run 1:

For the first date,

Enter month: August

Enter year: 2011

For the second date,

Enter month: March

Enter year: 1999

These dates are 12 years and 5 months apart.

Sample Run 2:

For the first date,

Enter month: June

Enter year: 1999

For the second date,

Enter month: April

Enter year: 2002

These dates are 2 years and 10 months apart.

Sample Run 3:

For the first date,

Enter month: July

Enter year: 1998

For the second date,

Enter month: July

Enter year: 1995

These dates are 3 years and 0 months apart.

Sample Run 4:

For the first date,

Enter month: March

Enter year: 1995

For the second date,

Enter month: July

Enter year: 1995

These dates are 0 years and 4 months apart.

Sample Run 5:

For the first date,

Enter month: December

Enter year: 1998

For the second date,

Enter month: December

Enter year: 1998

These dates are 0 years and 0 months apart.

HINT:

It is important to store the month both as a String type value and as an integer type value (1 for January, 2 for February, ect... ). The String type name is used to check which month the user entered; the integer number is used for calculating date difference.

if ( firstMonth.equalsIgnoreCase(January))

{

firstMonthNumber = 1;

}

else if ( firstMonth.equalsIgnoreCase(February))

{

firstMonthNumber = 2;

}

.

.

.

else

{

...

}

A Difference between two date can be calculated in the following steps:

1) Subtract the larger year from the smaller year to get the year difference. Year difference

is 0 or positive.

2) Subtract the month associated with the larger year from the month associated with the

smaller year to get the month difference. The month difference can be negative as

shown in sample 2

No. Dates Subtracting months and years Real Date Difference

1 August, 2011 12 years and 5 months These dates are 12 years and 5 months apart

March, 1999

2 June, 199 3 years and -2 months These dates are 2 years and 10 months apart

April, 2002

In sample 2, the month difference is negative. The difference, 3 years and -2 months,

means it would be 3 years difference if this was 2 months later. But the date difference

has to be a positive value. IF our month difference turns out to be negative we must

3) Adjust the difference by regrouping 1 year into 12 months

a. Deduct 1 year from the year difference

b. And add 12 to the month difference

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Horse Betting The Road To Absolute Horse Racing 2

Authors: NAKAGAWA,YUKIO

1st Edition

B0CFZN219G, 979-8856410593

More Books

Students also viewed these Databases questions