What is wrong with the below code? It gives wrong day for any date of the year.
import java.util.Scanner;
import java.util.Calendar;
public class Solution {
public static String getDay(String d, String m, String y) {
String[] days = {"SUNDAY", "MONDAY", "TUESDAY", "WEDNESDAY", "THURSDAY", "FRIDAY", "SATURDAY"};
Calendar c = Calendar.getInstance();
c.set(Integer.parseInt(y), Integer.parseInt(m), Integer.parseInt(d));
return days[c.get(Calendar.DAY_OF_WEEK) - 1];
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String m = in.next();
String d = in.next();
String y = in.next();
System.out.println(getDay(d, m, y));
}
}
See the documentation for the Calendar
class: https://docs.oracle.com/javase/8/docs/api/java/util/Calendar.html#set-int-int-int-
The value for month is 0-indexed, so if you provide 3
as the month value, it is interpreted as "April".