java f:convertDateTime 是否支持 Java8 LocalDate / LocalDateTime?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30395398/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 17:03:10  来源:igfitidea点击:

f:convertDateTime support for Java8 LocalDate / LocalDateTime?

javajsfjava-8jsf-2.2

提问by alfonx

The JSF Core Tag f:convertDateTimecan format java.util.Dateobjects. The Date class has many deprecated methods and with Java 8 come new classes to present local dates and times: LocalDateTimeand LocalDate.

JSF 核心标签f:convertDateTime可以格式化java.util.Date对象。Date 类有许多不推荐使用的方法,Java 8 提供了新的类来显示本地日期和时间:LocalDateTimeLocalDate

f:convertDateTimecan not format LocalDateTimenor LocalDate.

f:convertDateTime不能格式化LocalDateTimeLocalDate

Does anybody know, if there is an equivalent to the JSF core tag convertDateTime that can deal with LocalDateTime objects? Is support planned for a future release, or are alternative tags available?

有人知道,是否有等效于 JSF 核心标记 convertDateTime 可以处理 LocalDateTime 对象?是否计划为未来版本提供支持,或者是否有替代标签可用?

回答by hinneLinks

Just write your own Converter and extend the javax.faces.convert.DateTimeConverter- that way you can Reuse all the attributes that <f:convertDateTime>supports. Also it will take care of Localization too. Unfortunately it's a bit more complicated to write a Converter with Attributes.

只需编写您自己的转换器并扩展javax.faces.convert.DateTimeConverter- 这样您就可以重用所有<f:convertDateTime>支持的属性。它也将负责本地化。不幸的是,编写带有属性的转换器有点复杂。

Create component
First write your own Converter that extends javax.faces.convert.DateTimeConverter- just let the super-calls do all the work (including locale-stuff) and convert the result from/to LocalDate.

创建组件
首先编写您自己的扩展转换器javax.faces.convert.DateTimeConverter- 只需让超级调用完成所有工作(包括语言环境)并将结果从/到 LocalDate 转换。

@FacesConverter(value = LocalDateConverter.ID)
public class LocalDateConverter extends DateTimeConverter {
    public static final String ID = "com.example.LocalDateConverter";

    @Override
    public Object getAsObject(FacesContext facesContext, UIComponent uiComponent, String value) {
        Object o = super.getAsObject(facesContext, uiComponent, value);

        if (o == null) {
            return null;
        }

        if (o instanceof Date) {
            Instant instant = Instant.ofEpochMilli(((Date) o).getTime());
            return LocalDateTime.ofInstant(instant, ZoneId.systemDefault()).toLocalDate();
        } else {
            throw new IllegalArgumentException(String.format("value=%s could not be converted to a LocalDate, result super.getAsObject=%s", value, o));
        }
    }

    @Override
    public String getAsString(FacesContext facesContext, UIComponent uiComponent, Object value) {
        if (value == null) {
            return super.getAsString(facesContext, uiComponent,value);
        }

        if (value instanceof LocalDate) {
            LocalDate lDate = (LocalDate) value;
            Instant instant = lDate.atStartOfDay().atZone(ZoneId.systemDefault()).toInstant();
            Date date = Date.from(instant);
            return super.getAsString(facesContext, uiComponent, date);
        } else {
            throw new IllegalArgumentException(String.format("value=%s is not a instanceof LocalDate", value));
        }
    }
}

Then create a file LocalDateConverter-taglib.xmlin META-INF:

然后LocalDateConverter-taglib.xmlMETA-INF以下位置创建一个文件:

<facelet-taglib version="2.2"
    xmlns="http://xmlns.jcp.org/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-facelettaglibrary_2_2.xsd">

    <namespace>http://example.com/LocalDateConverter</namespace>
    <tag>
        <tag-name>convertLocalDate</tag-name>
        <converter>
            <converter-id>com.example.LocalDateConverter</converter-id>
        </converter>
    </tag>
</facelet-taglib>

And lastly, register the taglib in web.xml:

最后,在web.xml以下位置注册 taglib :

<context-param>
    <param-name>javax.faces.FACELETS_LIBRARIES</param-name>
    <param-value>/META-INF/LocalDateConverter-taglib.xml</param-value>
</context-param>

Usage
To use the new Tag in your JSF-Page add the new Taglib xmlns:ldc="http://example.com/LocalDateConverter"and use the tag:

用法
要在您的 JSF-Page 中使用新标签,请添加新的 Taglibxmlns:ldc="http://example.com/LocalDateConverter"并使用该标签:

<ldc:convertLocalDate type="both" dateStyle="full"/>