【发布时间】:2021-03-19 07:16:11
【问题描述】:
屏幕格式应该是:
"dd-MM-yyyy HH:mm"
到现在为止
try
{
boolean correct = false;
while(!correct)
{
Scanner in = new Scanner(System.in);
System.out.print("Date: ");
String ans = in.next();
if(ans.matches("dd-MM-yyyy HH:mm"))
{
correct = true;
date = ans;
}
else{
System.out.println("Please use this format (dd-MM-yyyy HH:mm)");
}
}
}catch (InputMismatchException e)
{
System.out.println("--Please Enter an String--");
}
验证(检查)用户这样输入的最佳方法是什么?
【问题讨论】:
-
这个已经讲过很多次了。了解
DateTimeFormatter和LocalDateTime课程。陷阱DateTimeParseException。
标签: java datetime-format