Java

Java Calendar Days of Week

Java has different classes which allow us to extract the day, year, month, date, and time. We can also modify these fields like days after and before the specified day or date. The day of the week is acquired using the code interface of the LocaleDate and Calendar classes. Here, we will get the day of the week from the Java calendar class and obtain the day of the week from the simple LocaleDate methods. The calendar class field Day_Of_Week is used to retrieve the number of the day. Note that the object of the calendar class shouldn’t be shared by several threads.

Example 1:

Here, we first use the LocaleDate to get the name of the week with the specified date which has a different method from the calendar class. The getValue method of DayOfWeek class gets the integer value of the week number. Let’s have a Java program to explain the getValue() method of the DayOfWeek.


In the code, we first set the libraries to allow us to get the day of the week. We then declare the Java class to perform the main() method. Inside it, we create the LocalDate instance “dt” where we use the LocalDate.of() method to provide the locale date. Then, we set the “DOW” instance of the DayOfWeek class to find the day of the specified locale date using the from() method.  After that, we utilize the print statement to print the name of the day.

Next, we declare another instance which is “getVal” where the getValue() method is applied on the “DOW” instance to get the integer value of the day of the week. Once the integer value of the day is retrieved, the getVal instance shows the number of the day from the print statement.

On the specified date, “Monday” is the designated day of the week. Monday is the first day of the week, so the retrieved integer value is “1”.

Example 2:

The previous Java code of getting the day of the week is demonstrated which gets the name of the day along with its integer value which lies in the range of the week numbers. Now, the day of the week can also be fetched in the different format text styles of Java using the methods from the “time.format.TextStyle” package.


In the code, we set some required libraries to get the name of the day along with the different format styles. After that, we have a “DaysOfWeek2” Java class which encloses the main() method where the source code to get the day of the week is provided. To obtain the current date on our server, we first construct the “today” instance of the LocalDate using the now() method.

Then, we get the day of the week from the “today” instance using the getDayOfWeek() method. The day of the week value in the form of the integer is obtained through the use of the getValue method along with the “MyDayOfWeek” instance. Next, the weekday is then formatted using Java’s TextStyle. First, we show the FULL format day of the week and then the SHORT-format day of the week.

The current day of our system is “Wednesday” which is the third day of the week as the value is displayed, too. The full and short day of the week are also printed on the following output image:

Example 3:

Now, the calendar class is utilized to get the day of the week. The calendar.Day_of_Week is declared in the get() method and returns the day of the week from the given date. Consider the calendar class day of the week program:


In the code, we give a Calendar module from Java. Next, we create the Java class to implement the program inside this class’s main() method. We declare the calendar “cal” variable and initialize it with the getInstance() method to obtain the current date and time. Then, we employ the print message where we get the current time from the system and transform that time into the string type using the toString() method.

After that, we define another object which is “Day” where the “cal” object uses the get() method. The get() method takes the current day of the week from the calendar and prints the day of the week which occurs on the date of our system. Along with this, we also fetch the hours and minutes from the calendar class.

The day of the week in string and integer format for the provided date is shown in the following output along with the date and time of the system:

Example 4:

The calendar class is also used to fetch the first and last days of the week. The day can also be represented as a string value rather than a number value. The SimpleDateFormat is used to obtain the day as text.


In the code, we set the text module of Java to get the day as text. For this, we create the main() method inside the class of the Java program. Here, we decalre the “Date” object to get the date from the server. Then, we call the set() method where we set the calendar with the starting day of the week. After that, we define the “df” object where the format of the day and date is provided to the SimpleDateFormat(). The current date and day are then printed by getting the day and date from the getTime() method in the specified format. Next, we obtain the last day of the week using the for-loop. The for-loop iterates through a 0 to 6 value since the Java range of week begins with the 0 value. The last day with the date is also displayed after iteration.

The starting day of the week in Java is “SUNDAY” which is obtained in the output screen. The last day of the week is Saturday which is also executed in the following:

Conclusion

The day of the week is obtained by performing the different operations which are explored in this Java document. The first two programs are implemented with the LocaleDate method where the day of the week is obtained from the specified date. The next program uses the calendar class to operate for getting the day of the week. The calendar class method is also used for the first day and the last day of the week. Moreover, the day of the week is also formatted as a text-based day.

About the author

Saeed Raza

Hello geeks! I am here to guide you about your tech-related issues. My expertise revolves around Linux, Databases & Programming. Additionally, I am practicing law in Pakistan. Cheers to all of you.