LocalDate today = LocalDate.now();
System.out.println(DateTimeFormatter.ofPattern("dd-MM-yyyy");
// Outputs "12-09-2019"
Source: https://howtodoinjava.com/java/date-time/localdate-format-example/
After some fiddling with Spring Boot integration testing this week, I came across a scenario where some of my tests would fail. They only failed when running all tests in one go; running the tests isolated did work.
Apparently, the test data in your H2 database is not cleared after or before running the test. When decorating your test with @Transactional
, all changes are rolled back after the test has run.
This is very simple (if you're using at least JDK 8).
public int calculateAge(
LocalDate birthDate,
LocalDate currentDate) {
// validate inputs ...
return Period.between(birthDate, currentDate).getYears();
}