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

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Jacob
  • 14,463
  • 65
  • 207
  • 320
  • 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. – BalusC Feb 27 '12 at 15:09
  • Hope this helps you: http://docs.oracle.com/javase/1.4.2/docs/api/java/text/SimpleDateFormat.html#parse(java.lang.String, java.text.ParsePosition) –  Feb 27 '12 at 15:09
  • Why not extend Date and just use your own toString method? – James Black Feb 27 '12 at 15:09
  • 1
    @JamesBlack That is one *terrible* advice, I'd say! – Marcelo Feb 27 '12 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. ` – Jacob Feb 27 '12 at 15:24

4 Answers4

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.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Thanks a lot BalusC, this helped. Appreciate for your great answer. – Jacob Feb 27 '12 at 15:48
  • But I see a small problem when I display the date in ` – Jacob Feb 27 '12 at 15:54
  • 1
    No, it has something to do with the time zone. Your system default timezone is apparently beyond GMT. `Date` defaults to GMT. The `` 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 Feb 27 '12 at 15:56
  • BalusC Thanks again. Like you mentioned http://stackoverflow.com/questions/2689245/fconvertdatetime-displays-wrong-date I added ` javax.faces.DATETIMECONVERTER_DEFAULT_TIMEZONE_IS_SYSTEM_TIMEZONE true ` web.xml and that solved the issue with date. Appreciated. – Jacob Feb 27 '12 at 16:12
  • I have another question regarding data pattern. I have the following in my jsf page ` – Jacob Mar 02 '12 at 22:03
  • What is your problem? That's just the default format when you print `date.toString()` (see also [javadoc](http://docs.oracle.com/javase/6/docs/api/java/util/Date.html#toString())). Why do you want to store date in a different fixed format as a `String`? Why would your Java code care about this? Just keep it `Date`. This way you also won't run in future problems whenever you want to save it in a DB, or want to do math/comparisons on it, etcetera. – BalusC Mar 02 '12 at 22:04
  • I am not converting date.toString() anywhere in my code and startDate is declared as Date not as String. But still when I try to insert to DB, the format is different that of DD-MMM-YYYY. I am not sure how come my date format changed to `Wed Feb 16 00:00:00 AST 2011` In my bean I have `for (Schedule editValues : beanValues) { editValues.setStartDate(selectedRow.getStartDate());` For DB update code I have error coming from this line `preparedStatement = prepareStatement(connection, SQL_EDIT, values);` where values is declared as `Object[] values` – Jacob Mar 02 '12 at 22:24
2

Using the same SimpleDateFormat object you created,

df.parse(yourDateString);
DaveJohnston
  • 10,031
  • 10
  • 54
  • 83
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

Community
  • 1
  • 1
Tiago PC
  • 109
  • 1
  • 7
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;
}