Javatpoint Logo
Javatpoint Logo

How to Get Day Name from Date in Java?

In this section, we will create a Java program to get the day name from date.

There are the following classes that are used when we deal with date and time in Java.

  • Calendar Class: The class belongs to java.util package. It extends the Object class and implements the Serializable, cloneable, Comparable<Calendar> interfaces. It provides the methods for converting specific instance in time and a set of calendar fields (such as YEAR, MONTH, DAY, HOUR, DAY_OF MONTH, etc.)
  • Date Class: The class belongs to the util package. It represents a specific instant in time, with millisecond precision. It allows the interpretation of dates as year, month, day, hour, minute, and second values. Besides this formatting and parsing of date strings is also allowed.
  • LocalDate Class: It belongs to time package. It represents the date according to ISO-8601 calendar system, such as 2002-08-24. It is an immutable date-time object that represents a date in yyyy-mm-dd format. Also, we can access the other date fields, such as day-of-year, day-of-week and week-of-year. For example, the value "22nd December 2003" can be stored in a LocalDate. Note that the class does not store or represent a time or time-zone.
  • SimpleDateFormat Class: The class belongs to java.text package. It extends the DateFormat class. SimpleDateFormatis a concrete class for formatting and parsing dates in a locale-sensitive manner.
  • Format: Format is an abstract base class for formatting locale-sensitive information such as dates, messages, and numbers.
  • DateFormat: The class belongs to java.text package. It extends the Format class.

There are the following ways to get day name from date:

  • Using SimpleDateFormat Class
  • Using DateFormatSymbols Class
  • Using GregorianCalendar Class

Java Program to Get Day Name from Date

Get Day Name of the Current Date

Using SimpleDateFormat Class

The following Java program demonstrate how to get today' date day name.

In the following program, we have created an instance of the Calendar class and invoked the getInstance() method of the class. It returns a Calendar object whose calendar fields have been initialized with the current date and time. It may produce all calendar fields.

We have parsed a date format in the SimpleDateFormat class constructor. In the print statement, we have called the format() method of the SimpleDateFormat class that formats the given Date into a date/time string and appends the result to the given StringBuffer. Inside the format() method we have called the getTime() method of the Calendar class. The method returns a Date object representing this Calendar's time value.

Format class is a base class that contains other format classes such as DateFormat and SimpleDatefFormat class. In the Format class constructor, we have passed EEEE that represents the day name in week. In the next statement, we have invoked the format() method and parsed the object of the Date class. The method formats an object to produce a string. At last, print the string that shows the day name.

GetDayNameExample1.java

Output:

Today's date: 05-10-2021
Day Name: Tuesday

Let's see another program for the same.

Using DateFormatSymbols Class

In the following program, we have create a constructor of the DateFormatSymbols() class and invoked the getWeekdays() method that returns the string of weekdays. We have store that weekdays in an array named dayNames[].

After that, we have created an instance of the Calendar class and invoked the getInstance() method of the class. It returns a Calendar object whose calendar fields have been initialized with the current date and time. It may produce all calendar fields.

In the print statement, we have invoked the get() method of the Calendar class and passed the field DAY_OF_WEEK as an argument. It obtains the number for get and set indicating the day of the week.

Therefore, it prints the day of the week.

GetDayNameExample2.java

Output:

Today is Tuesday

Note: Your output may differ as shown above.

Using GregorianCalendar Class

Java GregorianCalendar Class belong to java.util package. It is a concreate subclass of the Calendar class. It provides the standard calendar system.

In the following example, we have created an instance of the Date class and parsed a GregorianCalendar class object as an argument. In the GregorianCalendar class constructor we passed year, month and day of the month of which we want to know day.

The getTime() method returns the number of milliseconds since January 1, 1970, 00:00:00 GMT represented by this Date object. The Date class constructor allocates a Date object and initializes it so that it represents the time at which it was allocated, measured to the nearest millisecond.

We have created a method dayName() that determines the day name of a specified date. Inside the method, we have created a constructor of the SimpleDateFormat class and parsed the pattern for day name i.e. EEEE.

GetDayNameExample3.java

Output:

This date was on: Thursday

Let's see another Java program for the same.

GetDayNameExample4.java

Output:

Sat, 12 Jun 2021






Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA