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 回答