Java get day of the week as an int using DayOfWeek


Get Day of the week as Int Java Example

To get the day of the week as an int, make use of the getValue() from the DayOfWeek class from Java Date & Time API (java.time),



Code Snippet:
//Example 1
LocalDate localDateNow = LocalDate.now();
DayOfWeek dayOfWeek = localDateNow.getDayOfWeek();
int dayOfWeekAsInt = dayOfWeek.getValue();
System.out.println("Day of week for "+localDateNow+" as an int: " + dayOfWeekAsInt);


//Example 2
LocalDate localDate20200101 = LocalDate.of(2020,01,01);
DayOfWeek dayOfWeekFor20200101 = localDate20200101.getDayOfWeek();
int dayOfWeekAsIntFor20200101 = dayOfWeekFor20200101.getValue();
System.out.println("Day of week for "+localDate20200101+" as an int: " + dayOfWeekAsIntFor20200101);


Day of the week for 2022-05-14 as an int: 6
Day of the week for 2020-01-01 as an int: 3



Advertisements
Try Out Code2care Dev Tools:

Advertisements

Advertisements
Code2care is an initiative to publish and share varied knowledge in programming and technical areas gathered during day-to-day learnings and development activities.

Students and software developers can leverage this portal to find solutions to their various queries without re-inventing the wheel by referring to our easy to understand posts. Technical posts might include learnings, tutorials, trouble-shooting steps, video tutorials, code snippets, how-to, blogs, articles, etc.

🎉 We are celebrating the 10th years of Code2care! Thank you for all your support!

We strongly support Gender Equality & Diversity.