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




Top Hashtags:

Advertisements

Advertisements




Advertisements