Java Period get time span from today until your birthday

Description

Java Period get time span from today until your birthday

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

public class Main {

  public static void main(String[] args) {

    LocalDate today = LocalDate.now();
    LocalDate birthday = LocalDate.of(1976, Month.APRIL, 1);

    LocalDate nextBDay = birthday.withYear(today.getYear());

    // If your birthday has occurred this year already, add 1 to the year.
    if (nextBDay.isBefore(today) || nextBDay.isEqual(today)) {
      nextBDay = nextBDay.plusYears(1);/*from w ww . j ava 2 s.co  m*/
    }

    Period p = Period.between(today, nextBDay);
    long p2 = ChronoUnit.DAYS.between(today, nextBDay);
    System.out.println("There are " + p.getMonths() + " months, and " + p.getDays()
        + " days until your next birthday. (" + p2 + " total)");
  }
}



PreviousNext

Related