Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

When I enter 00/00/0000 in the date range filter(to and from date) in the webpage, it is saved as 11/30/0002.

I am trying to use setLenient(false) but no luck. Here is part of the code. hope it helps. I am including the webpage code which takes the values and the code from backing bean.

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

Code in the backing bean. The dialog box pops up 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 anyone please explain me more about this?

like image 923
user2988851 Avatar asked Mar 10 '14 17:03

user2988851


People also ask

What is TZ date format?

The T is just a literal to separate the date from the time, and the Z means “zero hour offset” also known as “Zulu time” (UTC). If your strings always have a “Z” you can use: SimpleDateFormat format = new SimpleDateFormat( “yyyy-MM-dd'T'HH:mm:ss).

What is the date 0?

new Date(0) is January 1, 1970 in UTC.


1 Answers

This is an artifact of putting zeroes into fields that are one-based. It auto-corrects to the end of the previous unit.

In the common era, there is traditionally no "year 0"; the year before 1 AD/CE was 1 BC/BCE. So your entered year 0000 becomes 1 BCE.

Then "month 0" of the year 1 BCE becomes the last month of the previous year: December, 2 BCE.

Similarly, "day 0" of December becomes the last day of the previous month: November 30.

Hence, 00/00/0000 is corrected to November 30, 2 BCE. Your format doesn't have a field for the era, so your display loses the fact that it's a BCE year in the result "11/30/0002".

If you prefer the parser reject values outside their normal range, just call dateFormat.setLenient(false) before calling dateFormat.parse(); be advised that the parser will then raise an exception when given such dates.

like image 131
Mark Reed Avatar answered Sep 30 '22 11:09

Mark Reed