- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我一直在 Java EE 应用程序中使用 Joda Time 进行日期时间操作,其中关联客户端提交的日期时间字符串表示已使用以下转换例程进行转换,然后再将其提交到数据库,即在 getAsObject()
中。 JSF 转换器中的方法。
org.joda.time.format.DateTimeFormatter formatter = org.joda.time.format.DateTimeFormat.forPattern("dd-MMM-yyyy hh:mm:ss a Z").withZone(DateTimeZone.UTC);
DateTime dateTime = formatter.parseDateTime("05-Jan-2016 03:04:44 PM +0530");
System.out.println(formatter.print(dateTime));
UTC
提前 5 小时 30 分钟/
GMT
.因此,转换为
UTC
应该从使用 Joda 时间正确发生的给定日期时间中减去 5 小时 30 分钟。它按预期显示以下输出。
05-Jan-2016 09:34:44 AM +0000
+0530
代替
+05:30
已被采用,因为它依赖于
<p:calendar>
以这种格式提交区域偏移量。似乎不可能改变
<p:calendar>
的这种行为。 (否则就不需要这个问题本身)。
java.time.format.DateTimeFormatter formatter = java.time.format.DateTimeFormatter.ofPattern("dd-MMM-yyyy hh:mm:ss a Z").withZone(ZoneOffset.UTC);
ZonedDateTime dateTime = ZonedDateTime.parse("05-Jan-2016 03:04:44 PM +0530", formatter);
System.out.println(formatter.format(dateTime));
05-Jan-2016 03:04:44 PM +0000
UTC
它应该在其中转换。
java.time.format.DateTimeFormatter formatter = java.time.format.DateTimeFormatter.ofPattern("dd-MMM-yyyy hh:mm:ss a z").withZone(ZoneOffset.UTC);
ZonedDateTime dateTime = ZonedDateTime.parse("05-Jan-2016 03:04:44 PM +05:30", formatter);
System.out.println(formatter.format(dateTime));
05-Jan-2016 09:34:44 AM Z
Z
已被替换为
z
和
+0530
已被替换为
+05:30
.
<p:calendar>
可以考虑什么中间方式和 Java 8 中的 Java Time 可以一致且连贯地工作
<p:calendar>
内部使用
SimpleDateFormat
连同
java.util.Date
?
@FacesConverter("dateTimeConverter")
public class DateTimeConverter implements Converter {
@Override
public Object getAsObject(FacesContext context, UIComponent component, String value) {
if (value == null || value.isEmpty()) {
return null;
}
try {
return ZonedDateTime.parse(value, DateTimeFormatter.ofPattern("dd-MMM-yyyy hh:mm:ss a Z").withZone(ZoneOffset.UTC));
} catch (IllegalArgumentException | DateTimeException e) {
throw new ConverterException(new FacesMessage(FacesMessage.SEVERITY_ERROR, null, "Message"), e);
}
}
@Override
public String getAsString(FacesContext context, UIComponent component, Object value) {
if (value == null) {
return "";
}
if (!(value instanceof ZonedDateTime)) {
throw new ConverterException("Message");
}
return DateTimeFormatter.ofPattern("dd-MMM-yyyy hh:mm:ss a z").withZone(ZoneId.of("Asia/Kolkata")).format(((ZonedDateTime) value));
// According to a time zone of a specific user.
}
}
<p:calendar>
.
<p:calendar id="dateTime"
timeZone="Asia/Kolkata"
pattern="dd-MMM-yyyy hh:mm:ss a Z"
value="#{bean.dateTime}"
showOn="button"
required="true"
showButtonPanel="true"
navigator="true">
<f:converter converterId="dateTimeConverter"/>
</p:calendar>
<p:message for="dateTime"/>
<p:commandButton value="Submit" update="display" actionListener="#{bean.action}"/><br/><br/>
<h:outputText id="display" value="#{bean.dateTime}">
<f:converter converterId="dateTimeConverter"/>
</h:outputText>
@ManagedBean
@ViewScoped
public class Bean implements Serializable {
private ZonedDateTime dateTime; // Getter and setter.
private static final long serialVersionUID = 1L;
public Bean() {}
public void action() {
// Do something.
}
}
05-Jan-2016 12:00:00 AM +0530
,它会重新显示
05-Jan-2016 05:30:00 AM IST
因为
05-Jan-2016 12:00:00 AM +0530
的原始转换至
UTC
在转换器出现故障。
+05:30
的本地时区转换至
UTC
然后从
UTC
转换回到那个时区显然必须重新显示与通过日历组件输入的相同日期时间,这是给定的转换器的基本功能。
java.sql.Timestamp
相互转换的 JPA 转换器和
java.time.ZonedDateTime
.
import java.sql.Timestamp;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import javax.persistence.AttributeConverter;
import javax.persistence.Converter;
@Converter(autoApply = true)
public final class JodaDateTimeConverter implements AttributeConverter<ZonedDateTime, Timestamp> {
@Override
public Timestamp convertToDatabaseColumn(ZonedDateTime dateTime) {
return dateTime == null ? null : Timestamp.from(dateTime.toInstant());
}
@Override
public ZonedDateTime convertToEntityAttribute(Timestamp timestamp) {
return timestamp == null ? null : ZonedDateTime.ofInstant(timestamp.toInstant(), ZoneOffset.UTC);
}
}
最佳答案
您的具体问题是您从 Joda 的无区域日期时间实例迁移 DateTime
到 Java8 的分区日期时间实例 ZonedDateTime
而不是 Java8 的无区域日期时间实例 LocalDateTime
.
使用 ZonedDateTime
(或 OffsetDateTime
)而不是 LocalDateTime
需要至少 2 个额外的更改:
ZonedDateTime
中。在格式化期间必须使用实例。DateTimeFormatter#withZone()
只会用 ZonedDateTime
给出令人困惑的结果因为它将在解析过程中充当回退(它仅在输入字符串或格式模式中不存在时区时使用),并且在格式化过程中将充当覆盖(存储在 ZonedDateTime
中的时区被完全忽略)。这是您的可观察问题的根本原因。只是省略withZone()
在创建格式化程序时应该修复它。timeOnly="true"
,那么您不需要指定 <p:calendar timeZone>
.即使您这样做,您也更愿意使用 TimeZone.getTimeZone(zonedDateTime.getZone())
而不是对其进行硬编码。TIMESTAMP WITH TIME ZONE
从 Oracle 得知的列类型和 PostgreSQL数据库。例如,MySQL does not support it .你需要第二列。LocalDateTime
并在所有层(包括数据库)中依赖固定/预定义的时区。通常为此使用UTC。
ZonedDateTime
在 JSF 和 JPA 中
ZonedDateTime
用合适的
TIMESTAMP WITH TIME ZONE
DB 列类型,使用下面的 JSF 转换器在
String
之间转换在 UI 和
ZonedDateTime
中在模型中。此转换器将查找
pattern
和
locale
来自父组件的属性。如果父组件本身不支持
pattern
或
locale
属性,只需将它们添加为
<f:attribute name="..." value="...">
.如果
locale
属性不存在,(默认)
<f:view locale>
将被使用。有
否
timeZone
属性的原因如上面 #1 中所述。
@FacesConverter(forClass=ZonedDateTime.class)
public class ZonedDateTimeConverter implements Converter {
@Override
public String getAsString(FacesContext context, UIComponent component, Object modelValue) {
if (modelValue == null) {
return "";
}
if (modelValue instanceof ZonedDateTime) {
return getFormatter(context, component).format((ZonedDateTime) modelValue);
} else {
throw new ConverterException(new FacesMessage(modelValue + " is not a valid ZonedDateTime"));
}
}
@Override
public Object getAsObject(FacesContext context, UIComponent component, String submittedValue) {
if (submittedValue == null || submittedValue.isEmpty()) {
return null;
}
try {
return ZonedDateTime.parse(submittedValue, getFormatter(context, component));
} catch (DateTimeParseException e) {
throw new ConverterException(new FacesMessage(submittedValue + " is not a valid zoned date time"), e);
}
}
private DateTimeFormatter getFormatter(FacesContext context, UIComponent component) {
return DateTimeFormatter.ofPattern(getPattern(component), getLocale(context, component));
}
private String getPattern(UIComponent component) {
String pattern = (String) component.getAttributes().get("pattern");
if (pattern == null) {
throw new IllegalArgumentException("pattern attribute is required");
}
return pattern;
}
private Locale getLocale(FacesContext context, UIComponent component) {
Object locale = component.getAttributes().get("locale");
return (locale instanceof Locale) ? (Locale) locale
: (locale instanceof String) ? new Locale((String) locale)
: context.getViewRoot().getLocale();
}
}
并使用下面的 JPA 转换器在
ZonedDateTime
之间进行转换在模型和
java.util.Calendar
在 JDBC 中(体面的 JDBC 驱动程序将需要/将它用于
TIMESTAMP WITH TIME ZONE
类型列):
@Converter(autoApply=true)
public class ZonedDateTimeAttributeConverter implements AttributeConverter<ZonedDateTime, Calendar> {
@Override
public Calendar convertToDatabaseColumn(ZonedDateTime entityAttribute) {
if (entityAttribute == null) {
return null;
}
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(entityAttribute.toInstant().toEpochMilli());
calendar.setTimeZone(TimeZone.getTimeZone(entityAttribute.getZone()));
return calendar;
}
@Override
public ZonedDateTime convertToEntityAttribute(Calendar databaseColumn) {
if (databaseColumn == null) {
return null;
}
return ZonedDateTime.ofInstant(databaseColumn.toInstant(), databaseColumn.getTimeZone().toZoneId());
}
}
LocalDateTime
在 JSF 和 JPA
LocalDateTime
使用适当的基于 UTC 的
TIMESTAMP
(没有时区!)DB 列类型,使用下面的 JSF 转换器在
String
之间转换在 UI 和
LocalDateTime
中在模型中。此转换器将查找
pattern
,
timeZone
和
locale
来自父组件的属性。如果父组件本身不支持
pattern
,
timeZone
和/或
locale
属性,只需将它们添加为
<f:attribute name="..." value="...">
.
timeZone
属性必须表示输入字符串的回退时区(当
pattern
不包含时区时)和输出字符串的时区。
@FacesConverter(forClass=LocalDateTime.class)
public class LocalDateTimeConverter implements Converter {
@Override
public String getAsString(FacesContext context, UIComponent component, Object modelValue) {
if (modelValue == null) {
return "";
}
if (modelValue instanceof LocalDateTime) {
return getFormatter(context, component).format(ZonedDateTime.of((LocalDateTime) modelValue, ZoneOffset.UTC));
} else {
throw new ConverterException(new FacesMessage(modelValue + " is not a valid LocalDateTime"));
}
}
@Override
public Object getAsObject(FacesContext context, UIComponent component, String submittedValue) {
if (submittedValue == null || submittedValue.isEmpty()) {
return null;
}
try {
return ZonedDateTime.parse(submittedValue, getFormatter(context, component)).withZoneSameInstant(ZoneOffset.UTC).toLocalDateTime();
} catch (DateTimeParseException e) {
throw new ConverterException(new FacesMessage(submittedValue + " is not a valid local date time"), e);
}
}
private DateTimeFormatter getFormatter(FacesContext context, UIComponent component) {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(getPattern(component), getLocale(context, component));
ZoneId zone = getZoneId(component);
return (zone != null) ? formatter.withZone(zone) : formatter;
}
private String getPattern(UIComponent component) {
String pattern = (String) component.getAttributes().get("pattern");
if (pattern == null) {
throw new IllegalArgumentException("pattern attribute is required");
}
return pattern;
}
private Locale getLocale(FacesContext context, UIComponent component) {
Object locale = component.getAttributes().get("locale");
return (locale instanceof Locale) ? (Locale) locale
: (locale instanceof String) ? new Locale((String) locale)
: context.getViewRoot().getLocale();
}
private ZoneId getZoneId(UIComponent component) {
Object timeZone = component.getAttributes().get("timeZone");
return (timeZone instanceof TimeZone) ? ((TimeZone) timeZone).toZoneId()
: (timeZone instanceof String) ? ZoneId.of((String) timeZone)
: null;
}
}
并使用下面的 JPA 转换器在
LocalDateTime
之间进行转换在模型和
java.sql.Timestamp
在 JDBC 中(体面的 JDBC 驱动程序将需要/将它用于
TIMESTAMP
类型列):
@Converter(autoApply=true)
public class LocalDateTimeAttributeConverter implements AttributeConverter<LocalDateTime, Timestamp> {
@Override
public Timestamp convertToDatabaseColumn(LocalDateTime entityAttribute) {
if (entityAttribute == null) {
return null;
}
return Timestamp.valueOf(entityAttribute);
}
@Override
public LocalDateTime convertToEntityAttribute(Timestamp databaseColumn) {
if (databaseColumn == null) {
return null;
}
return databaseColumn.toLocalDateTime();
}
}
LocalDateTimeConverter
使用
<p:calendar>
针对您的具体情况
<p:calendar>
不查找转换器 forClass
,您要么需要使用 <converter><converter-id>localDateTimeConverter
重新注册它在 faces-config.xml
,或更改注释如下 @FacesConverter("localDateTimeConverter")
<p:calendar>
没有 timeOnly="true"
忽略 timeZone
,并在弹出窗口中提供编辑它的选项,您需要删除 timeZone
属性以避免转换器混淆(仅当 pattern
中不存在时区时才需要此属性)。timeZone
输出期间的属性(使用 ZonedDateTimeConverter
时不需要此属性,因为它已存储在 ZonedDateTime
中)。<p:calendar id="dateTime"
pattern="dd-MMM-yyyy hh:mm:ss a Z"
value="#{bean.dateTime}"
showOn="button"
required="true"
showButtonPanel="true"
navigator="true">
<f:converter converterId="localDateTimeConverter" />
</p:calendar>
<p:message for="dateTime" autoUpdate="true" />
<p:commandButton value="Submit" update="display" action="#{bean.action}" /><br/><br/>
<h:outputText id="display" value="#{bean.dateTime}">
<f:converter converterId="localDateTimeConverter" />
<f:attribute name="pattern" value="dd-MMM-yyyy hh:mm:ss a Z" />
<f:attribute name="timeZone" value="Asia/Kolkata" />
</h:outputText>
如果您打算创建自己的
<my:convertLocalDateTime>
对于属性,您需要将它们作为带有 getter/setter 的 bean 类属性添加到转换器类中,并在
*.taglib.xml
中注册它。如本答案所示:
Creating custom tag for Converter with attributes
<h:outputText id="display" value="#{bean.dateTime}">
<my:convertLocalDateTime pattern="dd-MMM-yyyy hh:mm:ss a Z"
timeZone="Asia/Kolkata" />
</h:outputText>
关于jsf - 如何在 p :calendar 中使用 java.time.ZonedDateTime/LocalDateTime,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34883270/
关于strcat函数。 while (*p) p++; 和 while (*++p) ; 两者都有效,但是 while (*p++) ; 不起作用。我认为 first 和 th
" in HTML?(HTML中的““是什么
?)
下面例子中的第一行代码是什么。我看到一个YouTuber在写下面的代码,它显示了一个设计在csswar Challenges中。我也尝试了一下,它很管用。但我以前从未在任何HTML教程上看到过它,我在
vs.
是不间断空格,表示没有换行的空白处。 如果我用 我在两个段落之间有一个空格(更大的间隔)。如果我使用 我在两个段落之间只有一个新行(没有中断)。为什么? 最佳答案 在 HTML 中
这个问题已经有答案了: Why are these constructs using pre and post-increment undefined behavior? (14 个回答) 已关闭 9
我对编程还很陌生,只是想知道为什么这段代码: for ( ; *p; ++p) *p = tolower(*p); 当 p 指向一个字符串时,可以降低 c 中字符串的大小写吗? 最佳答案 一般来说,这
代码 int n = 25; int *p = &n; printf("%x\n %d\n %x\n", p, p[0], p[1]); 返回: \ 当然我永远不会这样做,但在 K&R 中声明
所以,我想创建一个简单的程序,返回有关连续素数的计算结果。首先,我创建一个包含所有这些素数的列表,然后尝试计算结果,但这给了我一个超出范围的索引。有人可以帮助我吗?我的程序: primes = []
这个问题在这里已经有了答案: With arrays, why is it the case that a[5] == 5[a]? (20 个答案) 关闭 9 年前。 我想知道 C/C++ 中以下四
我仍在努力理解 *p、&p 和 p 之间的区别。根据我的理解,* 可以被认为是“指向的值”,而 & 可以被认为是“地址”。换句话说,* 保存值,而 & 保存地址。如果这是真的,那么 *p 和 p 之间
你是吗? [xxxrecipientFirstNamexxx]
和你是吗? {recipientFirstName}
需要更换 你是吗? [xxxrecipientFirstNamexxx] 和 你是吗? {recipientFirstName} 。我尝试使用边界匹配器。但结果并不符合预期。我尝试使用下面的代码 "A
我想按 IsTop 属性升序排序对象,然后按 JobId 属性降序排序: query = query.OrderBy(p => p.IsTop).ThenOrderByDescending(p =
在我尝试使用 Apache POI 进行转换的 Excel 文件中,我有一个单元格的数值为 -3.97819466831428,自定义格式为“0.0 p.p.;(0.0 p.p.)”。因此,在 Exc
我想创建一个扩展方法,允许我调用 ToSerializableDictionary(p => p.ID)而不是 .ToDictionary(p => p.ID)在以下 LINQ 上下文中。虽然我不确定
在下面的 HTML 代码上运行此 jQuery 代码会返回不同的结果,我认为它们应该返回相同的值。 jQuery 代码: var counter = 0; $("p").each(function()
在下面的代码片段中,符号 *p 等同于 p[0],*(p + 1) 等同于p[1],依此类推。 int* p = new int[3] { 1, 2, 3}; cout << *p << ' ' <<
这个问题在这里已经有了答案: What will happen when I call a member function on a NULL object pointer? [duplicate]
这个问题在这里已经有了答案: 关闭 10 年前。 Possible Duplicate: Undefined Behavior and Sequence Points 按照标准中的定义,E1 +=
" in HTML?(在HTML中“
以下示例中的第一行代码是什么。我看到一个youtube用户写下面的代码,它显示在cssbattle挑战的设计。我也试过,它的作品。但我从来没有见过它在任何HTML教程之前,我在谷歌上搜索它,但它只显示
每当我收到来自 MS outlook 的电子邮件时,我都会收到此标记 & nbsp ; (没有空格)哪个显示为?在 <>. 当我将其更改为 ISO-8859-1 时,浏览器页面字符集编码为 UTF-8
p1
TESTp2
代码: from bs4 import BeautifulSoup soup = BeautifulSoup('p1TESTp2') print soup.div() 结果: [p1, p2] 为什么
我是一名优秀的程序员,十分优秀!