4

In my JSF managed bean I have declared startDate as java.utilDate type and I have getters and setters as well. From database startDate is date type.

When I receive value format is of default type and I format the date

SimpleDateFormat df = new SimpleDateFormat(" dd MMM yyyy");
Date oneDate = new Date(startDate);
df.format(oneDate);

Issue I am facing is df.format(oneDate); returns String. Is it possible to convert df.format(oneDate) back to Date, so that I need not have to change my startDate data type.

Any help is highly appreciated.

Thanks

5
  • 2
    Just use the parse() method. I however fail to see how that would be useful. You would likely end up with the same Date object as you started with. If you intend to format it for human representation in some UI, just format it at exactly the moment you're going to present it to humans. Commented Feb 27, 2012 at 15:09
  • Hope this helps you: docs.oracle.com/javase/1.4.2/docs/api/java/text/…, java.text.ParsePosition) Commented Feb 27, 2012 at 15:09
  • Why not extend Date and just use your own toString method? Commented Feb 27, 2012 at 15:09
  • 1
    @JamesBlack That is one terrible advice, I'd say! Commented Feb 27, 2012 at 15:11
  • @BalusC I agree with what you said, it is better to format in UI. So I added the following in my jsf page. <p:inputText value="#{vacationschedule.convertTime(vacationschedule.selectedRow.startDate)}" and convertTime method in managedBean is public String convertTime(Date time){ Date date = new Date(); Format format = new SimpleDateFormat("yyyy MM dd"); return format.format(date); } <p:inputText is showing correctly however if I would like to use <p:calendar then I am getting error SEVERE: java.lang.IllegalArgumentException: Cannot format given Object as a Date Commented Feb 27, 2012 at 15:24

4 Answers 4

10

As per the comment on the question:

@BalusC I agree with what you said, it is better to format in UI. So I added the following in my jsf page.

<p:inputText value="#{vacationschedule.convertTime(vacationschedule.selectedRow.startDate)}">

and convertTime method in managedBean is

public String convertTime(Date time){ 
    Date date = new Date(); 
    Format format = new SimpleDateFormat("yyyy MM dd"); 
    return format.format(date); 
} 

<p:inputText> is showing correctly however if I would like to use <p:calendar> then I am getting error

SEVERE: java.lang.IllegalArgumentException: Cannot format given Object as a Date

You're looking for the solution in the wrong direction. Human-targeted formatting has to be done in the View (UI) side, not in the Model side, let alone the Controller side.

To present a Date object in a human friendly pattern in a JSF component, you should be using <f:convertDateTime> tag provided by standard JSF component set:

<p:inputText value="#{vacationschedule.selectedRow.startDate}">
    <f:convertDateTime pattern="yyyy MM dd" />
</p:inputText>

This way you can keep the property just Date all the time. This way you will also be able to save the edited value (which wouldn't be possible with your initial attempt!).

As to the PrimeFaces' <p:calendar> component, it has a pattern attribute exactly for this purpose:

<p:calendar value="#{vacationschedule.selectedRow.startDate}" pattern="yyyy MM dd" />

Download and consult the PrimeFaces Users Guide to learn about all available attributes.

Sign up to request clarification or add additional context in comments.

7 Comments

Thanks a lot BalusC, this helped. Appreciate for your great answer.
But I see a small problem when I display the date in <p:calendar,the date is shown with one date ahead i.e. if I am selecting 01-Dec-2012, it is getting displayed as 02-Dec-2012 Is this something to primefaces?
No, it has something to do with the time zone. Your system default timezone is apparently beyond GMT. Date defaults to GMT. The <p:calendar> defaults to system default timezone. Use the timeZone attribute to explicitly specify the time zone, e.g. timeZone="GMT". See also the PrimeFaces Users Guide documentation which I've linked in my answer.
BalusC Thanks again. Like you mentioned stackoverflow.com/questions/2689245/… I added <context-param> <param-name>javax.faces.DATETIMECONVERTER_DEFAULT_TIMEZONE_IS_SYSTEM_TIMEZONE</param-name> <param-value>true</param-value> </context-param> web.xml and that solved the issue with date. Appreciated.
I have another question regarding data pattern. I have the following in my jsf page <p:calendar value="#{bean.selectedRow.endDate}" pattern="dd-MMM-yyyy" In UI date is shown as dd-MMM-yyyy. But when I receive value in bean, the format is changed to Wed Feb 16 00:00:00 AST 2011 How can I make sure even in bean I am getting the date in the same format as in UI? And what is the best way to implement this? Thanks
|
2

Using the same SimpleDateFormat object you created,

df.parse(yourDateString);

Comments

0

I hope that my code help you

public String dateToString(Date date, SimpleDateFormat formatter) {
    return formatter.format(date);
}

public Date stringToDate(String date, SimpleDateFormat formatter) {
    try {
        return formatter.parse(date);
    } catch (ParseException e) {
        //Catching exception
    }
    return null;
}

Comments

0

This works for me:

<p:calendar value="#{calTestBean.dateStr}" pattern="MM/dd/yyyy">
     <f:convertDateTime pattern="MM/dd/yyyy"/>
</p:calendar>

Reference: JSF 2.0 + Primefaces 2.x: Bind string to calendar

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.