How to change the date of a Calendar instance in Java?

Viewed 3112

I have a class DayOfWeek which takes a date from the user and prints the day of the week on that date.

For example the user will enter his date as: (with the format MM DD YYYY)

07 22 2016

and the output should be

FRIDAY

This is my class:

public class DayOfWeek{
    public static void main(String[] args){
        System.out.println("Enter the date (Format: MM DD YYYY) :");
        Scanner sc = new Scanner(System.in);
        String month = sc.next();
        String day = sc.next();
        String year = sc.next();
        int m = Integer.parseInt(month);
        int d = Integer.parseInt(day);
        int y = Integer.parseInt(year);
        Calendar cal = Calendar.getInstance();
        Date date = new Date(y, m, d);
        cal.setTime(date);
        int dayOfWeek = cal.get(Calendar.DAY_OF_WEEK);
        SimpleDateFormat sdf = new SimpleDateFormat("EEEE");
        System.out.println((sdf.format(dayOfWeek)).toUpperCase());
    }
}

However when I enter the date stated above (07 22 2016), I don't get the required output (FRIDAY). Instead the output is the current day of week on my system. I am totally stuck on what the problem is.

3 Answers
Related