In this source code example, we show you how to get the previous day using Java 8 LocalDate class.
Get Previous Day in Java Example
package com.ramesh.java8.datetime;
import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
import java.time.temporal.TemporalAdjusters;
/**
* Useful Java8DateUtiliy Methods
* @author javaguides.net
*
*/
public final class Java8DateUtility {
/**
* Get Previous Day.
* @param localDate
* @return
*/
public static LocalDate getPreviousDay(LocalDate localDate) {
return localDate.minus(1, ChronoUnit.DAYS);
}
}
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 getPreviousDayTest() {
System.out.println("Get Previous Day :: " + Java8DateUtility.getPreviousDay(LocalDate.now()));
}
}
Run the JUnit test cases will print the output:
Get Previous Day :: 2018-07-20