0

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();
    }
 
}
tactikal
  • 19
  • 5
  • 1
    In other words your actual question is "*how to parse March 2, 1999 to LocalDate?*". Take a look at https://stackoverflow.com/a/4216767 (part starting from "Java 8 update") – Pshemo Aug 01 '21 at 15:25
  • And in the link from @Pshemo do scroll down to the *Java 8 update* since you are wisely using `LocalDate` introduced from Java 8. – Ole V.V. Aug 01 '21 at 16:24

2 Answers2

5

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();
    }

}
geobreze
  • 2,274
  • 1
  • 10
  • 15
5

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.

Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110