All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

How To Get Day From Date In Java

Last Updated : Mar 11, 2024

How To Get Day From Date In Java

In this article we will show you the solution of how to get day from date in java, the java.time package allows you to determine the day of a week from the specified date in Java. For a given date, you can build a LocalDate object, or you can acquire the current date by calling LocalDate.now().

The DayOfWeek enum encoding the day of a week for the date that has been entered is returned by the getDayOfWeek() function.

The DayOfWeek enum's toString() method can then be used to print out the day of a week.

The day of the week can also be displayed as a string by formatting the LocalDate object using a DateTimeFormatter.

By specifying the desired pattern, the day of the week can be shown in a different format. Now let's talk about the Java concept of getting the day from the date.

Step By Step Guide On How To Get Day From Date In Java :-

import java.text.Format;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Calendar;
public class TkGetDayName
{
public static void main(String args[]) throws Exception
{
Calendar cal = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
System.out.println("Today's date: "+sdf.format(cal.getTime()));
Format f = new SimpleDateFormat("EEEE");
String str = f.format(new Date());
System.out.println("Day Name: "+str);
}
}
  1. You may see how we wrote a java programme here. Java code creates a class called "TkGetDayName" that uses the language's built-in date and calendar classes to retrieve the current date & the name of the day.
  2. In order to operate with dates and calendars, the code first imports the required Java classes.
  3. The "main" method, which acts as the program's entry point, is specified in the "TkGetDayName" class.
  4. The "getInstance" function of the "Calendar" class creates an instance while retrieving the current time and date.
  5. The desired date format—in this case, "dd-MM-yyyy"—is used to build a "SimpleDateFormat" object.
  6. According to this format, the day should be shown first, then the month, and finally the year, with a hyphen separating each element.
  7. The "getTime" method of the calendar object is used to retrieve the current date, which is then formatted by the SimpleDateFormat object.
  8. In this scenario, the format "EEEE" is used to build a "Format" object with the appropriate day name format.
  9. The whole name of the day of the week, such as "Monday," "Tuesday," etc., must be presented, according to this format.
  10. The day name is obtained from the current date by creating a "Date" object and utilising the Format object's "format" technique to access the "Date" object.
  11. The "println" method of the "system.out" object is used for printing the name of the present day.
  12. As soon as the programme ends, the user sees the results.

Conclusion :-

As a result, we were able to understand how to retrieve day from date in Java.

Using Java's built-in date & calendar classes, this Java code fetches the current date as well as the name of the day of the week, formats them in accordance with the specified format, and shows the results to the user.

I hope this article on how to get day from date in java helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Pragati

Experienced coding content writer who enjoys breaking down complex concepts in programming languages like Java, Python, C, and C++. Over three years of experience producing interesting and relevant content for a variety of entities. Committed to providing concise and easy-to-understand articles that assist readers in easily understanding technology and industry trends in the world of coding and software development.

Follow Pragati On Linkedin 🡪