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.
There are the following ways to get day name from date:
Java Program to Get Day Name from DateGet Day Name of the Current DateUsing 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 ClassIn 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 ClassJava 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 Next TopicZigzag Array in Java |
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India