0

I have noticed that numerous Date methods in Java have been deprecated, but rather most people tell me to use Calender.

Here is my problem. I am trying to create a Calendar where you can select a date and within Java set the date. So I have

public class someClass
{

    private Date startDate;
    private Date endDate;

    public void setStartDate(Date date) 
    {
        this.startDate = date;
    }

    public void setEndDate(Date date) 
    {
        this.endDate = date;
    }   

    public Date getStartDate()
    {
        return this.startDate;
    }

    public Date getEndDate() 
    {
        return this.end;
    }

}

with the RichFaces code

<rich:calendar popup="true" mode="ajax"  value="#{someClass.startDate}"/>

When I manually set it up in code it seems to show up, but when I change to a different date it never sets the date.

I have also seen that within calendars they tell me to use Calendar. But when I type in Calenders.Time. it's not in Java, is it because it's protected? How does it work with RichFaces? Can RichFaces access it?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Kevin
  • 3,509
  • 4
  • 31
  • 45
  • Your concrete problem has **nothing** to do with those deprecated methods of `Date`. – BalusC Aug 03 '11 at 15:39
  • You did create an instance of `someClass` or register it as a managed bean right? Note that commonly class names start with a capital letter, e.g. `SomeClass`. – Thomas Aug 03 '11 at 15:40
  • Yeah I was just using that as an example – Kevin Aug 03 '11 at 15:41

1 Answers1

1

As to the concrete question of "How to use java.util.Calendar with <rich:calendar>?", just bind the component to Calendar#getTime() which in turn returns a fullworthy java.util.Date.

E.g.

public class Bean {

    private Calendar startDate;

    public Bean() {
        startDate = Calendar.getInstance(); // You need to prepopulate it.
    }

    // ...
}

with

 <rich:calendar value="#{bean.startDate.time}" />

But that will not solve the real problem of the value not being set in the model. This is caused by something else. Don't get mislead by the deprecated methods of Date. Using Date in <rich:calendar> is perfectly fine. You only shouldn't be using those deprecated methods to set the year/month/day/etc of the date programmatically yourself like as in bean's constructor.

Your real problem can have many causes. Perhaps the <rich:calendar> isn't been placed in a <h:form>? Perhaps you're nesting multiple <h:form> components? Perhaps you've somewhere an immediate="true"? Perhaps you're grabbing/debugging the value at the wrong moment? Etc..etc..

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • question, what do you mean its in h:form? I'm pretty new at this or maybe what is immediate = true? I literally don't have much code around the calender besides the one i just showed – Kevin Aug 03 '11 at 15:48
  • Well, that explains it. You need a `` around it in order to be able to submit the input value along HTTP POST to the server. I'd say, put JSF aside for a while and take some time to learn HTML, HTML forms and HTTP (and preferably later also Servlets) to grasp the basic concepts. See also http://stackoverflow.com/questions/1958808/java-web-development-what-skills-do-i-need. JSF is basically a HTML code generator. Rightclick page in browser, *View Source* and look. Any HTML `` element is supposed to be inside a HTML `
    ` element.
    – BalusC Aug 03 '11 at 15:50
  • well. that makes sense... lol I didn't know i had to do that. Thanks! – Kevin Aug 03 '11 at 15:54
  • What books/tutorials are you using to learn JSF? JSF has a relatively steep learning curve, definitely for ones who aren't already familiar with basic HTTP/HTML/Servlets. Please ensure that you're having/reading the proper ones. Otherwise you will still pull out your hairs after one year or two and unnecessarily rant on JSF for remnant of your life. – BalusC Aug 03 '11 at 15:58
  • I just started a job and kind of got thrown into that. I don't do much web development, but I do a lot of .net in my previous jobs. This is actually my first permanent job. so that's why, btw what did you mean by immediate = true? what is it suppose to do? – Kevin Aug 03 '11 at 16:00
  • As to `immediate="true"`, check this: http://balusc.blogspot.com/2006/09/debug-jsf-lifecycle.html True, it's JSF 1.2 targeted, but the principles are not different for JSF 2.0 (which you're supposed to use nowadays). As to the learning materials, please grab yourself a good book. "JSF (2.0): The Complete Reference" is decent. – BalusC Aug 03 '11 at 16:01
  • I've tried numerous things, added the h:form, added the immediate, nothing's working for me. this is sooo stressful. – Kevin Aug 03 '11 at 17:48
  • You've got to provide more information than "it doesn't work". Ask a new question wherein you tell in detail the exact JSF and RichFaces versions you're using (e.g. Mojarra 2.1.2 with RichFaces 4.0.0) and provide the smallest possible view (from `` until with ``) and model (the backing bean) which exhibits your problem. – BalusC Aug 03 '11 at 18:36
  • Okay. So, Stupid mistake. i forgot to add @In. Two letters ruined a day for me. – Kevin Aug 03 '11 at 19:08