This Week I Learnt: week 37

Java: convert LocalDate to string

LocalDate today =;
// Outputs "12-09-2019"


Java: Spring Boot integration testing and rolling back the changes

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.


Java: calculating age

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();