-6

I am a beginner in java.
I'm trying to create a program which calculates age of a person. How can I get birthday date input from user?
screenshot

Andreas
  • 154,647
  • 11
  • 152
  • 247
  • i have just started working in java. kindly tell me a simple code – Farhan Rao Nov 29 '16 at 17:56
  • 3
    Start with some introductory tutorials on Java. Getting input from the user is generally covered there. – David Nov 29 '16 at 17:59
  • 2
    Did you *bother* to look at the javadoc of [`LocalDate`](https://docs.oracle.com/javase/8/docs/api/java/time/LocalDate.html), and look at the [methods](https://docs.oracle.com/javase/8/docs/api/java/time/LocalDate.html#method.summary) it has? Down-vote for lack of research. – Andreas Nov 29 '16 at 18:05
  • @Andreas brother as i siad i am a beginner. and this is my first question on this site. – Farhan Rao Nov 29 '16 at 18:12
  • 1
    Please go through some tutorials, and also go through what kind of questions must be posted and how to format them. – Rishabh Nov 29 '16 at 18:15
  • @David thats not just about input.i know how to input integer char etc but i am unable to input date of birth which gives me correct current age – Farhan Rao Nov 29 '16 at 18:15
  • @FarhanRao Being a beginner doesn't exempt you from reading the documentation and/or searching the web, aka doing some research. Rather the exact opposite: As a student of programming, that's what you're *supposed* to be doing. Not asking for the cheat sheet here. – Andreas Nov 29 '16 at 18:15
  • @FarhanRao: *Why* are you unable to? What have you tried and where are you stuck? If you know how to get input from the user, then what's stopping you from getting input from the user? You have to put in at least *some* effort. – David Nov 29 '16 at 18:18
  • i am just trying.you should encourage someone not to discourage.this is my first question.it takes time – Farhan Rao Nov 29 '16 at 18:19
  • @Andreas i am not asking for cheat sheet. i want just simple code which can be understandable to a early beginner – Farhan Rao Nov 29 '16 at 18:25
  • @FarhanRao You found *nothing* helpful when searching the web for `java date input`? I find that hard to believe. – Andreas Nov 29 '16 at 21:12
  • Duplicate: [How can I get the user input in Java?](http://stackoverflow.com/q/5287538/642706) – Basil Bourque Nov 30 '16 at 00:47

1 Answers1

0

Is you are using Java 8, you can use LocalDate to parse the date. This will parse dates in the format YYYY-MM-DD...

import java.time.LocalDate;
import java.util.Scanner;

public class ParseDate {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        System.out.print("Enter date> ");
        String input = scanner.nextLine();

        LocalDate parsed = LocalDate.parse(input);

        System.out.println("You entered: " + parsed);

        scanner.close();
    }
}

Sample output:

Enter date> 1978-12-23
You entered: 1978-12-23
BretC
  • 4,141
  • 13
  • 22