Get Day Number of Week in Java


To get the day of the week, use Calendar.DAY_OF_WEEK.

Firstly, declare a calendar object and get the current date and time.

Calendar calendar = Calendar.getInstance();
System.out.println(calendar.getTime().toString());

Now, fetch the day of the week in an integer variable.

int day = calendar.get(Calendar.DAY_OF_WEEK);

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(calendar.getTime().toString());
      int day = calendar.get(Calendar.DAY_OF_WEEK);
      System.out.println("Day: " + day);
      int hour = calendar.get(Calendar.HOUR_OF_DAY);
      System.out.println("Hour: " + hour);
      int minute = calendar.get(Calendar.MINUTE);
      System.out.println("Minute: " + minute);
   }
}

Output

Mon Nov 19 10:11:41 UTC 2018
Day: 2
Hour: 10
Minute: 11

Updated on: 27-Jun-2020

10K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements