Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Using C++ Write a program that inputs a pair of month/year dates and outputs the

ID: 644388 • Letter: U

Question

Using C++


Write a program that inputs a pair of month/year dates and outputs the total months contained within the range covered by these dates. The program should work correctly whether the first date inputted is before, equal to, or after, the second date inputted.

Using C++ Write a program that inputs a pair of month/year dates and outputs the total months contained within the range covered by these dates. The program should work correctly whether the first date inputted is before, equal to, or after, the second date inputted. Each date should be input as two integers with a slash ( / )character between them. Other input errors (such as a character other than slash between the month and year numbers) should result in a error message followed by shutting the program down.

Explanation / Answer

import java.util.Calendar;
import java.util.Date;
import java.text.ParseException;
import java.util.concurrent.TimeUnit;
import java.text.SimpleDateFormat;
import java.util.Scanner;
public class HelloWorld{

public static void main(String []args){
Scanner sc = new Scanner(System.in);
SimpleDateFormat myFormat = new SimpleDateFormat("MM/yyyy");
System.out.println("Enter first date like 03/2015");
String inputString1 = sc.nextLine();
System.out.println("Enter second date like 03/2015");
String inputString2 = sc.nextLine();
try {
Date date1 = myFormat.parse(inputString1);
Date date2 = myFormat.parse(inputString2);
long diff = date1.getTime() - date2.getTime();
System.out.println ("MONTH: " + TimeUnit.DAYS.convert(diff, TimeUnit.MILLISECONDS)/30);
} catch (ParseException e) {
e.printStackTrace();
}
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote