1

We need to change the date format of the default openxava date(javaSript) component. default format is MM/dd/yy, and we need to change it to MM/dd/yyyy.

With the below link we can change the format of list view with implementing IFormatter interface. but in this conversation not clearly mention how to change the format of the date selection component.

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

Please help me to fix this issue...

user3496599
  • 1,207
  • 2
  • 12
  • 28

1 Answers1

1

To change the way a date, or any other type, is parsed and formatted you have to define a formatter for that type. To define a formatter edit the editors.xml file and add an entry like this:

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

You have to write YourDateFormatter that implements IFormatter. For example, the default formatter for date is this:

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

}
javierpaniza
  • 677
  • 4
  • 10
  • @javiepaniza Thanks for the your reply. I want to change the date format of the java script component as well. with this it is not going to change the format of the java script component. – user3496599 May 24 '16 at 07:49
  • Hi user, about the JavaScript formatter put your question the OpenXava forum, I'll answer you there. – javierpaniza May 27 '16 at 12:52