Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Display Names of Weekdays in Calendar Year Using Java



There are 5 weekdays in a week, which are Monday, Tuesday, Wednesday, Thursday and Friday. The two remaining days i.e. Saturday and Sunday make up the weekend. In this article, we will learn how to write a Java program to display name of the weekdays in a calendar year.

Using the DateFormatSymbols Class

The Java standard library's java.text package contains the DateFormatSymbols class which provides methods for retrieving and setting the following date and time symbols ?

  • Month and day names and abbreviations

  • Day of the week names and abbreviations

  • Era names

  • AM/PM strings

  • Time Zone Names and Abbreviations

DateFormatSymbols by default uses the locale-specific symbols for the current default Locale. However, you can create an instance of DateFormatSymbols with a different Locale or custom symbol arrays for any of the above fields. We will make use of the getWeekdays() method of this class which returns an array of weekday names.

Example

A Java program that displays name of the weekdays in a calendar year is given below ?

import java.text.DateFormatSymbols;

public class Weekdays {
   public static void main(String[] args) {
      DateFormatSymbols symbols = new DateFormatSymbols();
      String[] weekdays = symbols.getWeekdays();
      for (int i = 2; i <= 6; i++) {
         System.out.println(weekdays[i]);
      }
   }
}

When you execute the code, it will produce the following result ?

Monday
Tuesday
Wednesday
Thursday
Friday

Using Calendar Class

In this approach, we first initialize the Calendar class and then, iterate through the days of the week from Monday to Friday using a for loop. For each day, we set the Calendar object's day of week and retrieve the full name of that day in the default locale.

The Java standard library's java.util package contains the Calendar class which offers methods for retrieving or setting various fields of a date or time, such as year, month, day, hour, minute, second and millisecond.

Example

In this Java program, we use the Calendar class to display name of the weekdays.

import java.util.Calendar;
import java.util.Locale;

public class Weekdays {
   public static void main(String[] args) {
      Calendar calendar = Calendar.getInstance();
      for (int i = Calendar.MONDAY; i <= Calendar.FRIDAY; i++) {
         calendar.set(Calendar.DAY_OF_WEEK, i);
         String name = calendar.getDisplayName(
            Calendar.DAY_OF_WEEK, Calendar.LONG, Locale.getDefault());
            System.out.println(name);
      }
   }
}

When you execute the code, it will produce the following result ?

Monday
Tuesday
Wednesday
Thursday
Friday

Using DayOfWeek Enum

In this approach, iterate through the days of the week using the DayOfWeek enum and print the full name of each day in the default locale, but only for the first five days, which is Monday to Friday. Here, we use the getDisplayName() method with TextStyle.FULL to get the full name of each day.

Example

Let's see the practical implementation ?

import java.time.DayOfWeek;
import java.time.format.TextStyle;
import java.util.Locale;

public class Weekdays {
   public static void main(String[] args) {
      int c=0;
      for (DayOfWeek day : DayOfWeek.values()) {
         if(c<5)
         {
            String name = day.getDisplayName(TextStyle.FULL, Locale.getDefault());
            System.out.println(name);
            c++;
         }
      }
   }
}

When you execute the code, it will produce the following result ?

Monday
Tuesday
Wednesday
Thursday
Friday
Updated on: 2024-09-30T15:56:38+05:30

666 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements