Find Day of Week using SimpleDateFormat class in Java

Given the day, month, and year, the task is to find the corresponding day of the week using SimpleDateFormat class in Java.

Examples

Input: 11-08-2020

Output: Tuesday

Explanation: The day corresponding to the date 11-08-2020 is tuesday.



Input: 17-08-2006

Output: Thursday

Explanation: The day corresponding to the date 17-08-2006 is thursday.

Approach: 

  • Input the date, month, and year from the user as integer type.
  • Check if the date, month, and year are in the required range. If not, raise an error message.
  • Convert the input into Date type using SimpleDateFormat class.
  • Format the date to the corresponding day of the week using SimpleDateFormat class.
  • Print the corresponding day of the week.

Note: If you want the full name of the day (ex: Sunday, Monday), use “EEEE”. If you want the shorter version of the name of the day (ex: Sun, Mon), use “EE”.

Below is the implementation of the above approach:

Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program for the above approach
import java.util.Date;
import java.text.SimpleDateFormat;
import java.text.ParseException;
  
public class GFG {
  
    public void findDay(int day, int month, int year)
    {
        String dayOfWeek = "";
        boolean wrongDate = false;
  
        if (day < 1 || day > 31) {
            dayOfWeek += "Give day in range. ";
            wrongDate = true;
        }
  
        if (month < 1 || month > 12) {
            dayOfWeek += "Give month in range. ";
            wrongDate = true;
        }
  
        if (year <= 0) {
            wrongDate = true;
            dayOfWeek += "Give year in range.";
        }
  
        if (!wrongDate) {
  
            SimpleDateFormat dateFormatter
                = new SimpleDateFormat("dd-MM-yyyy");
            String dateString
                = day + "-" + month + "-" + year;
  
            try {
                // Parse the String representation of date
                // to Date
                Date date = dateFormatter.parse(dateString);
                dayOfWeek
                    = "Day of week on " + dateString + " : "
                      + new SimpleDateFormat("EEEE").format(
                            date);
            }
            catch (ParseException e) {
                e.printStackTrace();
            }
        }
  
        System.out.println(dayOfWeek);
    }
  
    // Driver Code  
    public static void main(String arg[])
    {
  
        GFG gfg = new GFG();
        gfg.findDay(17, 8, 2006);
    }
}

chevron_right


Output

Day of week on 17-8-2006 : Thursday

Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up


If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.