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

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



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
Updated on: 2020-06-27T08:47:20+05:30

483 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements