Click here to Skip to main content
15,910,981 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
You are given a date. Your task is to find what the day is on that date.

Input Format

A single line of input containing the space separated month, day and year, respectively, in MM DD YYYY format.

Output Format

Output the correct day in capital letters.

My code:

C#
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int month = in.nextInt();
        int day = in.nextInt();
        int year = in.nextInt();
        Calendar cal = Calendar.getInstance();
        
        cal.set(month,day,year);
        switch(cal.get(cal.DAY_OF_WEEK))
            {
            case 1:System.out.println("SUNDAY");
            break;
            case 2:System.out.println("MONDAY");
            break;
            case 3:System.out.println("TUESDAY");
            break;
            case 4:System.out.println("WEDNESDAY");
            break;
            case 5:System.out.println("THURSDAY");
            break;
            case 6:System.out.println("FRIDAY");
            break;
            case 7:System.out.println("SATURDAY");
            break;
        }
    }
}


What I have tried:

The above code yields success only for few test cases and fails for major test cases.
Can someone help me notifying where am I making a mistake ?
Posted
Updated 3-Apr-17 8:04am

are you sure

cal.set(month,day,year);


is correct ? I'd have a look at the doc Calendar (Java Platform SE 7 )[^] - I would have thought using a calendar for this was overkill - in my day we used Zeller's congruence and liked it Zeller's congruence - Wikipedia, the free encyclopedia[^]
 
Share this answer
 
Comments
GaneshRfromSpace 28-Sep-16 5:03am    
Thanks. Implemented Zeller's rule and it worked fine for all test cases.
Why to force doors wide open? Java supports custom formats for date formatting.

Check this: Customizing Formats[^]
SimpleDateFormat (Java Platform SE 6)[^]

Another way is shown here: Change date format in a Java string - Stack Overflow[^]
 
Share this answer
 
i have experienced the same problem, the wrong u did was that the months in callender class starts from 0 to 11,
so 0=January,
1= February ,...
..
...
and so on...
the solution to that problem is ..
Java
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
		int month = in.nextInt();
		int day = in.nextInt();
        
        
        int year = in.nextInt();
        Calendar cal = Calendar.getInstance();
          
      /// cal.set(month,day,year);
	  cal.set(cal.YEAR,year);
	  cal.set(cal.MONTH,month-1);
	  cal.set(cal.DATE,day);
//System.out.println("Calendar's year:-- " + cal.get(Calendar.YEAR));
	  // System.out.println("Calendar's month:-- " + cal.get(Calendar.MONTH));
		
		 //  System.out.println("Calendar's Date:-- " + cal.get(Calendar.DATE));
		  // System.out.println( + cal.get(Calendar.DAY_OF_MONTH));
		 //   System.out.println("Calendar's Day:-- " + cal.get(Calendar.DAY_OF_WEEK));
        // switch(cal.get(cal.DAY_OF_WEEK))
		
						switch(cal.get(Calendar.DAY_OF_WEEK))
            {
            case 1:System.out.println("SUNDAY");
            break;
            case 2:System.out.println("MONDAY");
            break;
            case 3:System.out.println("TUESDAY");
            break;
            case 4:System.out.println("WEDNESDAY");
            break;
            case 5:System.out.println("THURSDAY");
            break;
            case 6:System.out.println("FRIDAY");
            break;
            case 7:System.out.println("SATURDAY");
            break;
        }
    }
}
 
Share this answer
 
The answer will be

import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int month = in.nextInt();
        int day = in.nextInt();
        int year = in.nextInt();
        Calendar c = Calendar.getInstance();
        c.set(Calendar.YEAR, year);
        c.set(Calendar.MONTH, month-1);
        c.set(Calendar.DAY_OF_MONTH, day);
        int day_of_week = c.get(Calendar.DAY_OF_WEEK); 
        String[] days = {"SUNDAY","MONDAY", "TUESDAY", "WEDNESDAY", "THURSDAY", "FRIDAY", "SATURDAY"};
        System.out.print(days[day_of_week-1]);
    }
}
 
Share this answer
 
Comments
CHill60 3-Apr-17 18:17pm    
In what way is this different to Solution 3? Don't do this

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900