- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我的 Web 应用程序正在使用 Apache CXF 和 JAVA8,如果用户发送 xs:datetime
,则会在响应中面临以下错误输入(秒 00)为
<urn1:dateTimeVal>2016-04-29T20:00:00</urn1:dateTimeVal>
错误:
org.apache.cxf.interceptor.Fault: Marshalling Error: cvc-datatype-valid.1.2.1: '2016-04-29T20:00' is not a valid value for 'dateTime'.
我调试分析,如果用户发送dateTimeVal
如2016-04-29T20:00:00
然后输入的 CXF 验证通过,XML 值UnMarshaled 为 java.time.LocalDateTime
如2016-05-05T20:00
,并且在返回响应时,由于秒部分(00)丢失而发生编码错误。
任何帮助/提示表示赞赏。
P.S:您可以尝试使用以下代码片段:
java.time.LocalDateTime dt= java.time.LocalDateTime.of(2016, Month.MAY, 5, 20, 00, 00);
System.out.println(dt);
注意:以上代码示例仅用于理解打印日期时间值。但 Web 应用程序中预期的实际返回类型是 java.time.LocalDateTime
预期输出:2016-05-05T20:00:00
实际产出:2016-05-05T20:00
编辑:该字段的绑定(bind)(JAXB)内容是:
@XmlElement(required = true, type = String.class)
@XmlJavaTypeAdapter(LocalDateTimeAdapter.class)
@XmlSchemaType(name = "dateTime")
@Generated(value = "com.sun.tools.xjc.Driver", date = "2016-05-03T05:28:57+05:30", comments = "JAXB RI v2.2.11")
@NotNull
protected LocalDateTime dateTimeVal;
和LocalDateTimeAdapter
文件是
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import java.time.temporal.TemporalAccessor;
import javax.xml.bind.annotation.adapters.XmlAdapter;
public class LocalDateTimeAdapter
extends XmlAdapter<String, LocalDateTime>
{
public static LocalDateTime parse(String value)
{
DateTimeFormatter dateTimeAndZoneformatter = DateTimeFormatter.ISO_OFFSET_DATE_TIME;
DateTimeFormatter dateTimeformatter = DateTimeFormatter.ISO_LOCAL_DATE_TIME;
TemporalAccessor ta = null;
try
{
ta = dateTimeformatter.parse(value);
}
catch (DateTimeParseException ex)
{
ta = dateTimeAndZoneformatter.parse(value);
}
return LocalDateTime.from(ta);
}
public static String print(LocalDateTime value)
{
return value.toString();
}
public LocalDateTime unmarshal(String value)
{
return parse(value);
}
public String marshal(LocalDateTime value)
{
return print(value);
}
}
最佳答案
问题似乎出在 LocalDateTimeAdapter.print()
中。 LocalDateTime.toString()
当秒值为 0 时省略秒。
如果你把它改成
public static String print(LocalDateTime value)
{
return value.format(DateTimeFormatter.ISO_LOCAL_DATE_TIME);
}
它在编码时也会提供秒数。
要查看快速示例,请注意以下代码段的结果:
System.out.println(LocalDateTime.of(2016,1,1,0,0,0,0).toString());
System.out.println(LocalDateTime.of(2016,1,1,0,0,0,0).format(DateTimeFormatter.ISO_LOCAL_DATE_TIME));
它给出的输出是
2016-01-01T00:00
2016-01-01T00:00:00
在 LocalDateTime.toString()
的文档中,它解释了此行为:
The output will be one of the following ISO-8601 formats:
- uuuu-MM-dd'T'HH:mm
- uuuu-MM-dd'T'HH:mm:ss
- uuuu-MM-dd'T'HH:mm:ss.SSS
- uuuu-MM-dd'T'HH:mm:ss.SSSSSS
- uuuu-MM-dd'T'HH:mm:ss.SSSSSSSSS
The format used will be the shortest that outputs the full value of the time where the omitted parts are implied to be zero.
关于datetime - 如果秒为 00,java.time.LocalDateTime 转换问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37024797/
我在从两个 LocalDateTime 对象获取秒数时遇到问题。 // a timestammp for the time when the connection was established pu
我有字段 initiationDate,它由 ToStringSerializer 类序列化为 ISO-8601 格式。 @JsonSerialize(using = ToStringSerializ
我有一个初始日期和一个 cron 表达式。我怎样才能找到满足此 cron 表达式的下一个日期?。 String cronExpresion = "* * * * * *" LocalD
我需要做一个 LocalDateTime UTC 日期到另一个日期的转换 LocalDateTime考虑特定时区 tz 的变量。 在我的研究中,我找到了很多解决方案,但它们都转换了 LocalDate
这个问题在这里已经有了答案: Java 8: Difference between two LocalDateTime in multiple units (11 个答案) 关闭 6 年前。 我知道
给定日期时间和时间, LocalDateTime rightDateWrongTime = new LocalDateTime("2017-03-02T15:23:00.000");
将 LocalDateTime 转换为 UTC 中的 LocalDateTime。 LocalDateTime convertToUtc(LocalDateTime date) { //do
这是我的代码: private final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MMM dd HH:mm:ss");
我想为一些沉迷于日期值的数据创建一个小型 GUI。所以我在 JavaFX 中使用 LocalDateTimeTextField。所以我将使用以下代码获取选定的时间: LocalDateTimeText
解析 DateTime 时出现异常。我在这里缺少什么 DateTimeFormatter formatter = DateTimeFormatter.ofPattern("0DDDHHmmss");
我有 REST 网络服务公开资源和创建日期。它是用 Java 8 编写的 - 使用 LocalDateTime。 Jackson 2 正在将其序列化为: “创建日期”:[2016, 5, 19, 18
有什么简单的方法可以将 Java 8 的 LocalDateTime 转换为 Joda 的 LocalDateTime? 其中一种方法是将其转换为字符串,然后从该字符串创建 Joda 的 LocalD
我在 Java 8 中使用 LocalDateTime.now() 获取 LocalDateTime。但有时这个 now() 函数会以没有秒的格式返回时间给我。我认为这是因为秒为零,但我需要秒。 代码
Date与LocalDateTime和LocalDate互相转换思路 Date转LocalDateTime和LocalDate都可以通过Date先转换成Instant然后再转换成LocalDateTi
在本教程中,我们将为LocalDate和LocalDateTime类编写自定义序列化程序和反序列化程序。 LocalDateSerializer LocalDateTimeSerializer Loc
从 API 传递的字符串通常遵循格式 yyyy-MM-dd HH:mm:ss.SSS但是当时间戳中有尾随 0 时,它们会被截断,例如 2019-07-16 13:29:15.100转换为 2019-0
我在 Java 8 中使用 spring boot 2.2.6 和 Jackson 2.10.3。我在整个项目中使用 localdatetime 对象。 Jackson 无法正确解析 LocalDat
我有使用年份和一年中的某一天 (1-365/366) 加上一天中的时间的数据,例如 2018-338T14:02:57.47583,而不是年月日。 我正在尝试编写一个输入时间戳的函数,它通过一堆正则表
我正在尝试将以下日期时间字符串解析为 LocalDateTimeObject,但是我无法识别日期时间字符串的格式。 2021 年 10 月 9 日星期六 02:10:23 -0400 Stri
我尝试并失败(或部分成功)为 LocalDateTime 实现扩展功能 这是我的尝试: fun LocalDateTime.isNotBefore(other: ChronoLocalDateTime
我是一名优秀的程序员,十分优秀!