LoginSignup
2
4

More than 5 years have passed since last update.

【Java】LocalDateを使って年齢を計算する方法

Last updated at Posted at 2018-06-19

java.time.LocalDateを使って誕生日から現在の年齢を計算する方法。

BirthdayService.java

public int getAge(int year, int month, int day) {

    // 計算対象の誕生日
    LocalDate birthday = LocalDate.of(year, month, day);

    // 現在の年月日
    LocalDate today = LocalDate.now();

    long duration = ChronoUnit.YEARS.between(birthday, today);

    return (int)duration;
}

LocalDate (Java Platform SE 8)

2
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
4