question archive Java Assignment Using Java Date and Time libraries, create a new class called CalendarHelper

Java Assignment Using Java Date and Time libraries, create a new class called CalendarHelper

Subject:Computer SciencePrice:3.87 Bought7

Java Assignment

Using Java Date and Time libraries, create a new class called CalendarHelper.

This class should do the following:

  1. Create a list of five dates that are holidays (ex: December 25)
  2. Ask the user for their birthday and add it to this list
  3. Print the current date
  4. Display the days remaining until all holidays in the list (ex: if the current day is December 10, there are 15 days remaining until Christmas)
  5. Alert the user which holiday is coming soonest

pur-new-sol

Purchase A New Answer

Custom new solution created by our subject matter experts

GET A QUOTE

Answer Preview

Answer:

import java.util.*;
import java.util.Scanner;//this is used for taking inputs from the user
import java.time.LocalDate;//to get present date
import java.time.Month;//to get present month
import java.time.temporal.ChronoUnit;//to get number of days between two given dates

class ArrayList2{

public static void main(String args[]){

    Calendar now = Calendar.getInstance();//intializing the date
    Scanner in = new Scanner(System.in);//intilalising the scanner
    int i=0;//intializing a integer i
    List<String> list=new ArrayList<String>();//Creating arraylist
    ArrayList<String> months=new ArrayList<String>();//to store months seperately

    System.out.print("Enter your date of Birth(ex:MAY 04):");//printing the statement
    list.add(in.nextLine());//asking user to enter date of Birth
    list.add("January 6");//similarly adding all the date strings
    list.add("April 10");
    list.add("November 1");
    list.add("December 25");


    int [] date = new int[list.size()];//creating an array of integers to save numbers
    long[] compare_dates = new long[list.size()];//another array to store the number of days for each holidays
//    LocalDate currentdate = LocalDate.now();
    Date obDate = new Date();//creating a Date object
    int day_of_month = now.get(Calendar.DAY_OF_MONTH);//Saving current day in int day_of_month
    int currentMonth = now.get(Calendar.MONTH)+1;//storing current month
    int year = now.get(Calendar.YEAR);//storing current year

    System.out.print("Todays date is:"+getMonthName(currentMonth)+" "+obDate.getDate());
    System.out.print(" "+year);//comment it by using "//" at the begging of the line
    //printing present date
    System.out.print("\n");//to next line

    for(String x: list){
      System.out.println(x);//comment this if you dont want to see the output by "//" adding at the beggining
      months.add(getMonth(x));//seperating MOnth and adding it to the list
      date[i] = getDate(x);//seperating date and adding it to the array
      i++;
    }
  
    int j=0;//index position of j to specifiy the date of month
    int k=0;//index position of comparing array
    for(String monthitem:months){

      if(getpriority(monthitem)<getpriority(getMonthName(currentMonth))){
      //checking if the month is going to be present in the year
          long x = TotalDays(year,currentMonth,day_of_month,year+1,getpriority(monthitem),date[j]);
          //finding number of days between the present date and holidays
          System.out.println("Number of days from ("+getMonthName(currentMonth)+" "+obDate.getDate()+") to ("+monthitem+" "+date[j]+")are="+x);
          //printing statement for confirmation
          j++;
          compare_dates[k] = x;//storing the number of days
          k++;
      }
      else if (getpriority(monthitem)==getpriority(getMonthName(currentMonth))) {
        //if the month is same but date has passed away then we have to count the number of days in the next year
        if(obDate.getDate()>date[j]){
          long x = TotalDays(year,currentMonth,day_of_month,year+1,getpriority(monthitem),date[j]);
          //finding number of days between the present date and holidays
          System.out.println("Number of days from ("+getMonthName(currentMonth)+" "+obDate.getDate()+") to ("+monthitem+" "+date[j]+")are="+x);
          //printing statement for confirmation
          j++;
          compare_dates[k] = x;//storing the number of days
          k++;
        }
      
      }
      else{
        long x = TotalDays(year,currentMonth,day_of_month,year,getpriority(monthitem),date[j]);
        System.out.println("Number of days from ("+getMonthName(currentMonth)+" "+obDate.getDate()+") to ("+monthitem+" "+date[j]+")are="+x);
        j++;
        compare_dates[k] = x;
          k++;
      }
    }
    Arrays.sort(compare_dates);//sorting the array to get minimum number of holidays date
    System.out.println("The nearest Holiday is"+list.get(minIndec(compare_dates)));//getting the index of minimum number of days
}


static String getMonth(String str){ //getMOnth method to seperate months from the list
        String alpha="";//intialising the string to store the aplhabets
        for (int i=0; i<str.length(); i++) //iterating through the string
        {
            if(Character.isAlphabetic(str.charAt(i))) //checking whether the character is a alphbet
                alpha+=(str.charAt(i)); //appending to the string
        }
        return alpha;//returning the alphabet
    }

static int getDate(String str){
        String num="";
        for (int i=0; i<str.length(); i++)
        {
            if (Character.isDigit(str.charAt(i)))
                num+=(str.charAt(i));
        }
        return Integer.parseInt(num);
    }

static int getpriority(String month){
    //to get the month number if months name is given
    month = month.toUpperCase();//converting all characters to uppercase letters
    if(month.equals("JANUARY")){
      return 1;
    }
    else if (month.equals("FEBRUARY")) {
      return 2;
    }
    else if (month.equals("MARCH")){
      return 3;
    }
    else if (month.equals("APRIL")){
      return 4;
    }
    else if (month.equals("MAY")){
      return 5;
    }
    else if (month.equals("JUNE")){
      return 6;
    }
    else if (month.equals("JULY")){
      return 7;
    }
    else if (month.equals("AUGUST")){
      return 8;
    }
    else if (month.equals("SEPTEMBER")){
      return 9;
    }
    else if (month.equals("OCTOBER")){
      return 10;
    }
    else if (month.equals("NOVEMBER")){
      return 11;
    }
    else if (month.equals("DECEMBER")){
      return 12;
    }
    return 0;
}

static String getMonthName(int month){
    //method to getNameof the month for the specific numbet given
    if(month==1){//checking whether the parameter passed is 1 and returning month name
      return "JANUARY";
    }
    else if (month==2) {
      return "FEBRUARY";
    }
    else if (month==3) {
      return "MARCH";
    }
    else if (month==4) {
      return "APRIL";
    }
    else if (month==5) {
      return "MAY";
    }
    else if (month==6) {
      return "JUNE";
    }
    else if (month==7) {
      return "JULY";
    }
    else if (month==8) {
      return "AUGUST";
    }
    else if (month==9) {
      return "SEPTEMBER";
    }
    else if (month==10) {
      return "OCTOBER";
    }
    else if (month==11) {
      return "NOVEMBER";
    }
    else if (month==12) {
      return "DECEMBER";
    }
    return "ERROR";
}

static long TotalDays(int curYear,int curMonth,int curDate,int finYear,int finMonth ,int finDate){
    //taking parameters where dateBefore is the current date and date Afteris Holidays Date
    LocalDate dateBefore = LocalDate.of(curYear, curMonth, curDate);
        //29-July-2017, change this to your desired End Date
    LocalDate dateAfter = LocalDate.of(finYear, finMonth, finDate);
    //LocalDate dateAfter = LocalDate.of(2017, 7, 29);
    long noOfDaysBetween = ChronoUnit.DAYS.between(dateBefore, dateAfter);
    return noOfDaysBetween;//returning number of days between present date and the holiday date
}

static int minIndec(long [] array){
    //this method returns the index of minimum number oh holidays
    long x = array[0];
    int j=0;
    for (int i=0;i<array.length;i++ ) {
      if(array[i]<=x){//checking for the minimum
        j= i;//if minimum number is found it is replaced by i to j
      }
    }
    return j;//returning the index of the holiday date
}
}

PFA

Related Questions