This problem has 5 points Complete problems 411 and 419 in y

This problem has 5 points. Complete problems 411 and 4.19 in your textbook. This problem has 10 points. One of the most frustrating aspects of using the Gregorian calendar is that it is difficult to communicate compute elapsed time. For example, if I tell you that something is experiencing its 2-month anniversary, you don\'t know the exact number of day that have elapsed unless tell you which 2 months February 3rd to April 3rd is 59 days (in a non-leap year), while April 3rd to June 3rd is 61 days. For this problem, you need to write a program that will accept two days of the same year (in month-day form) and output the elapsed time between the two days (not including the last day). To keep it simple, the program should first ask the user for the month of the first day, then the date of the first day, then the month of the second day, and lastly the date of the second day (so four total prompts). For example, if you wanted to know the elapsed number of days between September 4th and November 18th, you would enter 9 into the first prompt. 4 into the second prompt. 11 into the third prompt, and 18 into the fourth prompt. Assume that it is not a leap year?

Solution

//Following program is in java


import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.Scanner;


public class DateDiff {

   public static void main(String[] args) {
       Scanner scan= new Scanner(System.in);
       System.out.println(\"Enter 1st day\");
       int dayFirst=scan.nextInt();//getting the first day
       System.out.println(\"Enter 1st Month\");
       int monthFirst=scan.nextInt();//getting the first month
       System.out.println(\"Enter 2nd day\");
       int daySecond=scan.nextInt();//getting the second day
       System.out.println(\"Enter 2nd Month\");
       int monthSecond=scan.nextInt();//getting the second month
      
       int year= Calendar.getInstance().get(Calendar.YEAR);//getting the current year from the calender
      
       GregorianCalendar gc = new GregorianCalendar();
       gc.set(GregorianCalendar.DAY_OF_MONTH, dayFirst);//setting the first day to the calender
       gc.set(GregorianCalendar.MONTH, monthFirst-1);//setting the first month to the calender. Index start with 0 hence minus with 1
       gc.set(GregorianCalendar.YEAR, year);//setting the year to the calender
       int num1stDay=gc.get(GregorianCalendar.DAY_OF_YEAR);//getting the nth day of the year
      
       GregorianCalendar gcSecond = new GregorianCalendar();
       gcSecond.set(GregorianCalendar.DAY_OF_MONTH, daySecond);
       gcSecond.set(GregorianCalendar.MONTH, monthSecond-1);//setting the second month to the calender. Index start with 0 hence minus with 1
       gcSecond.set(GregorianCalendar.YEAR, year);//setting the year to the calender
       int num2ndDay=gcSecond.get(GregorianCalendar.DAY_OF_YEAR);//getting the nth day of the year
      
       System.out.println(num2ndDay-num1stDay+\" day(s)elapsed between two dates\");//showing the difference of two days
      
       scan.close();
      
              
   }

}

/---------------------output-----------------------/
Enter 1st day
4
Enter 1st Month
1
Enter 2nd day
6
Enter 2nd Month
2
33 day(s)elapsed between two dates
/-------------output-----------------------/

Note: Feel free to ask question if any doubt

 This problem has 5 points. Complete problems 411 and 4.19 in your textbook. This problem has 10 points. One of the most frustrating aspects of using the Gregor
 This problem has 5 points. Complete problems 411 and 4.19 in your textbook. This problem has 10 points. One of the most frustrating aspects of using the Gregor

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site