java date day of week

java

To get the day of the week for a java.util.Date object, you can use the get method of the Calendar class. Here is an example:

import java.util.Calendar;
import java.util.Date;

public class Main {
  public static void main(String[] args) {
    Date date = new Date();
    Calendar cal = Calendar.getInstance();
    cal.setTime(date);
    int dayOfWeek = cal.get(Calendar.DAY_OF_WEEK);
    System.out.println(dayOfWeek);
  }
}

This will print the day of the week as an integer, where 1 is Sunday, 2 is Monday, and so on. If you want to get the name of the day of the week, you can use the getDisplayName method of the Calendar class:

import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

public class Main {
  public static void main(String[] args) {
    Date date = new Date();
    Calendar cal = Calendar.getInstance();
    cal.setTime(date);
    String dayOfWeek = cal.getDisplayName(Calendar.DAY_OF_WEEK, Calendar.LONG, Locale.getDefault());
    System.out.println(dayOfWeek);
  }
}

This will print the name of the day of the week in the default locale.

Note: If you are using Java 8 or later, you can use the classes of the java.time package, such as LocalDate, to work with dates and times. These classes provide more powerful and simpler-to-use alternatives to the Date and Calendar classes.