Date value: when entered, 00/00/0000 saves as 11/30/0002

When I enter 00/00/0000 into the date range filter (before and from the date) on the web page, it is saved as 11/30/0002.

I try to use setLenient(false)but no luck. Here is the piece of code. Hope it helps. I am including a webpage code that takes values ​​and code from bean support.

XHTML Page Code:

<h:panelGroup layout="block" styleClass="myWorkDate">
                <h:commandLink action="#{myFollowupBean.startRangeFilter}" id="lnkDateRange" value="#{myFollowupBean.rangeFilter.display}" />
            </h:panelGroup>

MyFollowUpBean.java: Bean file

The code in the backup bean. A dialog box appears for the date range filter. When I enter zero, it saves as 11/30/0002 and processes the request instead of giving an error RangeFilterVO - model class for dates

 public void startRangeFilter() {
                    Command saveCommand = new Command() {

                        @Override
                        public String execute(Object returnObject) {
                            RangeFilterVO vo = (RangeFilterVO)returnObject;
                            rangeFilter.setFromDate(vo.getFromDate());
                            rangeFilter.setToDate(vo.getToDate());
                            searchAll();
                            fetch();

                            return null;
                        }
                    };

                    CalendarRangeFilterDialog dialog = new CalendarRangeFilterDialog();

                    dialog.setWidth(300);
                    try {
                        RangeFilterVO param = (RangeFilterVO) BeanUtils
                                .cloneBean(rangeFilter);
                        dialog.setVo(param);

                    } catch (IllegalAccessException | InstantiationException
                            | InvocationTargetException | NoSuchMethodException e) {
                    }
                    DialogFrame.displayDialog(dialog, saveCommand);

                }


    DateTimeConverter.java:

public static String getDatePattern(){
DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.SHORT,       FacesUtils.getLocale());
String datePattern = ((SimpleDateFormat) dateFormat).toPattern();
Date myDate;
try {
    if(myDate == dateFormat.parse(datePattern)){
        if (datePattern.indexOf("MM") == -1){
            datePattern = StringUtils.replace(datePattern, "M", "MM");
        }
        if (datePattern.indexOf("dd") == -1){
            datePattern = StringUtils.replace(datePattern, "d", "dd");
        }
        if (datePattern.indexOf("yyyy") == -1){
            datePattern = StringUtils.replace(datePattern, "yy", "yyyy");
        }   
    }
} catch (ParseException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

return datePattern;
}

Can someone explain me more about this?

+7
source share
1

, . .

" 0"; 1 /CE 1 .. , 0000 1 ..

" 0" 1 .. : 2 ..

, " 0" : 30 .

, 00/00/0000 30 2 .. , , .. "30.11.2000".

, , dateFormat.setLenient(false) dateFormat.parse(); , , .

+8

Source: https://habr.com/ru/post/1531019/


All Articles