I was trying to do a question where I have to print the name of the weekday. At first, i tried using this method:
public static String findDay(int month, int day, int year){
Calendar calendar = Calendar.getInstance();
calendar.set(year, month, day);
Date date = calendar.getTime();
System.out.println(date);
return new SimpleDateFormat("EEEE").format(date).toUpperCase();
}
In this, I am getting the output as THURSDAY.
However, when I used the below code, I am getting the correct answer that is MONDAY
class Result {
public static String findDay(int month, int day, int year) {
/*Calendar calendar = Calendar.getInstance();
calendar.set(year, month, day);
Date date = calendar.getTime();
return new SimpleDateFormat("EEEE").format(date).toUpperCase();*/
String final_day = "";
String input_date = day + "/" + month + "/" + year;
SimpleDateFormat format1 = new SimpleDateFormat("dd/MM/yyyy");
try
{
DateFormat format2 = new SimpleDateFormat("EEEE");
final_day = format2.format(format1.parse(input_date));
}
catch(Exception e){}
return final_day.toUpperCase();
}
}
Can anyone tell me how is it possible?