【问题标题】:enforcing specific date format in java在java中强制执行特定的日期格式
【发布时间】:2014-02-28 11:23:35
【问题描述】:

在我的 java 程序中,一个 java 变量 String 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;
}

【问题讨论】:

  • 好的。您面临的问题是什么?
  • 你解析的一个并且没有捕获到异常(如果它不能解析它会抛出一个异常)并将它存储在一个变量中.. parse 方法将返回!

标签: java validation date


【解决方案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
    }

【讨论】:

    【解决方案2】:

    这个不行,试试这个

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

    【讨论】:

    • 对于parse() 方法,默认情况下宽恕是false(对于其他风格的parse,默认情况下是true,它接受ParsePosition)。
    • 尝试 SimpleDateFormat df = new SimpleDateFormat("dd/MM/yyyy");System.out.println(df.isLenient());
    【解决方案3】:

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

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

    【讨论】:

    • 好的,String regex = "^\\d{2}/\\d{2}/\\d{4}$"; 中的 d 代表整数。但 78/78/1256 也被识别为有效日期。(!!!)
    【解决方案4】:

    据我了解,您要做的是确保输入的日期与格式相符。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-13
      • 1970-01-01
      • 2017-10-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多