Display Day Name of Week using Java Calendar

Java 8Object Oriented ProgrammingProgramming

For using Calendar class, import the following package.

import java.util.Calendar;

Using the Calendar class, create an object.

Calendar calendar = Calendar.getInstance();

Now, create a string array of the day names.

String[] days = new String[] { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" };

Display the day name.

days[calendar.get(Calendar.DAY_OF_WEEK) - 1]

The following is the final example.

Example

 Live Demo

import java.util.Calendar;
public class Demo {
   public static void main(String[] args) {
      Calendar calendar = Calendar.getInstance();
      System.out.println("Day: " + (calendar.get(Calendar.DATE)));
      System.out.println("Month: " + (calendar.get(Calendar.MONTH) + 1));
      System.out.println("Year: " + (calendar.get(Calendar.YEAR)));
      String[] days = new String[] { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" };
      System.out.println("Current day = " + days[calendar.get(Calendar.DAY_OF_WEEK) - 1]);
   }
}

Output

Day: 19
Month: 11
Year: 2018
Current day = Monday
raja
Published on 17-Dec-2018 14:36:16
Advertisements