1

デフォルトの openxava date(javaSript) コンポーネントの日付形式を変更する必要があります。デフォルトの形式は MM/dd/yy で、MM/dd/yyyy に変更する必要があります。

以下のリンクを使用すると、IFormatter インターフェイスを実装してリスト ビューの形式を変更できます。ただし、この会話では、日付選択コンポーネントの形式を変更する方法について明確に言及していません。

https://sourceforge.net/p/openxava/discussion/419690/thread/40db1436/

この問題を解決するのを手伝ってください...

4

1 に答える 1

1

日付やその他の型を解析してフォーマットする方法を変更するには、その型のフォーマッタを定義する必要があります。フォーマッタを定義するには、editors.xml ファイルを編集し、次のようなエントリを追加します。

<editor name="DateCalendar" url="dateCalendarEditor.jsp">
    <formatter class="com.yourcompany.yourapp..formatters.YourDateFormatter" />
    <for-type type="java.util.Date" />
</editor>

IFormatter を実装する YourDateFormatter を作成する必要があります。たとえば、日付のデフォルトのフォーマッタは次のとおりです。

package org.openxava.formatters;

import java.text.*;

import javax.servlet.http.*;

import org.openxava.util.*;

/**
 * Date formatter with multilocale support. <p>
 * 
 * Although it does some refinement in Spanish case, it support formatting
 * on locale basis.<br>
 *  
 * @author Javier Paniza
 */

public class DateFormatter implements IFormatter {

    private static DateFormat extendedDateFormat = new SimpleDateFormat("dd/MM/yyyy"); // Only for some locales like "es" and "pl"

    private static DateFormat [] extendedDateFormats = { // Only for some locales like "es", "fr", "ca" and "pl"
        new SimpleDateFormat("dd/MM/yy"), 
        new SimpleDateFormat("ddMMyy"),
        new SimpleDateFormat("dd.MM.yy")                
    };

    public String format(HttpServletRequest request, Object date) {
        if (date == null) return "";
        if (Dates.getYear((java.util.Date)date) < 2) return "";
        return getDateFormat().format(date);
    }

    public Object parse(HttpServletRequest request, String string) throws ParseException {
        if (Is.emptyString(string)) return null;                
        if (isExtendedFormat()) { 
            if (string.indexOf('-') >= 0) { // SimpleDateFormat does not work well with -
                string = Strings.change(string, "-", "/");
            }       
        }
        DateFormat [] dateFormats = getDateFormats(); 
        for (int i=0; i<dateFormats.length; i++) {
            try {
                dateFormats[i].setLenient(false);
                return dateFormats[i].parseObject(string);
            }
            catch (ParseException ex) {
            }                       
        }
        throw new ParseException(XavaResources.getString("bad_date_format",string),-1);
    }

    private boolean isExtendedFormat() {
        return "es".equals(Locales.getCurrent().getLanguage()) ||
            "ca".equals(Locales.getCurrent().getLanguage()) || 
            "pl".equals(Locales.getCurrent().getLanguage()) ||
            "fr".equals(Locales.getCurrent().getLanguage());
    }

    private DateFormat getDateFormat() {
        if (isExtendedFormat()) return extendedDateFormat;
        return DateFormat.getDateInstance(DateFormat.SHORT, Locales.getCurrent());      
    }

    private DateFormat[] getDateFormats() {
        if (isExtendedFormat()) return extendedDateFormats;
        return new DateFormat [] { getDateFormat() };
    }

}
于 2016-05-23T11:11:04.940 に答える