Showing posts with label Date Time. Show all posts
Showing posts with label Date Time. Show all posts

Monday, September 7, 2026

How to Get the Day of the Week from a Date in Java

There are many ways to find the day name from a given date. But here I have used simple one.Really its so easy but sometimes we hang on it .We never found the solutions.Today its happening with me, because I always prefer  less search on Google.

Below the code for finding the day name from an input date.import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class DayFinder {
    public static void main(String[] args) {
        String inputDate = "01/08/2012";
        SimpleDateFormat format1 = new SimpleDateFormat("dd/MM/yyyy");

        try {
            Date dt1 = format1.parse(inputDate);
            DateFormat format2 = new SimpleDateFormat("EEEE");
            String finalDay = format2.format(dt1);
            System.out.println("My Day is: " + finalDay);
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }
}


Output:- Wednesday

Explanation :- 

First I prepare a date by using SimpleDateFormat . Then Prepare a DateFormat by using that before SimpleDateFormat(format1).

EEEE is the date Format Suffix for Day.More
 

Modern Java Solution (java.time)

In modern Java, this is simpler, safer, and does not need Date or SimpleDateFormat:

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

public class DayFinderModern {
    public static void main(String[] args) {
        String inputDate = "01/08/2012";
        
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy");
        LocalDate date = LocalDate.parse(inputDate, formatter);

        // Get day of the week directly
        System.out.println("My Day is: " + date.getDayOfWeek()); 
        // Or format it as full text ("Wednesday"):
        // date.format(DateTimeFormatter.ofPattern("EEEE"))
    }