Java code to create date from given day of week, week of month, month and year as input. Example- if the iputs are as below:
day-Monday, month-july, week-1, year-2018,
then output should be-02/07/2018.
Below is the code used:
System.out.println("Enter a year,month,week,day:");
int year = Integer.parseInt(obj.nextLine());
int month = Integer.parseInt(obj.nextLine());
int week = Integer.parseInt(obj.nextLine());
String day = obj.nextLine();
String date;
SimpleDateFormat dateFormat = new SimpleDateFormat ("yyyy/MM/dd");
Calendar cal = Calendar.getInstance();
cal.set(Calendar.YEAR, year); // set the year
cal.set(Calendar.MONTH, month-1); // set the month
cal.set(Calendar.WEEK_OF_MONTH, week);
//***error in the below line********
cal.set(Calendar.DAY_OF_WEEK,day);
date=dateFormat.format(cal.getTime());
System.out.println("Result:" +date);
The marked line won’t compile. Why not? How should I fix it?