Question

i have a simple problem but i didnot find a solution for it. I have a simple p:selectCheckboxMenu and i want use the selectedDates after click on the button.

I tried it with f:convertDateTime

<h:form id="mainform">
    <p:panelGrid columns="2">
        <p:selectCheckboxMenu label="Date" value="#{myBean.selectedDates}">
                <f:selectItems value="#{myBean.dates}" var="date" itemValue="#{date}" itemLabel="#{myBean.convertDate(date)}"/>
                <f:convertDateTime type="date" pattern="dd-MM-yyyy"/>
            </p:selectCheckboxMenu>
            <p:commandButton value="Test" actionListener="#{myBean.printDates}"/>
    </p:panelGrid>

but than i get an Error- Message: "Invaild Value".

Than i tried a Converter:

@FacesConverter("myDateConverter")
public class MyDateConverter extends DateTimeConverter{

public MyDateConverter(){
    setPattern("MM/dd/yyyy");
}}

and

<p:selectCheckboxMenu label="Date" value="#{myBean.selectedDates}" converter="myDateConverter">

But same error message. When i use no converter i get "String"- Values in my Date-List because type erasure.

Question: How i get the selected dates as dates?

Here is my bean for completeness:

@ManagedBean(name = "myBean")
@ViewScoped
public class MyBean implements Serializable {

private List<Date> dates;

private List<Date> selectedDates;

private SimpleDateFormat dateFormat;

@PostConstruct
public void init() {
    System.out.println("POST CONSTRUCT!");
    dateFormat = new SimpleDateFormat("yyyy.MM.dd");
    dates = new ArrayList<Date>();
    dates.add(new Date());
}

/**
 * 
 */
public void printDates(){
    for(Date d : selectedDates){
        System.out.println(d);
    }
}

/**
 * 
 * @param date
 * @return
 */
public String convertDate(Date date){
    return dateFormat.format(date);
}
Was it helpful?

Solution

The converter is the source of the problem as it removes the time and then it uses the default time when converting back to Date.

You can use

< f:convertDateTime pattern="yyyy-MM-dd HH:mm:ss.SSS Z" />

or try with a < f:datetimeconverter >

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top