Get the day of week for a particular date in Java

Java 8Object Oriented ProgrammingProgramming

To get the day of week for a particular date in Java, use the Calendar.DAY_OF_WEEK constant.

Let us set a date first.

Calendar one = new GregorianCalendar(2010, Calendar.JULY, 10);

Since, we have used the Calendar as well as GregorianCalendar classes, therefore at first, import the following packages.

import java.util.Calendar;
import java.util.GregorianCalendar;

Now, we will find the day of week.

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

The following is an example

Example

 Live Demo

import java.util.Calendar;
import java.util.GregorianCalendar;
public class Demo {
   public static void main(String[] argv) throws Exception {
      Calendar one = new GregorianCalendar(2010, Calendar.JULY, 10);
      int day = one.get(Calendar.DAY_OF_WEEK);
      System.out.println(day);
      Calendar two = new GregorianCalendar(2011, Calendar.AUGUST, 9);
      day = two.get(Calendar.DAY_OF_WEEK);
       System.out.println(day);
      Calendar three = new GregorianCalendar(2017, Calendar.OCTOBER, 22);
      day = three.get(Calendar.DAY_OF_WEEK);
      System.out.println(day);
      Calendar four = new GregorianCalendar(2018, Calendar.DECEMBER, 30);
      day = four.get(Calendar.DAY_OF_WEEK);
      System.out.println(day);
   }
}

Output

7
3
1
1
raja
Published on 19-Dec-2018 17:53:46
Advertisements