Write a Java program to get a day of the week of a specific date


The code you provided is a Java program that uses the java.util package to get the day of the week for the current date and print it to the console. Here is a breakdown of the code:

  • The java.util package is imported at the beginning of the code.
  • The DayWeek_SpecificDate class is defined, which contains the main method.
  • Within the main method, the Calendar.getInstance() method is called to get an instance of the Calendar class, which represents the current date and time.
  • The get() method is called on the cal variable with an argument of Calendar.DAY_OF_WEEK, which returns the day of the week as an integer value, where 1 is Sunday and 7 is Saturday. This value is stored in the dow variable.
  • The System.out.println() method is called to print the string "Day of the Week : " concatenated with the value of the dow variable, which represents the day of the week for the current date.

Note that the Calendar class is a legacy class in Java that has been replaced by the newer java.time package since Java 8. You can use the java.time.DayOfWeek enum to get the day of the week in the newer API.

Source Code

import java.util.*;
public class DayWeek_SpecificDate
{
	public static void main(String []args)
	{
		Calendar cal = Calendar.getInstance();
		int dow = cal.get(Calendar.DAY_OF_WEEK);
		System.out.println("Day of the Week : " + dow);
	}
}

Output

Day of the Week : 7

Example Programs