Get Day Number of Week in Java

Java 8Object Oriented ProgrammingProgramming

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
raja
Published on 17-Dec-2018 14:30:50
Advertisements