w3resource
Java Programming Exercies

Java DateTime, Calendar Exercises: Calculate your age

Java DateTime, Calendar: Exercise-32 with Solution

Write a Java program to calculate your age.

Sample Solution:

Java Code:

import java.time.*;
import java.util.*;

public class Exercise32 {  
   public static void main(String[] args)
    {
        // date of birth
        LocalDate pdate = LocalDate.of(1989, 04, 11);
        // current date
        LocalDate now = LocalDate.now();
        // difference between current date and date of birth
        Period diff = Period.between(pdate, now);
 
     System.out.printf("\nI am  %d years, %d months and %d days old.\n\n", 
                    diff.getYears(), diff.getMonths(), diff.getDays());
   }
}

Sample Output:

I am  28 years, 2 months and 10 days old.

N.B.: The result may varry for your system date and time.

Flowchart:

Flowchart: Java DateTime, Calendar Exercises - Calculate your age

Java Code Editor:

Improve this sample solution and post your code through Disqus

Previous: Write a Java program to compute the difference between two dates (Hours, minutes, milli, seconds and nano).
Next: Write a Java program to get the next and previous Friday.

What is the difficulty level of this exercise?



New Content: Composer: Dependency manager for PHP, R Programming