I want to get back to the start of the loop if the catch happened, so the user can input the date in the right format.
System.out.print("Enter date (Format DD.MM.JJJJ)");
String date = eingabe.next();
SimpleDateFormat dateStandardFormat= new SimpleDateFormat("dd.mm.yyyy");
Date dateStandardFormat = null;
do {
try {
dateStandardFormat = dateStandardFormat.parse(datum);
} catch (java.text.ParseException e1) {
System.out.println("date invalid! Please enter the date in Format DD.MM.YYYY");
validity = true;
}
}while(!validity);
But if I get the throw, it brings me back in a perma loop. What Im doing wrong?
CodePudding user response:
You can use it like this:
boolean validity = false;
Scanner eingabe = new Scanner(System.in);
Date parsedDate = null;
SimpleDateFormat dateStandardFormat = new SimpleDateFormat("dd.mm.yyyy");
System.out.println("Enter date (Format DD.MM.JJJJ)");
do {
String date = eingabe.next();
try {
parsedDate = dateStandardFormat.parse(date);
validity = true;
} catch (Exception e) {
System.out.println("Date invalid! Please enter the date in Format DD.MM.YYYY");
}
} while(!validity);
System.out.println("Parsed date: " parsedDate.toString());
My output from this code:
Enter date (Format DD.MM.JJJJ)
Hello!
Date invalid! Please enter the date in Format DD.MM.YYYY
27.10.2019
Parsed date: Sun Jan 27 00:10:00 CET 2019
CodePudding user response:
To start, you can not have two objects declared with the same local variable name in the same method, for example:
SimpleDateFormat dateStandardFormat= new SimpleDateFormat("dd.mm.yyyy");
Date dateStandardFormat = null;
This is a 'no go' and won't even allow for compiling. Below would be good enough if you plan on using these troublesome classes:
SimpleDateFormat dateStandardFormat= new SimpleDateFormat("dd.mm.yyyy");
Date newDate = null;
If you look closely to the string date pattern used for SimpleDateFormat
you will notice the mm
within the pattern. This designation is for minutes not for months. You need to change this to MM
(uppercase M) so that the formatter knows you mean months otherwise you will just end up getting some obscure date that in no way relates to the date supplied by the User. The String pattern for the SimpleDateFormat constructor should be: dd.MM.yyyy
, for example:.
SimpleDateFormat dateStandardFormat= new SimpleDateFormat("dd.MM.yyyy");
And where does datum
come from? dateStandardFormat = dateStandardFormat.parse(datum);
. This should be: newDate = dateStandardFormat.parse(date);
.
SimpleDateFormat dateStandardFormat= new SimpleDateFormat("dd.MM.yyyy");
Date newDate = null;
boolean validity = false;
while (!validity) {
System.out.print("Enter date (Format: DD.MM.YYYY): -> ");
String strDate = eingabe.next();
try {
newDate = dateStandardFormat.parse(strDate);
validity = true;
}
catch (ParseException ex) {
System.out.println("Date invalid! (" strDate ") Please try again...");
}
}
System.out.println(newDate);
On A Side:
If you can, you try and avoid using the antique Date and SimpleDateFormat classes since they are well known for being troublesome. Instead use the LocalDate, LocalDateTime, and other classes from the java.time package and the DateFormatter / DateTimeFormatter classes (java 8 ). Here is an example:
Scanner eingabe = new Scanner(System.in);
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd.MM.yyyy");
LocalDate date = null;
String strDate = "";
boolean validity = false;
while (!validity) {
System.out.print("Enter date (format: DD.MM.YYYY): -> ");
strDate = eingabe.nextLine().trim();
if (strDate.equalsIgnoreCase("n")) {
strDate = "N/A";
break;
}
else if (strDate.isEmpty()) {
System.out.println("You must enter a date or enter 'n' for "
"\"N/A\"!" System.lineSeparator()
"Try again..." System.lineSeparator());
}
else {
try {
date = LocalDate.parse(strDate, formatter);
strDate = date.format(formatter);
validity = true;
}
catch (DateTimeParseException ex) {
System.out.println("Date invalid! (" strDate
") Please try again..." System.lineSeparator());
}
}
}
System.out.println("User Supplied: " strDate);
You may also notice that, knowing 2022 it is not a Leap Year but the User was to enter 31.02.2022
, the day value will be automatically corrected to the proper end of month: 28.02.2022
.