0

I have the next data table in my page:

<t:dataTable value="#{bean.listOfPersons}" var="current">
   <t:column>
      <t:outputText value="#{current.birthdate}">
         <f:converter pattern="dd/mm/yyyy HH:mm" type="date"/>
      </t:outputText>
   </t:column>
</t:dataTable>

Person is an object I created, and one of it's properties is birthdate. Now I wish to display the details of many persons in this <t:dateTable>, including the birthdate. As you can see, I choose to display the birthdate in dd/mm/yyyy HH:mm pattern, so I can see the time if known. Now, the problem is that some of the birthdates doesn't have a time (only date), and when the time is null in java.util.Date, the time is automatic 00:00. How can I display the birthdate in short pattern if time is unknown (only date), and the regular pattern when I know the time (date and time)?

What happens now:

17/3/1994 5:56
1/1/1991 00:00
12/2/1990 17:53
21/12/2012 00:00

What I want to see:

17/3/1994 5:56
1/1/1991
12/2/1990 17:53
21/12/2012

Short pattern when time is unknown.


This is what I'm writing in the page.xhtml:

<t:outputText value="#{bean.date}">
<f:converter convertetId="myconverter" pattern="[some pattern]" timeZone="America/Chicago" type="date">
</t:outputText>

This is the converter:

public class MyConverter extends DateTimeConverter
{
@Override
public String getAsString (FacesContext faces, UIComponent comp, Object obj)
{
this.setTimeZone(null);
String result = super.getAsString(faces, comp, obj);
// My converter here...
return result;
}

@Override
public void setTimeZone(TimeZone tz)
{
// Always setting my own time zone, no matter what.
super.setTimeZone(TimeZone.getTimeZone("America/Chicago"));
}
}

Now, as you can see, I'm always setting the time zone myself on getAsString because somehow now the f:converter doesn't recognize it's attributes... know why?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
MightGod
  • 436
  • 2
  • 8
  • 19
  • 1
    Just finished reading. You would need to implement a custom converter I think. I haven't worked with Java dates that much so I'm not sure if there's a pattern that already deals with your scenario. I'm going to bed (it's 4am) so if no one answers you I'll show you how to do it. You should get an answer quick though, that's an easy question...then again I'm know to make hasty judgments so I might be wrong lol. – Andy Jul 03 '13 at 08:13
  • What is the data type of current.birthdate ? – erencan Jul 03 '13 at 09:02
  • 1
    As Andy said, gooing with a custom converter is a good choice (And I think the best). Another ugly solution, would be to get the pattern from your Person objects: `` and in `getBirthdayPattern` you test on the date value – Laabidi Raissi Jul 03 '13 at 09:06
  • Thanks, Andy and Laabidi. It seems I have to do a costum converter, but I don't know how to do this. Laabidi, I thought to do similiar solution as you suggested: to create a `getter` only for displaying, which I perform a check If I have a time or not, and return the date as string with the suitable pattern. But in the future I would have many objects with date, and I don't want to do this ugly solution in each object. So... How can I create a custom converter? Thanks. :) – MightGod Jul 03 '13 at 09:30
  • erencan, `current.birthdate` is a `java.util.Date`, a property inside `Person` I've created (which is `current`). – MightGod Jul 03 '13 at 09:33
  • for custom converter, take a look here: http://www.mkyong.com/jsf2/custom-converter-in-jsf-2-0/ – Laabidi Raissi Jul 03 '13 at 10:00
  • You can extend `java.util.Date` to your own class and override `toString()` method to modify the printed date if you do not want to implement a converter. – erencan Jul 03 '13 at 10:34
  • Wow, it's sounds like a good idea, but I have many places I'm using date in my project, and I don't want to run over the project and change them all. Thanks :) Laabidi, I'm checking the link you gave me. – MightGod Jul 03 '13 at 12:16
  • @Andy, didn't get what you mean – Laabidi Raissi Jul 03 '13 at 16:25
  • @LaabidiRaissi lol, sorry. Nice alternative. I never thought of using pattern that way. – Andy Jul 03 '13 at 16:26
  • ah, lol, it's the kind of lazy solutions, but using converters is much better (centralized solution) – Laabidi Raissi Jul 03 '13 at 16:28
  • Hi everyone, I'm still working on the new converter. You all gave me great solutions! Thank you all! If I'll have issues, I hope you still be here :) – MightGod Jul 04 '13 at 05:51
  • Hi everyone, I've finished the converter, and it seems to work fine. But when I tried to set the other properties in (like "type"/"pattern"/"timeZone", suddenly it doesn't familiar with this attributes. What I did is everytime when the f:converter fires the main method (`getAsString`), I'm setting the properties myself and it's working. But anyone know why f:converter suddenly don't recognize his attributes? – MightGod Jul 04 '13 at 08:00
  • It could be anywhere so we might need to look at it. Show us the converter and how you are calling it from your xhtml. Off the top of my head I can't answer with seeing it because as far as I know that's not standard behavior – Andy Jul 05 '13 at 06:50
  • Are you doing something like this by any chance '' cuz if you are that's wrong. You should be stacking them. Google add multiple converters in JSF or something. Its hard to use the comment section on a phone :/ – Andy Jul 05 '13 at 06:57
  • @Andy Hi , I'm sorry for the silence in the few last days. I'm updating the code right now. – MightGod Jul 08 '13 at 07:58
  • possible duplicate of [How to set converter properties for each row of a datatable?](http://stackoverflow.com/questions/7530560/how-to-set-converter-properties-for-each-row-of-a-datatable) – BalusC Jul 08 '13 at 17:09
  • @BalusC thanks man, I see the problem there. But I have different issue: I don't have my own converter: I'm using `` and `f:converter` have more some attributes (pattern, type, timeZone...) and somehow it doesn't recognize the other attributes at all. – MightGod Jul 09 '13 at 07:32
  • @user2542954 Still having issues ?! He basically gave you the answer. If you are using `converterId="myconverter"` then it's implied you are using a custom one written by you. Let me know if you solved it. I'll see what I can do – Andy Jul 10 '13 at 21:55
  • @user2542954 Let me know if you can't solve it still I mean. No use torturing yourself after almost a week. I'll help you out if you need me to. – Andy Jul 11 '13 at 02:39
  • @Andy Hi Andy and thank you all. You gave me the answer, and that's why I probably can't use the other attributes of the converter. I solve the problem, as I said, by setting this attributes manually each row... Thats a nasty solution, but it's working... Thank you all again! – MightGod Jul 15 '13 at 06:16
  • @user2542954 sure no problem. Good luck with everything – Andy Jul 15 '13 at 06:18

1 Answers1

0

Use PrettyTime library by ocpsoft. It has a nice pre-built converter for JSF, also with support for different Locales.

Aritz
  • 30,971
  • 16
  • 136
  • 217