tactikal
tactikal

Reputation: 19

How do I calculate the age from dob using the format of month day, yyyy in java? ex. (March 2, 1999)

The current code below uses the yyyy-mm-dd format. However the input should look like this (March 2, 1999). How do I calculate it or convert it?

public class CalculateAgeJava8 {
 
    public static void main(String[] args) {
        System.out.print("Please enter date of birth: ");
        Scanner scanner = new Scanner(System.in);
        String input = scanner.nextLine();
        scanner.close();
 
        LocalDate dob = LocalDate.parse(input);
        System.out.println("Age is:" + getAge(dob));
    }
 
    public static int getAge(LocalDate dob) {
        LocalDate curDate = LocalDate.now();
        return Period.between(dob, curDate).getYears();
    }
 
}

Upvotes: 0

Views: 305

Answers (2)

Arvind Kumar Avinash
Arvind Kumar Avinash

Reputation: 79395

You need a DateTimeFormatter to parse the date string in the specified format e.g.

import java.time.LocalDate;
import java.time.Period;
import java.time.format.DateTimeFormatter;
import java.util.Locale;
import java.util.Scanner;

public class CalculateAgeJava8 {

    public static void main(String[] args) {
        System.out.print("Please enter date of birth in the format MMMM d, yyyy (e.g. March 2, 1999): ");
        Scanner scanner = new Scanner(System.in);
        String input = scanner.nextLine();
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("MMMM d, u", Locale.ENGLISH);

        LocalDate dob = LocalDate.parse(input, dtf);
        System.out.println("Age is:" + getAge(dob));
    }

    public static int getAge(LocalDate dob) {
        LocalDate curDate = LocalDate.now();
        return Period.between(dob, curDate).getYears();
    }    
}

A sample run:

Please enter date of birth in the format MMMM d, yyyy (e.g. March 2, 1999): March 2, 1999
Age is:22

Here, you can use y instead of u but I prefer u to y.

Learn more about the modern Date-Time API from Trail: Date Time.

Note: Never close Scanner(System.in) as it also closes System.in and there is no way, other than restarting the JVM, to open it again which means if some other part of your application is using System.in, it will result in an exception.

Upvotes: 5

geobreze
geobreze

Reputation: 2422

You can use java.time.format.DateTimeFormatter for this case.

class CalculateAgeJava8 {

    public static void main(String[] args) {
        System.out.print("Please enter date of birth: ");
        Scanner scanner = new Scanner(System.in);
        String input = scanner.nextLine();
        scanner.close();

        DateTimeFormatter fm = DateTimeFormatter.ofPattern("MMMM d, yyyy", Locale.ENGLISH);

        LocalDate dob = LocalDate.parse(input, fm);
        System.out.println(dob);
        System.out.println("Age is:" + getAge(dob));
    }

    public static int getAge(LocalDate dob) {
        LocalDate curDate = LocalDate.now();
        return Period.between(dob, curDate).getYears();
    }

}

Upvotes: 5

Related Questions