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

objectives This assignment requires you to write a program in Java that calculat

ID: 3834412 • Letter: O

Question

objectives This assignment requires you to write a program in Java that calculates the day of week and the week of month for a given date, as well as to print the calendar of the month where the given date exists Background 2017 MAY UNIVERSITY OF For a given date, we can use the Gregorian calendar to find the comesponding the day of the week, and the week of the month. For example, May 272017 is a Saturday and locates in the fourth week 2017. In general, we can use the following formula (Zeller's congruence to calculate the day of a week for any given date after October 15 1582. K+ where his the day of the week (0- Saturday, 1 Sunday, 2-Monday, 3 Tuesday, 4 Wednesday, 5 Thursday, 6-Friday g is the day of the month m is the month (3-March, 4-April, 5-May, 6 June, 7 July, 8 August, 9 September, 10 October, 11 November, 12 December, 13 January, 14- February) Kis the year of the century lyear mod 100). year 1000. For example, the zero-based centuries for 1995 and 2000 are 19 and 20 respectively. For example, For 1 January 2000, the date would be treated as the 13th month of 1999, so the values would be: q-1, m-13, K-99, J-19, so the formula is (1+[182/5]+99+199/4] [19/4]+95) mod 7 (1+136.4]+99+124.75] (4.75) 95) mod 7 -(1+36+99+24+4+95) mod 7 Saturday However, for 1 March 2000, the date is treated as the 3rd month of 2000, so the values become q 1, m 3, K-00, J-20, so the formula is

Explanation / Answer

import java.text.ParseException;

import java.text.SimpleDateFormat;

import java.util.Calendar;

import java.util.Date;

public class AgeCalculator

{

   private static Age calculateAge(Date birthDate)

   {

      int years = 0;

      int months = 0;

      int days = 0;

      //create calendar object for birth day

      Calendar birthDay = Calendar.getInstance();

      birthDay.setTimeInMillis(birthDate.getTime());

      //create calendar object for current day

      long currentTime = System.currentTimeMillis();

      Calendar now = Calendar.getInstance();

      now.setTimeInMillis(currentTime);

      //Get difference between years

      years = now.get(Calendar.YEAR) - birthDay.get(Calendar.YEAR);

      int currMonth = now.get(Calendar.MONTH) + 1;

      int birthMonth = birthDay.get(Calendar.MONTH) + 1;

      //Get difference between months

      months = currMonth - birthMonth;

      //if month difference is in negative then reduce years by one and calculate the number of months.

      if (months < 0)

      {

         years--;

         months = 12 - birthMonth + currMonth;

         if (now.get(Calendar.DATE) < birthDay.get(Calendar.DATE))

            months--;

      } else if (months == 0 && now.get(Calendar.DATE) < birthDay.get(Calendar.DATE))

      {

         years--;

         months = 11;

      }

      //Calculate the days

      if (now.get(Calendar.DATE) > birthDay.get(Calendar.DATE))

         days = now.get(Calendar.DATE) - birthDay.get(Calendar.DATE);

      else if (now.get(Calendar.DATE) < birthDay.get(Calendar.DATE))

      {

         int today = now.get(Calendar.DAY_OF_MONTH);

         now.add(Calendar.MONTH, -1);

         days = now.getActualMaximum(Calendar.DAY_OF_MONTH) - birthDay.get(Calendar.DAY_OF_MONTH) + today;

      } else

      {

         days = 0;

         if (months == 12)

         {

            years++;

            months = 0;

         }

      }

      //Create new Age object

      return new Age(days, months, years);

   }

   public static void main(String[] args) throws ParseException

   {

      SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");

      Date birthDate = sdf.parse("29/11/1981"); //Yeh !! It's my date of birth :-)

      Age age = calculateAge(birthDate);

      //My age is

      System.out.println(age);

   }

}