
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Get Day of Week as String in Java
Some applications that work on calendars require a day name to be displayed for features like scheduling tasks, events or reminders. For this purpose, Java provides various built-in classes and methods including LocalDate, Calendar and SimpleDateFormat.
In this article, we will learn how to use these classes and methods in Java programs to find the day name of a week for a given date.
Using LocalDate Class
In this approach, we first find current date using LocalDate class and using its built-in method named getDayOfWeek(), we create a DayOfWeek Enum which can be converted to String to display day name.
LocalDate is a class of java.time package. It is used to display a date without a time-zone. The DayOfWeek in Java is an Enum which represents all the 7 days of the week, from Monday to Sunday.
Example
In the following Java program, we are using LocalDate class and DayOfWeek Enum to find day name of week.
import java.time.DayOfWeek; import java.time.LocalDate; public class Demo { public static void main(String[] args) { // getting current date LocalDate currentDate = LocalDate.now(); System.out.println("Current Date = "+currentDate); // getting day of the week DayOfWeek day = currentDate.getDayOfWeek(); int weekVal = day.getValue(); String weekName = day.name(); System.out.println("Week Number = " + weekVal); System.out.println("Week Name = " + weekName); } }
Following will be the output of above code ?
Current Date = 2019-04-12 Week Number = 5 Week Name = FRIDAY
Using Calendar Class
This is another way to find current day of the week and print its name. Here, we create an instance of the Calendar class which is available in the java.util package to get current date and time. Then, initialize a String array with the name of week days. In the end, use get() method by passing Calendar.DAY_OF_WEEK as a parameter value to retrieve the day of the week.
Example
The following Java program shows how to get day of week as String using Calendar class.
import java.util.Calendar; public class Demo { public static void main(String[] args) { // creating calendar instance Calendar calendar = Calendar.getInstance(); // defining string array with week days String[] days = new String[]{"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"}; // getting the today day name String weekName = days[calendar.get(Calendar.DAY_OF_WEEK) - 1]; System.out.println("Day of the week is:: " + weekName); } }
Output obtained as shown below ?
Day of the week is:: Monday
Using SimpleDateFormat Class
In this approach, we follow the below steps ?
- Create a Date object to represent the current date and time.
- Now, create a SimpleDateFormat object with the pattern EEEE, which represents the full name of the day of the week.
- Then, use format() method of the SimpleDateFormat class to get current day of the week.
Example
In this Java program, we find day of week using SimpleDateFormat class.
import java.text.SimpleDateFormat; import java.util.Date; public class Demo { public static void main(String[] args) { // fetching today's date Date todayDate = new Date(); // Printing today date System.out.println("Current Date: " + todayDate); SimpleDateFormat simpleDateFormat = new SimpleDateFormat("EEEE"); // getting day of the week String weekName = simpleDateFormat.format(todayDate); System.out.println("Day of the week is:: " + weekName); } }
Output of the above code is as follows ?
Current Date: Mon Aug 05 11:40:40 GMT 2024 Day of the week is:: Monday