omnifaces / optimusfaces

Utility library for OmniFaces + PrimeFaces combined

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Formatting Instant(s)

mydeadlyvenoms opened this issue · comments

Hi

I am currently trying to format the Instant fields provided by OmniPersistence (e.g., created or lastModified) without success. They look like e.g., 2019-01-01T11:00:00Z. Is there a way to "globally" format them using a FacesConverter?

Unfortunately the following snippet does not work.

@FacesConverter(forClass = Instant.class)
public class InstantConverter implements Converter<Instant> {

    private final DateTimeFormatter dateTimeFormatter;

    public InstantConverter() {
        dateTimeFormatter = DateTimeFormatter.ofPattern("dd.MM.yyyy HH:mm:ss")
                                             .withLocale(Locale.getDefault())
                                             .withZone(ZoneId.systemDefault());
    }

    @Override
    public Instant getAsObject(FacesContext context, UIComponent component, String value) {
        return dateTimeFormatter.parse(value, Instant::from);
    }

    @Override
    public String getAsString(FacesContext context, UIComponent component, Instant value) {
        return dateTimeFormatter.format(value);
    }

}

Thank you very much for your help.

Works for me. Remember to explicitly use <h:outputText>. Without it, JSF indeed won't try to find a converter on forClass. See also https://stackoverflow.com/q/5539894/157882

Actually also not an OptimusFaces problem. The same problem would manifest when not using OptimusFaces.