0

I was trying to do a question where I have to print the name of the weekday. At first, i tried using this method:

My program

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?

nisha
  • 1
  • 1
  • 1
    Probably time to change to using the [`java.time`](https://docs.oracle.com/javase/tutorial/datetime/TOC.html) API instead – MadProgrammer Feb 08 '22 at 06:21
  • What's your input? – MadProgrammer Feb 08 '22 at 06:22
  • Also, remember, "month" is zero indexed in the "old" date APIs (ie Jan == 0), so `findDay(1, 1, 2022)` isn't going to give you the expected result – MadProgrammer Feb 08 '22 at 06:24
  • both the ways are working fine. Just you need to consider "Month value is 0-based" and change the logic accordingly in your second block of code. – Pavan Feb 08 '22 at 06:27
  • *month - the value used to set the MONTH calendar field. Month value is 0-based. e.g., 0 for January.* – Scary Wombat Feb 08 '22 at 06:45
  • 1
    yes! it worked @MadProgrammer. I didn't consider the point that the month value starts with 0. That's why I was getting a different answer. Also, I would be glad to know which API should I use for further reference. – nisha Feb 08 '22 at 10:31
  • 2
    API: classes from the `java.time` package, like `LocalDate` (for year-month-day): `LocalDate.of(year, month, day).getDayOfWeek()` (this month is as usual, January is 1, but the `Month` enum can also be used) – user16320675 Feb 08 '22 at 17:08
  • `LocalDate.of(2022, 2, 7).getDayOfWeek()` gives `MONDAY` (the values mean February 7). – Ole V.V. Feb 09 '22 at 08:09
  • For your upcoming questions, please specify the arguments you pass to your method. Without them it’s impossible for us to verify whether `MONDAY` or `THURSDAY` is the correct return value. Even better, hard-code the values in your code example, for example using `calendar.set(2022, 2, 7);`. – Ole V.V. Feb 09 '22 at 08:14

0 Answers0