In this source code example, we show you how to get the first day of the Month in the Java using LocalDate class.
Get the first day of the Month in Java
package com.ramesh.java8.datetime;
import java.time.LocalDate;
import java.time.temporal.TemporalAdjusters;
/**
* Useful Java8DateUtiliy Methods
* @author javaguides.net
*
*/
public final class Java8DateUtility {
/**
* Get first day of the Month.
* @return LocalDate
*/
public static LocalDate getFirstDayOfMonth() {
LocalDate firstDayOfMonth = LocalDate.now().with(TemporalAdjusters.firstDayOfMonth());
return firstDayOfMonth;
}
}
JUnit test case
package com.ramesh.java8.datetime;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import java.time.LocalDate;
import org.junit.Test;
/**
* JUnit test cases for Java8DateUtiliy Methods
* @author javaguides.net
*
*/
public class Java8DateUtilityTest {
@Test
public void getFirstDayOfMonthTest() {
System.out.println("Get First day of Month :: " + Java8DateUtility.getFirstDayOfMonth());
}
}
Run the JUnit test cases will print the output:
Get First day of Month :: 2018-07-01