Sunday, December 15, 2013

Programming Challenges Question 6

Programming Challenges Question 

Write a program which will take the year (yyyy) and the numeric sequence of the month (0-11) as its input. 
The program will return the day on which the 28th of that particular month and year falls. The input can consist of two year-month combinations, one combination per line.

The numeric sequence of months is as follows:
0 – Jan
1 – Feb
2 – March
and so on......

The format for supplying the input is:
1999-5
Where 1999 is the year and 5 is the numeric sequence of the month (corresponding to June). The program should display the day on which June 28, 1999 fell, and in this case the output will be MONDAY. 

The output should be displayed in uppercase letters.
Suppose the following INPUT sequence is given to the program:
1999-5
1998-6
Then the output should be:
MONDAY
TUESDAY

/****************************Code in JAVA*************************************/
import java.io.*;
import java.text.DateFormatSymbols;
import java.util.*;

public class Main {
    public static void main(String[] args) {
    try{
       String data[]= new String[2];
       InputStreamReader sr= new InputStreamReader(System.in);
       BufferedReader br= new BufferedReader(sr);
       for(int i=0;i<2;i++)
       {
         data[i]=br.readLine();
         Calendar cal=Calendar.getInstance();
         cal.set(Integer.parseInt(data[i].split("-")[0]), Integer.parseInt(data[i].split("-")[1]), 28);
         String dayName = new DateFormatSymbols().getWeekdays()[cal.get(Calendar.DAY_OF_WEEK)];
         System.out.println(dayName.toUpperCase());
        }
        }
    catch(Exception ex)
    {
        System.out.println(ex.toString());
    }

 }
}




No comments:

Post a Comment