Java日期中的年份可以用字母进行解析
我已经编写了我的NotLenientDateEditor,它扩展了CustomDateEditorJava日期中的年份可以用字母进行解析,java,spring,Java,Spring,我已经编写了我的NotLenientDateEditor,它扩展了CustomDateEditor public NotLenientDateEditor(DateFormat dateFormat, boolean allowEmpty, int exactDateLength) { super(dateFormat, allowEmpty, exactDateLength); dateFormat.setLenient(false); 现在我需要再添加一个功能,因为启用ja
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')
throwsjava.lang.IllegalArgumentException:无法分析日期:它不是精确的10个字符长
。无法使用DateUtils类吗?