在java中强制使用特定日期格式
在我的java程序中,java变量在java中强制使用特定日期格式,java,validation,date,Java,Validation,Date,在我的java程序中,java变量字符串inputDate接受用户的输入。我希望强制用户仅以(dd/MM/yyyy)格式输入日期,因为我的其他模块仅依赖于该格式。以下是我迄今为止所做的尝试: public class InputQuery { private static String FLIGHT_DATE; public String getFLIGHT_DATE() { return FLIGHT_DATE; } public void setFLIGHT_DATE() {
字符串inputDate
接受用户的输入。我希望强制用户仅以(dd/MM/yyyy)格式输入日期,因为我的其他模块仅依赖于该格式。以下是我迄今为止所做的尝试:
public class InputQuery {
private static String FLIGHT_DATE;
public String getFLIGHT_DATE() {
return FLIGHT_DATE;
}
public void setFLIGHT_DATE() {
boolean invalid = true;
Scanner sc = null;
while(invalid){
System.out.println("Enter FLIGHT_DATE(dd/MM/yyy) :");
sc = new Scanner(System.in);
FLIGHT_DATE = sc.nextLine();
if( (invalid = isValidDate(FLIGHT_DATE)) ) {
System.out.println("For Feb 21,2016 enter 21/02/2016");
}
}
sc.close();
}
private boolean isValidDate(String flight_DATE) {
SimpleDateFormat myDateFormat = new SimpleDateFormat("dd/MM/yyyy");
if( flightDate.parse(flight_DATE)){
System.out.println("accepted OK");
return true;
}
return false;
}
使用myDateFormat.setLenient(false)
据我所知,您要做的是确保输入的日期符合格式
parse(String)
方法不返回布尔值,也从不返回null
。如果成功,则返回一个日期;如果失败,则抛出异常。方法是:
private boolean isValidDate(String flight_DATE) {
SimpleDateFormat myDateFormat = new SimpleDateFormat("dd/MM/yyyy");
try {
myDateFormat.parse(flight_DATE);
return true;
} catch (ParseException ex) {
// You may want to print the exception here, or do something else with it
return false;
}
}
这个不行,试试这个
private boolean isValidDate(String flightDate) {
SimpleDateFormat myDateFormat = new SimpleDateFormat("dd/MM/yyyy");
myDateFormat.setLenient(false);
try {
myDateFormat.parse(flightDate);
} catch (ParseException e) {
return false;
}
System.out.println("accepted OK");
return true;
}
您可以使用正则表达式检查给定输入是否为格式,或者不尝试以下操作:
public class DateValidator {
public static void main(String[] args){
Scanner scanner = new Scanner(System.in);
String flightDate = null;
boolean isDateValid = false;
while(!isDateValid){
System.out.print("Enter FLIGHT_DATE(dd/MM/yyy) :");
flightDate = scanner.nextLine().trim();
isDateValid = isDateValid(flightDate);
if(!isDateValid){
System.out.println("Wrong Format.");
}
}
System.out.println("continue.");
}
public static boolean isDateValid(String flightDate){
String regex = "^\\d{2}/\\d{2}/\\d{4}$";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(flightDate);
return matcher.find();
}
}
好啊您所面临的问题是什么?一个您进行解析但未捕获异常的问题(如果无法解析,则抛出异常)并将其存储在变量中。。解析方法将返回!默认情况下,
parse()
方法的宽容度为false
(默认情况下,true
用于另一种类型的parse
,它接受ParsePosition
)。请尝试SimpleDataFormat df=new SimpleDataFormat(“dd/MM/yyyy”);System.out.println(df.isLenient());好的,d inString regex=“^\\d{2}/\\d{2}/\\d{4}$”代码>表示整数。但78/78/1256也被标识为有效日期。(!!!)
public class DateValidator {
public static void main(String[] args){
Scanner scanner = new Scanner(System.in);
String flightDate = null;
boolean isDateValid = false;
while(!isDateValid){
System.out.print("Enter FLIGHT_DATE(dd/MM/yyy) :");
flightDate = scanner.nextLine().trim();
isDateValid = isDateValid(flightDate);
if(!isDateValid){
System.out.println("Wrong Format.");
}
}
System.out.println("continue.");
}
public static boolean isDateValid(String flightDate){
String regex = "^\\d{2}/\\d{2}/\\d{4}$";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(flightDate);
return matcher.find();
}
}