18 August 2016

How to get difference between 2 dates ( LocalDate ) in years , months or days ?

Solution for :
Java 8 and above

STORY:

August in my month of Java 8 where I try practice various things that are new in Java 8 and I should know already. At some point , I tried to create person  with  birthday stored as LocalDate and get age in years , but I found it that there is no method on Duration to check that. I was wonder can I do it this using only Java 8 . As it turns out yes.

SOLUTION:
You need use ChronoUnit class that has a nice method to do this.
LocalDate birthday = LocalDate.of(1980,4,4);
ChronoUnit.YEARS.between(birthday,LocalDate.now()));

EXAMPLE: (To run this example you need copy these 2 classes and add junit and assertJ libraries )


Person class:
package dms.pastor.kb.java8.example;

import java.time.LocalDate;
import java.time.temporal.ChronoUnit;

/**
 * Author Dominik Symonowicz
 * Created 18/08/2016
 * WWW:    http://pastor.ovh.org
 * Github: https://github.com/pastorcmentarny
 * Google Play:    https://play.google.com/store/apps/developer?id=Dominik+Symonowicz
 * LinkedIn: uk.linkedin.com/pub/dominik-symonowicz/5a/706/981/
 */
public class Person {
    String name;
    LocalDate birthday;

    public Person(String name, LocalDate birthday) {
        this.name = name;
        this.birthday = birthday;
    }

    public String getName() {
        return name;
    }

    public int getAge(){
       return Math.toIntExact(ChronoUnit.YEARS.between(birthday,LocalDate.now()));
    }

    public int getAgeInMonths(){
        return Math.toIntExact(ChronoUnit.MONTHS.between(birthday,LocalDate.now()));
    }

    public int getAgeInDays(){
        return Math.toIntExact(ChronoUnit.DAYS.between(birthday,LocalDate.now()));
    }
}


PersonTest class:
package dms.pastor.kb.java8.example;

import org.junit.Before;
import org.junit.Test;

import java.time.LocalDate;

import static org.assertj.core.api.Assertions.assertThat;

/**
 * Author Dominik Symonowicz
 * Created 18/08/2016
 * WWW:    http://pastor.ovh.org
 * Github: https://github.com/pastorcmentarny
 * Google Play:    https://play.google.com/store/apps/developer?id=Dominik+Symonowicz
 * LinkedIn: uk.linkedin.com/pub/dominik-symonowicz/5a/706/981/
 */
public class PersonTest {
    Person person;

    @Before
    public void setup() {
        person = new Person("Dominik", LocalDate.now().minusYears(18).minusMonths(1).minusDays(1));
    }

    @Test
    public void shouldReturn18YearsTest() throws Exception {
        //When
        final int age = person.getAge();

        //Then
        assertThat(age).isEqualTo(18);
    }

    @Test
    public void shouldReturn217MonthsTest() throws Exception {
        //When
        final int age = person.getAgeInMonths();

        //Then
        assertThat(age).isEqualTo(217);
    }

    @Test
    public void shouldReturn28DaysTest() throws Exception {
        //Given
        Person toddler = new Person("Toddler",LocalDate.now().minusDays(28));

        //When
        final int age = toddler.getAgeInDays();

        //Then
        assertThat(age).isEqualTo(28);
    }
}



:) I hope, you found this useful