Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 520907
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T08:11:39+00:00 2026-05-13T08:11:39+00:00

Suppose i have a month, day and year select. One select for each one.

  • 0

Suppose i have a month, day and year select. One select for each one. And now i need to bind them to a single backing bean property – java.util.Date. How do i get my goal ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T08:11:39+00:00Added an answer on May 13, 2026 at 8:11 am

    Three ways:

    1. Back or intermediate it by java.util.Calendar with three getters and three setters.
    2. Make use of a Converter, this is however going to be a bit hacky.
    3. Make use of a 3rd party component like rich:calendar.

    Edit: as per the comments, here’s how option 2 would look like.

    page.jsp:

    <h:form>
        <h:selectOneMenu value="#{myBean.date}">
            <f:converter converterId="datePartConverter" />
            <f:attribute name="part" value="day" />
            <f:selectItems value="#{myBean.days}" />
        </h:selectOneMenu>
        <h:selectOneMenu value="#{myBean.date}">
            <f:converter converterId="datePartConverter" />
            <f:attribute name="part" value="month" />
            <f:selectItems value="#{myBean.months}" />
        </h:selectOneMenu>
        <h:selectOneMenu value="#{myBean.date}">
            <f:converter converterId="datePartConverter" />
            <f:attribute name="part" value="year" />
            <f:selectItems value="#{myBean.years}" />
        </h:selectOneMenu>
    
        <h:commandButton value="submit" action="#{myBean.submit}"/>
        <h:messages />
    </h:form>
    

    mypackage.MyBean:

    package mypackage;
    
    import java.util.ArrayList;
    import java.util.Date;
    import java.util.List;
    
    import javax.faces.model.SelectItem;
    
    public class MyBean {
    
        private static List<SelectItem> days = new ArrayList<SelectItem>();
        private static List<SelectItem> months = new ArrayList<SelectItem>();
        private static List<SelectItem> years = new ArrayList<SelectItem>();
    
        static {
            // Just do your thing to fill them. Only ensure that those are Strings,
            // else you'll need to change the type in Converter accordingly.
            for (int i = 1; i <= 31; i++) days.add(new SelectItem(String.valueOf(i)));
            for (int i = 1; i <= 12; i++) months.add(new SelectItem(String.valueOf(i)));
            for (int i = 2000; i <= 2020; i++) years.add(new SelectItem(String.valueOf(i)));
        }
    
        private Date date;
    
        public void submit() {
            // Print submitted date to stdout.
            System.out.println("Submitted date: " + date);
        }
    
        public List<SelectItem> getDays() {
            return days;
        }
    
        public List<SelectItem> getMonths() {
            return months;
        }
    
        public List<SelectItem> getYears() {
            return years;
        }
    
        public Date getDate() {
            return date;
        }
    
        public void setDate(Date date) {
            this.date = date;
        }
    
    }
    

    mypackage.DatePartConverter:

    package mypackage;
    
    import java.text.ParseException;
    import java.text.SimpleDateFormat;
    import java.util.Date;
    import java.util.Map;
    
    import javax.faces.application.FacesMessage;
    import javax.faces.component.UIComponent;
    import javax.faces.component.UIInput;
    import javax.faces.context.FacesContext;
    import javax.faces.convert.Converter;
    import javax.faces.convert.ConverterException;
    
    public class DatePartConverter implements Converter {
    
        public Object getAsObject(FacesContext context, UIComponent component, String value) {
            String part = (String) component.getAttributes().get("part");
            Date date = null;
    
            if (context.getRenderResponse()) {
                // Convert any default/selected date for display.
                Date selectedDate = (Date) ((UIInput) component).getValue();
                if (selectedDate != null) {
                    if (("day".equals(part) && new SimpleDateFormat("d").format(selectedDate).equals(value))
                        || ("month".equals(part) && new SimpleDateFormat("M").format(selectedDate).equals(value))
                        || ("year".equals(part) && new SimpleDateFormat("yyyy").format(selectedDate).equals(value)))
                    {
                        date = selectedDate;
                    }
                }
            } else {
                // Convert submitted date after submit.
                Map<String, Object> map = context.getExternalContext().getRequestMap();
                if ("day".equals(part)) {
                    map.put("DatePartConverter.day", value); // Save until we have all parts.
                } else if ("month".equals(part)) {
                    map.put("DatePartConverter.month", value); // Save until we have all parts.
                } else if ("year".equals(part)) {
                    String day = (String) map.get("DatePartConverter.day");
                    String month = (String) map.get("DatePartConverter.month");
                    String dateString = String.format("%s-%s-%s", day, month, value);
    
                    try {
                        date = new SimpleDateFormat("d-M-yyyy").parse(dateString);
                    } catch (ParseException e) {
                        throw new ConverterException(new FacesMessage(e.getMessage()), e);
                    }
                }
            }
    
            return date;
        }
    
    }
    
        public String getAsString(FacesContext context, UIComponent component, Object value) {
            // Not relevant here. Just return SelectItem's value.
            return (String) value;
        }
    

    faces-config.xml

    <converter>
        <converter-id>datePartConverter</converter-id>
        <converter-class>mypackage.DatePartConverter</converter-class>
    </converter>
    
    <managed-bean>
        <managed-bean-name>myBean</managed-bean-name>
        <managed-bean-class>mypackage.MyBean</managed-bean-class>
        <managed-bean-scope>request</managed-bean-scope>
    </managed-bean>
    

    Note that there’s no Validator and that SimpleDateFormat is by default lenient. Thus, selecting for example 31 november would produce 1 december. You may need to implement a DatePartValidator yourself if you want to warn about that.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.