In this source code example, we show you how to get the day of the week in the Java using LocalDate class.
Get the day of the week in Java Example
package com.ramesh.java8.datetime;
import java.time.DayOfWeek;
import java.time.LocalDate;
/**
* Useful Java8DateUtiliy Methods
* @author javaguides.net
*
*/
public final class Java8DateUtility {
/**
* Get day of the week.
* @param localDate
* @return
*/
public static DayOfWeek getDayOfWeek(LocalDate localDate) {
DayOfWeek day = localDate.getDayOfWeek();
return day;
}
}
JUnit test case
package com.ramesh.java8.datetime;
import java.time.LocalDate;
import org.junit.Test;
/**
* JUnit test cases for Java8DateUtiliy Methods
* @author javaguides.net
*
*/
public class Java8DateUtilityTest {
@Test
public void getDayOfWeekTest() {
System.out.println("Get Day Of Week :: " + Java8DateUtility.getDayOfWeek(LocalDate.now()));
}
}
Run the JUnit test cases will print the output:
Get Day Of Week :: SATURDAY