Get the Day of the Week from Today's Date in Java

Java 8Object Oriented ProgrammingProgramming

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

Firstly, let us get the current date.

java.util.Date utilDate = new java.util.Date();
java.sql.Date dt = new java.sql.Date(utilDate.getTime());

Now, using GregorianCalendar, set the time.

java.util.GregorianCalendar cal = new java.util.GregorianCalendar();
cal.setTime(dt);

The last step would display the day of the week as shown in the following example.

Example

 Live Demo

import java.text.ParseException;
public class Demo {
   public static void main(String[] args) throws ParseException {
      java.util.Date utilDate = new java.util.Date();
      java.sql.Date dt = new java.sql.Date(utilDate.getTime());
      System.out.println("Today's date: "+dt);
      java.util.GregorianCalendar cal = new java.util.GregorianCalendar();
      cal.setTime(dt);
      // Getting the day of the week
      System.out.println("Day of the week: "+cal.get(java.util.Calendar.DAY_OF_WEEK));
   }
}

Output

Today's date: 2018-11-19
Day of the week: 2
raja
Published on 17-Dec-2018 14:23:35
Advertisements