How to Display Name of the Weekdays in Java



In this tutorial, we can display the names of the weekdays using classes like DateFormatSymbols and SimpleDateFormat in Java. The DateFormatSymbols class provides access to symbols like weekday names, while SimpleDateFormat helps in formatting dates and time based on specific patterns. The following are the ways to display the name of the weekdays

  • Using DateFormatSymbols

  • Using SimpleDateFormat with Locale

Display Name of Weekdays Using DateFormatSymbols

DateFormatSymbols: The DateFormatSymbols class provides language-sensitive information about the formatting and parsing of dates and times, including month names.

getWeekdays(): A method in DateFormatSymbols that returns an array of weekday names starting with an empty entry for Sunday.

Example

The following example displays the names of the weekdays in a short form with the help of the DateFormatSymbols().getWeekdays() method of the DateFormatSymbols class

import java.text.SimpleDateFormat;
import java.text.DateFormatSymbols;

public class Main {
   public static void main(String[] args) {
      String[] weekdays = new DateFormatSymbols().getWeekdays();
      
      for (int i = 2; i < (weekdays.length-1); i++) {
         String weekday = weekdays[i];
         System.out.println("weekday = " + weekday);
      }
   }
}

Output

weekday = Monday
weekday = Tuesday
weekday = Wednesday
weekday = Thursday
weekday = Friday

Display Name of Weekdays Using SimpleDateFormat With Locale

Locale: A class that represents a specific geographical, political, or cultural region. It affects how certain information (like month names or date formats) is displayed.

SimpleDateFormat("EEE", Locale.US): A SimpleDateFormat pattern that formats the date into a shortened weekday name (e.g., "Mon" for Monday).

SimpleDateFormat("EEE", Locale.US) is used to format the current date into a short weekday name. The pattern "EEE" formats the date as a three-letter abbreviation for the day of the week (e.g., "Mon" for Monday). The Locale.US ensures that the formatting follows the English format used in the United States.

Example

The following is another example of example of display name of the weekdays

import java.text.Format;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.Calendar;

public class Main {
   public static void main(String[] argv) throws Exception {
      Date d = new Date();
      SimpleDateFormat dateFormat = new SimpleDateFormat("EEE", Locale.US); 
      String str = dateFormat.format(d);
      System.out.println(str);
   }
}

Output

Mon
Advertisements