f:convertDateTime timezone doesnt apply - jsf

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 !

Related

LocalDate (only) with primefaces calendar

I want to display a primefaces calendar element with an associated LocalDate in the Bean.
I used How to use java.time.ZonedDateTime / LocalDateTime in p:calendar as an example. The example as is works fine, BUT I don't need the time part and more importantly I don't want the time part of the calendar to be displayed.
The problem is that when I change the pattern and remove the time part:
<p:calendar id="localDate"
pattern="dd-MMM-yyyy"
value="#{bean.periodFrom}">
<f:converter converterId="localDateConverter" />
<p:ajax update="display" />
</p:calendar>
the converter is no longer called/ used at all. Which surprises me a bit.
Removing the pattern does not work neither.
Is there an alternative way to use the example without the time part? And why is the converter no longer called when I "simplify" the pattern?
JSF 2.2/ Java8/ Primefaces 6
The ajax update is not being triggered, because you didn't specify any event on which the update should be triggered.
If you do not specify any event, the default event type "change" will be used.
p:calendar triggers a "change" event, when you enter the date in the
inputText and blur
p:calendar triggers a "dateSelect" event, when you select a date in
the calendar popup
In your case, you should insert multiple event listeners:
<p:ajax update="display" />
<p:ajax event="dateSelect" update="display" />
So your code will result in:
<p:calendar id="localDate"
pattern="dd-MMM-yyyy"
value="#{bean.periodFrom}">
<f:converter converterId="localDateConverter" />
<p:ajax update="display" />
<p:ajax event="dateSelect" update="display" />
</p:calendar>
(By the way: It is not possible to combine multiple events in the same p:ajax component as you can read here https://stackoverflow.com/a/38319944/1643015)
If you used the ZonedDateTimeConverter it couldn't work at all because LocalDate is not an instance of
ZonedDateTime. That's why the converter will throw a ConverterException (because the instanceof check fails). You would see that if you insert a p:message for your p:calendar.
The used pattern "dd-MMM-yyyy" does also not work for the ZonedDateTimeConverter because this pattern neither
provides a time nor a timezone. That's why the conversion throws a DateTimeParseException and the converter throws a
ConverterException, too.
So if you want to work with LocalDate you have to implement your own converter than can deal with that class, which is very easy. Just take the ZonedDateTimeConverter and replace ZonedDateTime with LocalDate.

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

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

jsf output text with fn substring

I want to format a date in jsf which is xmlgregoriancalendar type. I've come across post which say I need custom converter. Does anybody found solution which does not require custom date converter. I was trying following but I gives me error saying...
Element type "h:outputText" must be followed by either attribute specifications, ">" or "/>".
This is what I tried on jsf
<h:outputText value="#{fn:substringBefore(aClip.lastTransmittedDate,"T")}">
<f:convertDateTime pattern="dd.MM.yyyy" />
</h:outputText>
Can anybody point out the explain the error I'm getting?
Apart from your EL syntax error (basically, with that nested double quote you're ending the attribute value too soon that it becomes value="#{fn:substringBefore(aClip.lastTransmittedDate," which is completely wrong EL syntax), this is after all absolutely not the right approach.
The <f:convertDateTime> converts Date to String and vice versa. It does not convert String in date pattern X to String in date pattern Y at all as you incorrectly seemed to expect.
Given a XMLGregorianCalendar, you need XMLGregorianCalendar#toGregorianCalendar() to get a concrete java.util.Calendar instance out of it which in turn offers a getTime() method to get a concrete java.util.Date instance out of it, ready for direct usage in <f:convertDateTime>.
Provided that your environment supports EL 2.2, this should do:
<h:outputText value="#{aClip.lastTransmittedDate.toGregorianCalendar().time}">
<f:convertDateTime pattern="dd.MM.yyyy" />
</h:outputText>
Or, if your environment doesn't support EL 2.2, then change the model in such way that you have for example a getter returning java.util.Calendar:
public Calendar getLastTransmittedDateAsCalendar() {
return lastTransmittedDate.toGregorianCalendar();
}
so that you can use
<h:outputText value="#{aClip.lastTransmittedDateAsCalendar.time}">
<f:convertDateTime pattern="dd.MM.yyyy" />
</h:outputText>
You can even add a getter returning the concrete java.util.Date.
Actually this error points that you have not well-formed XML. In EL you should use single quotes to indicate String. In your case value="#{fn:substringBefore(aClip.lastTransmittedDate,'T')}"
What about date converter, you should see on the type of #{aClip.lastTransmittedDate} property.
If it is date, just use
<h:outputText value="#{aClip.lastTransmittedDate}">
<f:convertDateTime pattern="dd.MM.yyyy" />
</h:outputText>
If it is not date (i.e. String), <f:convertDateTime/> won't work, and you should reformat it in Java code. For example, create another getter that will return modified string representing property value.

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?

s:convertDateTime not working in h:selectManyCheckbox

I'm using JSF 1.2 with Seam and am trying to get a date formatted as dd/MM/yyyy within an h:selectManyCheckbox. The functionality of the h:selectManyCheckbox works fine in itself - it's just that it doesn't display the date correctly.
<h:selectManyCheckbox id="paymentDates" value="#{entity.selectedPaymentDates}" layout="pageDirection" styleClass="radio">
<s:convertDateTime type="date" dateStyle="short" pattern="dd/MM/yyyy"/>
<s:selectItems value="#{entity.calculatedPaymentDates}" var="entity" label="#{entity}" hideNoSelection="true" />
</h:selectManyCheckbox>
Any ideas greatfully appreciated!
AJ
How is the date displayed?
you're using dateStyle with pattern. Use one or the other, not both. 'short" will format the date as:
5/14/07 12:55:42 PM
Though I think you'd have to use style="both" to get the time portion.
If you want to use a specific pattern try:
<s:convertDateTime type="date" pattern="dd/MM/yyyy"/>
or maybe:
<s:convertDateTime type="both" pattern="dd/MM/yyyy"/>

Resources