primefaces calendar component is getting Date by default. Can I get only month and year without dates.
How to get only month and year
Utilized this feature from the Primefaces Documentation:
Another handy event is the viewChange that is fired when month and year changes. An instance of org.primefaces.event.DateViewChangeEvent is passed to the event listener providing the current month and year information.
Made the following code: XHTML
FROM:
TO:
CSS
.monthYearOnly .ui-datepicker-calendar{
display: none;
}
Bean
public void updateFrom(){
Map requestParams = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();
LOGGER.info("before: " + from);
Calendar c = Calendar.getInstance();
c.setTime(from);
c.set(Calendar.MONTH, Integer.parseInt(requestParams.get("form:from_month")) - 1);
c.set(Calendar.YEAR, Integer.parseInt(requestParams.get("form:from_year")));
from = c.getTime();
LOGGER.info("after: " + from);
}
public void updateTo(){
Map requestParams = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();
LOGGER.info("before: " + to);
Calendar c = Calendar.getInstance();
c.setTime(to);
c.set(Calendar.MONTH, Integer.parseInt(requestParams.get("form:to_month")) - 1);
c.set(Calendar.YEAR, Integer.parseInt(requestParams.get("form:to_year")));
c.set(Calendar.DATE,c.getActualMaximum(Calendar.DAY_OF_MONTH));
to = c.getTime();
Calendar now = Calendar.getInstance();
if(c.after(now)){
to = now.getTime();
}
LOGGER.info("after: " + to);
}
Basically the ajax trigger when month or year is changed is the main feature here. Unfortunately, this is a bit of a work around since I can't retrieve the org.primefaces.event.DateViewChangeEvent. This seems to be a bug and I posted a report on the Primefaces Forum just now. Will update this post when I get some feedback there.
Hope it helps.