Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/362.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java日期中的年份可以用字母进行解析_Java_Spring - Fatal编程技术网

Java日期中的年份可以用字母进行解析

Java日期中的年份可以用字母进行解析,java,spring,Java,Spring,我已经编写了我的NotLenientDateEditor,它扩展了CustomDateEditor public NotLenientDateEditor(DateFormat dateFormat, boolean allowEmpty, int exactDateLength) { super(dateFormat, allowEmpty, exactDateLength); dateFormat.setLenient(false); 现在我需要再添加一个功能,因为启用ja

我已经编写了我的NotLenientDateEditor,它扩展了CustomDateEditor

public NotLenientDateEditor(DateFormat dateFormat, boolean allowEmpty, int exactDateLength) {
    super(dateFormat, allowEmpty, exactDateLength);
    dateFormat.setLenient(false);
现在我需要再添加一个功能,因为启用javascript时,日期12.1.20asa21不可解析,但禁用javascript时,此日期可解析为12.1.20


您能否帮助我,使用此实现如何添加不解析年份包含字母的功能

package net.orique.stackoverflow.question11740273;

import java.text.DateFormat;

import org.springframework.beans.propertyeditors.CustomDateEditor;

public class NotLenientDateEditor extends CustomDateEditor {

    public NotLenientDateEditor(DateFormat dateFormat, boolean allowEmpty,
            int exactDateLength) {
        super(dateFormat, allowEmpty, exactDateLength);
        dateFormat.setLenient(false);
    }

}
使用
main
方法初始化:

package net.orique.stackoverflow.question11740273;

import java.text.SimpleDateFormat;

public class Main {

    public static void main(String[] args) {
        SimpleDateFormat sdf = new SimpleDateFormat("dd.m.yy");
        NotLenientDateEditor n = new NotLenientDateEditor(sdf, false, 7);

        n.setAsText("12.1.20"); // Ok
        n.setAsText("12.1.20asa21"); // Throws java.lang.IllegalArgumentException
    }

}
注:


如何实例化NotLenientDateEditor?您为DateFormat设置了哪种格式?请注意
month
的单个数字和
exactDateLength
构造函数参数的
7

这是可行的,但它支持文件12.1.2a(它使用12.2.02,这是我的问题)。我用这种方式初始化NotLenientEditor

public void initBinder(WebDataBinder binder) {
    SimpleDateFormat format = new SimpleDateFormat("dd.MM.yyyy");
    NotLenientDateEdtor editor = new NotLenientDateEdtor(format, false, 10);
    binder.registerCustomEditor(Date.class, editor);
}

启用Javascript如何改变Java代码中发生的事情?你需要弄清楚这里到底发生了什么。(我怀疑区别在于,当禁用Javascript时,您不会得到即时验证。)我验证了两次。使用javascript时,我们在textarea中编写,而不是在java中的post请求之后。Javascript是可以的,但是为了稳定性,我也需要用java.ok验证,所以Javascript位实际上是不相关的。现在,您可以向我们展示一个简短但完整的程序,演示Java代码中的问题。我尝试在这个正则表达式中使用组件,但我不知道如何比较它,因为当我想使用matches(正则表达式)时,我有nullPointerException
editor.setText('12.1.2a')
throws
java.lang.IllegalArgumentException:无法分析日期:它不是精确的10个字符长
。无法使用DateUtils类吗?