How to display java.util.Date in 2013-07-15 22:00:45 format - jsf

I have a variable stored in TIMESTAMP format in db. It stores time like this:
2013-07-15 22:00:45
I want to take this and use it in a h:outputText item. I make a database search, and store the results in a resultset.
java.util.Date dt= res.getTimestamp(5);
Then i want to use this date, which was read from database, in an outputtext item. But doing the following does not work:
<h:outputText value="#{list.date}"
Where list is an ArrayList. I am sure that list.date is the date I read from database but I cannot get its value with the format I specified. So how can I do this?

<h:outputText value="#{list.date}">
<f:convertDateTime type="both" pattern="YYYY-MM-dd HH:mm:ss"/>
</h:outputText>

I guess you can use JSF's convertDateTime tag.
e.g. :
<h:outputText value="#{list.date}">
<f:convertDateTime pattern="d-M-yyyy" />
</h:outputText>
See http://www.jsftoolbox.com/documentation/help/12-TagReference/core/f_convertDateTime.html

Related

f:convertDateTime timezone doesnt apply

I'm using JSF 2.3.
I have a datatable with some items, each item has an offsetDateTime.
I want to display the datetime in an other timezone.
<p:column headerText="UpdateTime" sortBy="#{item.updateTimestamp}">
<h:outputText value="#{item.updateTimestamp}">
<f:convertDateTime pattern="yyyy/MM/dd HH:mm:ss" timeZone="Europe/Brussels" type="offsetDateTime"/>
</h:outputText>
</p:column>
However for an input in UTC 2020-07-08T13:49:36.801693Z for instance, my app will display 2020/07/08 13:49:36 (it should be 15:49:36 as it's "summer" in Belgium).
I know that we can't put dynamic values for timezone (omnifaces converter has been created to tackle this issue) but here it should work as the timeZone is a String that corresponds to a standard TZ id.
Thank for you help !

JSF outputText and PrimeExtensions inputNumber [duplicate]

I am using JSF 2 and RichFaces 3. Here in the picture shown below, numbers are being displayed as what they are in the database.
But I want to display them as 6749395.20 if fraction part is there and 5095138.00 if no fraction part is there.
As of now I have tried something like this.
<rich:column>
<f:facet name="header">
<h:outputText value="Total Amount"/>
</f:facet>
<h:outputText value="#{rr[2]}">
<f:convertNumber type="number" groupingUsed="true" minFractionDigits="2" pattern="#0.00"/>
</h:outputText>
</rich:column>
Actually I am showing all of them together, but I have tried with all of them as all possible combinations with type, groupingUsed, minFractionDigits and pattern.
Why does it not work? How is this caused and how can I solve it?
That can happen if the value is not a Number at all, for example a String. You're then basically using the wrong type for the data it represents. To represent currencies in Java, you should be using BigDecimal. Also, make sure that the type in the database table is right, i.e. it should not be a varchar, but a decimal.
Once you've fixed the data type, then the <f:convertNumber> will work as you told it to do. Note that the pattern attribute will override the groupingUsed and minFractionDigits. You should use either the pattern or the others. Also, type="number" is already the default, so it can be removed.
So, either use
<f:convertNumber pattern="#0.00" />
or
<f:convertNumber groupingUsed="true" minFractionDigits="2" />
Note that they generate different formats. You probably want to set grouping to false.
You can also use type="currency", it will then automatically apply the right pattern as per the UIViewRoot#getLocale():
<f:convertNumber type="currency" />
See also the tag library documentation and the DecimalFormat javadoc.

Converter can't get rich:dataGrid rows as custom parameters

I need a converter with custom parameters, I've made it and it works well, except when I use it with row elements and I can't understand why, ideas?
Examples
This works well:
<h:outputText value="#{bean.value}">
<cc:converter param="#{bean.attribute}" />
</h:outputText>
also this one:
<h:outputText value="#{bean.value}">
<cc:converter param="fixedValue" />
</h:outputText>
this one doesn't work at all, param is null inside the converter:
<rich:dataGrid value="#{bean.list}" var="row">
<h:outputText value="#{row.value}">
<cc:converter param="#{row.attribute}" />
</h:outputText>
</rich:dataGrid>
See BalusC link. It describes why your converter doesn't work. Converter without parameters works fine inside datatable in JSF 1.2. For simulate conversion you can use getter method in object which represents row like getConvertedValue and move your code from converter into this method. After that in datatable you can call #{row.convertedValue} for displaying the converted value.

Display amount in format $###,###,###.## using f:convertNumber

I would like to display the amount in $12,050,999.00 format.
I tried as follows:
<h:outputText value="#{sampleBean.Amount}">
<f:convertNumber pattern="###,###" currencySymbol="$" type="currency"/>
</h:outputText>
However, it didn't display the amount in the desired format. I got 12,050,999 instead.
The desired format is shown in the below image:
How can I achieve this?
Your pattern is wrong for a currency. You should be using pattern="¤#,##0.00".
<f:convertNumber pattern="¤#,##0.00" currencySymbol="$" />
However, there's more at matter: in your original code you also specified the type attribute, which is correct, but this is mutually exclusive with the pattern attribute whereby the pattern attribute gets precedence.
You should actually be omitting the pattern attribute and stick to the type attribute.
<f:convertNumber type="currency" currencySymbol="$" />
Note that this uses the locale as available by UIViewRoot#getLocale() which is expected to be an English/US based locale in order to get the right final format for the USD currency. You'd like to explicitly specify it in either the <f:view>:
<f:view locale="en_US">
or in the locale attribute of the <f:convertNumber>:
<f:convertNumber type="currency" currencySymbol="$" locale="en_US" />
See also:
Does <f:convertNumber> use the right number separator when using patterns to format currency?

Format a JSF converted value

Today I noticed ocpsoft has a nice time library adapted to use in JSF 2 as a converter. The strong point of that is you can use the converter directly in the date displayed in the view and it converts it into a string telling the user something like 6 hours ago or 17 hours from now. I think my best is to combine both, the JSF converted date and this one to display something like 26-03-2013 17:00 (4 hours from now). I can achieve something similar with the following code:
<h:outputText value="#{task._StartDate}" style="padding:2px;">
<f:convertDateTime pattern="dd-MM-yyyy HH:mm" timeZone="GMT+1" />
</h:outputText>
<h:outputText value="#{task._StartDate}">
<f:converter converterId="org.ocpsoft.PrettyTimeConverter" />
</h:outputText>
My problem comes when I want to put the second value into parenthesis. The PrettyTimeConverter accepts only a date as a value and I can't write the parenthesis there directly. Also JSF is not accepting the following:
<h:outputFormat value="({0})">
<f:param value="#{task._StartDate}">
<f:converter converterId="org.ocpsoft.PrettyTimeConverter" />
</f:param>
</h:outputFormat>
With that I have the following error:
<f:converter> Parent not an instance of ValueHolder:
javax.faces.component.UIParameter#1492636
Any idea about how to achieve that avoiding writing both parentheses using specific h:outputText tags?
You can just put those parentheses directly in template text without the need for another <h:outputText>s.
<h:outputText value="#{task._StartDate}" style="padding:2px;">
<f:convertDateTime pattern="dd-MM-yyyy HH:mm" timeZone="GMT+1" />
</h:outputText>
(<h:outputText value="#{task._StartDate}">
<f:converter converterId="org.ocpsoft.PrettyTimeConverter" />
</h:outputText>)
See also:
Is it suggested to use h:outputText for everything?

Resources