Java Program of Birthday

Birthday – Day

Question:

The digits on showroom wants to send offer messages to their customers on all days of the week. For example, if the day is Monday “The offer is 20% OFF”, if it is Tuesday “The offer is buy one get one”. Likewise, the showroom has come up with different offers for all 7 days. The showroom does not want to send these offers to all the customers though. If the customer’s birthday falls on a “Monday”, then the Monday offer will be forwarded to that customer. Help the showroom to find the day on which the customer’s birthday falls on.

Partial code to do the above task is already given.  In the Birthday class, a method called findDay will accept the date as argument and it should return the day, but if the date format is invalid then return “Invalid Date”.

 [The input is a date in the format “MM-dd-yyyy”, and the output will be the day]. The valid date format should be “MM-dd-yyyy”. Handle the exception, if the date is in a different format.

Sample Input and Output1:

08-12-1984

Sunday

Sample Input and Output2:

28-12-1984

Invalid Date

Sample Input and Output2:

abc

Invalid Date

Code:

Main.java

 import java.util.Scanner;

 public class Main {
  public static void main(String[] args) {
 		Scanner sc=new Scanner(System.in);
 		String date=sc.next();
  	    //FILL CODE
 		Birthday a= new Birthday();
 	System.out.println(a.findDay(date));
 	
 	}
   }

Birthday.java

import java.util.Date;
import java.text.*;


public class Birthday {

   public String findDay(String date)
     {
      //FILL CODE
      SimpleDateFormat sf = new SimpleDateFormat("MM-dd-yy");
      sf.setLenient(false);
       try {
           Date date1=sf.parse(date);
           sf=new SimpleDateFormat("EEEE");
           return sf.format(date1);
           
        } catch(ParseException e) {
           return "Invalid Date";
        }		
      }
}

Recommended:

  1. Birthday
  2. Marathon Registration
  3. Array Manipulation – Use try with multi catch

Leave a Comment

Your email address will not be published. Required fields are marked *