0

CustomDateEditor を拡張する NotLenientDateEditor を作成しました

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

javascript を有効にすると日付 12.1.20asa21 を解析できないため、もう 1 つの機能を追加する必要がありますが、javascript を無効にすると、この日付は 12.1.20 として解析できます。

年に文字が含まれていることを解析しない機能を追加する方法を教えてください。

4

2 に答える 2

0

With this implementation seems to work:

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

}

Class with main method:

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
    }

}

Notes:

How do you instantiate NotLenientDateEditor? Which format do you set for DateFormat? Note the single digit for month and 7 for the exactDateLength constructor argument.

于 2012-07-31T12:59:58.887 に答える
0

これは機能しますが、ファイル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);
}
于 2012-08-05T15:38:10.140 に答える