2014-02-28 32 views
0

在我的java程序中,一个java变量String inputDate接受输入表单用户。我只强制用户输入(dd/MM/yyyy)格式的日期,因为我的其他模块仅依赖于该格式。这是我到目前为止所尝试的:在java中执行特定日期格式

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; 
} 
+1

好的。你面临的问题是什么? – RaviH

+0

你解析了一个,并且没有任何异常被捕获(如果它不能解析它会抛出一个异常)并将它存储在一个变量中..解析方法将返回! –

回答

1

使用myDateFormat.setLenient(false)

SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy"); 
    sdf.setLenient(false); 
    try{ 
     sdf.parse(incomingDateString); 
     // if you get here, the incomingDateString is valid 
    }catch(ParseException ex){ 
     // if you get here, the incomingDateString is invalid 
    } 
0

据我的理解,你想要做的是确保输入的日期符合格式。

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; 
    } 
} 
1

这样可不行,试试这个

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; 
} 
+1

默认情况下,'parse()'方法的宽容是'false'(默认情况下''parse',接受'ParsePosition'的另一种风格是'true')。 – Isaac

+0

尝试SimpleDateFormat df = new SimpleDateFormat(“dd/MM/yyyy”); System.out.println(df.isLenient()); –

+0

你是对的。 : - |我把它收回。 – Isaac

0

您可以使用正则表达式来检查给定的输入是格式或不试试这个:

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(); 
    } 
} 
+0

ok,d在'String regex =“^ \\ d {2}/\\ d {2}/\\ d {4} $”;'是Integer。 (!!!) ,但78/78/1256也被确定为有效日期。 – KNU