You can use a variable (e.g. boolean valid
in the code below) to track whether the input is valid. If the input is not valid, you need to loop back. I prefer using do-while loop, which guarantees to execute its body at least once, for such a scenario. I also recommend you handle exception for invalid input.
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.Scanner;
public class Main {
public static void main(String[] args) throws ParseException {
Scanner sc = new Scanner(System.in);
SimpleDateFormat parser = new SimpleDateFormat("d/M/yyyy", Locale.ENGLISH);
Date today = new Date();
boolean valid;
do {
valid = true;
System.out.print("Purchase date DD/MM/YYYY: ");
String strDate = sc.nextLine();
Date date = null;
try {
date = parser.parse(strDate);
if (date.after(today)) {
System.out.println("The date should not be a future date. Please try again.");
valid = false;
}
} catch (ParseException e) {
System.out.println("Invalid date/format. Please try again.");
valid = false;
}
} while (!valid);
System.out.println("Thank you! Processing ...");
}
}
A sample run:
Purchase date DD/MM/YYYY: a
Invalid date/format. Please try again.
Purchase date DD/MM/YYYY: 14/3/2021
The date should not be a future date. Please try again.
Purchase date DD/MM/YYYY: 12/2/2020
Thank you! Processing ...
Note that the java.util
date-time API and their formatting API, SimpleDateFormat
are outdated and error-prone. It is recommended to stop using them completely and switch to the modern date-time API*.
Using the modern date-time API:
import java.time.DateTimeException;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Locale;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
DateTimeFormatter parser = DateTimeFormatter.ofPattern("d/M/u", Locale.ENGLISH);
LocalDate today = LocalDate.now();
boolean valid;
do {
valid = true;
System.out.print("Purchase date DD/MM/YYYY: ");
String strDate = sc.nextLine();
LocalDate date = null;
try {
date = LocalDate.parse(strDate, parser);
if (date.isAfter(today)) {
System.out.println("The date should not be a future date. Please try again.");
valid = false;
}
} catch (DateTimeException e) {
System.out.println("Invalid date/format. Please try again.");
valid = false;
}
} while (!valid);
System.out.println("Thank you! Processing ...");
}
}
Learn more about the modern date-time API from Trail: Date Time.
Note:
- You have wrongly used
D
instead of d
. D
is used for Day in year, not for Day in month.
- You have wrongly used
Y
instead of y
. Y
is used for a Week year, not for year.
* For any reason, if you have to stick to Java 6 or Java 7, you can use ThreeTen-Backport which backports most of the java.time functionality to Java 6 & 7. If you are working for an Android project and your Android API level is still not compliant with Java-8, check Java 8+ APIs available through desugaring and How to use ThreeTenABP in Android Project.