Is there a way to check if a java Date
object is Monday? I see you can with a Calendar
object, but date? I'm also using US-eastern date and time if that changes indexing of monday
Get the Day of Week using LocalDate (Java 8)now(); DayOfWeek dayOfWeek = today. getDayOfWeek(); System. out. println("Day of the Week :: " + dayOfWeek); System.
DayOfWeek is an enum representing the 7 days of the week - Monday, Tuesday, Wednesday, Thursday, Friday, Saturday and Sunday. In addition to the textual enum name, each day-of-week has an int value. The int value follows the ISO-8601 standard, from 1 (Monday) to 7 (Sunday).
You just need to write the method, getDay, which returns the day on that date. For example, if you are given the date, August 14th 2017, the method should return MONDAY as the day on that date.
Checking a Weekend using LocalDateget(ChronoField. DAY_OF_WEEK) method returns an integer value in the range of 1 to 7. Each integer value represents a different weekday. 1 represents Monday, and so on 6 represents Saturday and 7 represents Sunday.
Something like this will work:
Calendar cal = Calendar.getInstance();
cal.setTime(theDate);
boolean monday = cal.get(Calendar.DAY_OF_WEEK) == Calendar.MONDAY;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With