在Converter中将JSF日历日期转换为JodaTime [英] Convert JSF calendar date into JodaTime in Converter

查看:126
本文介绍了在Converter中将JSF日历日期转换为JodaTime的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

也许,JodaTime是最好的日期和时间库.

JodaTime is amicably the best date and time library out there, maybe.

因此,我很想在后备bean的JodaTime实例中从前端xhtml日历小部件接收用户输入.因此,我正在考虑使用JSF Converter来完成这项工作.

So I am tempted to receive user input from the front end xhtml Calendar widget in JodaTime instance at the backing bean. Therefore I am thinking of using JSF Converter for this job.

这样做明智吗?

同时,由于我使用的是Primefaces组件库,并且已经在后备bean处收到Date对象,因此提供自定义Converter需要我重新做一遍吗? IE.从String格式转换为Date对象,然后转换为JodaTime DateTime对象.我可以以某种方式让默认的JSF首先将其转换为Date吗?

At the same time, since I'm using Primefaces component library and I am already receiving a Date object at my backing bean, would providing a custom Converter require me to do this all over again? I.e. convert from String format to Date object, then to JodaTime DateTime object. Can I somehow let the default JSF convert it to Date first?

推荐答案

只需在模型,视图和持久层中坚持使用java.util.Date(因为所有这些都已经内置了对它的支持,因此通常不需要执行日期)完全在这些层进行计算/操作),并且仅在需要基于java.util.Date值执行计算/操作时才在业务层中使用JodaTime.这将使很多事情变得简单.

Just stick to java.util.Date in the model, view and persistence layer (as all have already builtin support for it and you generally don't need to perform date calculations/manipulations at those layers at all) and use JodaTime in business layer only whenever you need to perform calculations/manipulations based on the java.util.Date value. This will keep a lot of things simple.

使用Java 8后,将java.util.Date迁移到 ZonedDateTime . JSF 2.3将在<f:convertDateTime>中附带其他Java 8支持.在此之前,您需要按照此答案中的说明进行自制程序:

Once you're on Java 8, migrate java.util.Date to java.time.Local/ZonedDateTime. JSF 2.3 will ship with additional Java 8 support in <f:convertDateTime>. Until then, you'll need to homebrew them as instructed in this answer: How to use java.time.ZonedDateTime / LocalDateTime in p:calendar.

这篇关于在Converter中将JSF日历日期转换为JodaTime的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆