java date day of week
javaTo 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.
Other Article on Tag java
- - after and before in java date
- - am pm java date format
- - apk minecraft java edition
- - as been blocked by CORS policy Response to preflight request doesn't pass access control check It does not have HTTP ok status
- - bubble sort in java
- - Can I compile and run a spring boot jar from inside another spring boot application
- - convert java date to localdate
- - design patterns in java
- - encrypt by java and decrypt by node
- - exception handling in java