java age from date

http‮.www//:s‬lautturi.com
java age from date

To calculate the age from a date of birth in Java, you can use the Period class of the java.time package.

The Period class represents a duration in terms of years, months, and days, and it can be used to calculate the difference between two LocalDate objects.

Here is an example of how you can calculate the age from a date of birth in Java:

import java.time.LocalDate;
import java.time.Period;
import java.time.format.DateTimeFormatter;

public class Main {
  public static void main(String[] args) {
    // Parse the date of birth from a string
    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy");
    LocalDate dateOfBirth = LocalDate.parse("01/01/1970", formatter);

    // Calculate the age from the date of birth
    LocalDate currentDate = LocalDate.now();
    Period age = Period.between(dateOfBirth, currentDate);

    // Print the age in years, months, and days
    System.out.printf("Age: %d years, %d months, %d days",
                      age.getYears(), age.getMonths(), age.getDays());
  }
}

In this example, the dateOfBirth variable is a LocalDate object that represents the date of birth, which is parsed from a string using the ofPattern method of the DateTimeFormatter class.

The currentDate variable is a LocalDate object that represents the current date, which is obtained using the now method of the LocalDate class.

The Period.between method calculates the difference between the dateOfBirth and currentDate variables, and it returns a Period object that represents the age in years, months, and days.

Finally, the getYears, getMonths, and getDays methods of the Period class are used to extract the years, months, and days from the age object, and they are printed using the printf method.

It is important to note that the Period class only represents a duration in terms of years, months, and days, and it does not take into account the number of days in each month or the leap years.

Therefore, the age calculated by the Period class may not be completely accurate, especially for dates that are far in the past or in the future.

If you need a more accurate calculation of the age, you can use the Duration class of the java.time package, which represents a duration in terms of seconds and nanoseconds.

Here is an example of how you can use the Duration class to calculate the age in Java:

import java.time.Duration;
import java.time.Instant;

public class Main {
  public static void main(String[] args) {
    // Parse the date of birth from a string
    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy");
    LocalDate dateOfBirth = LocalDate.parse("01/01/1970", formatter);

    // Calculate the age from the date of birth
    LocalDate birthDate = LocalDate.of(1990, 8, 15);
    LocalDate currentDate = LocalDate.now();
    Duration age = Duration.between(birthDate.atStartOfDay(), currentDate.atStartOfDay());
    System.out.println("Age in years: " + age.toDays() / 365);
 }
}
Created Time:2017-11-01 22:29:55  Author:lautturi