-1

I want to parse a user input to a date with the following code:

public class AlterBerechnen {
public static String date;
public static void main(String[] args) {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    System.out.println("Wann wurden sie geboren (DD.MM.YYYY) :");
    SimpleDateFormat dateformat = new SimpleDateFormat("dd.MM.YYYY");
    Date geb = null;
    try {
        try {
            date = br.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
        geb = dateformat.parse(date);
    } catch (ParseException e) {
        e.printStackTrace();
        System.out.println("Hier ist eine fehlangabe erfolgt!!!!");
    }
    System.out.println(geb);
}}

But if I enter 17.12.1993 I get Mon Jan 04 00:00:00 CET 1993. Please help me, I read two hours in the internet, its the resolution I found, but it didn't work. Best regards Nils

Nils
  • 3
  • 3
  • @Tunaki thanks a lot, you are my hero of this evening. case sensitive, i like that.... – Nils Feb 16 '15 at 22:10
  • If you have erroneous input you still print null. You should check for null before printing geb in your last line. – barq Feb 16 '15 at 22:22
  • @Nils Please search StackOverflow before posting. You would have found hundreds of existing Questions & Answers on this topic. By studying the examples given, and perhaps copy/pasting such code, you would have discovered your trivial mistake with uppercase-lowercase `yyyy`. Ex: [this](http://stackoverflow.com/q/7542378/642706), [this](http://stackoverflow.com/q/25225318/642706), [this](http://stackoverflow.com/q/7669817/642706), [this](http://stackoverflow.com/q/22464594/642706). – Basil Bourque Feb 17 '15 at 01:56

2 Answers2

1

See SimpleDateFormat javadoc: there's a nice table explaining what every character in the format means. "Y" means week year, while "y" means year.

Tunaki
  • 132,869
  • 46
  • 340
  • 423
0

Try: SimpleDateFormat("dd.MM.yyyy")

barq
  • 3,681
  • 4
  • 26
  • 39