Java 8 Java8 LocalDateTime到XMLGregorianCalendar删除+;05:30“;部分

Java 8 Java8 LocalDateTime到XMLGregorianCalendar删除+;05:30“;部分,java-8,java-time,Java 8,Java Time,像下面这样 LocalDateTime currentUTCTime = LocalDateTime.now(ZoneId.of("UTC")); String reqPattern = currentUTCTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss:SSS")); System.out.println("Required pattern: " + reqPattern); GregorianCalendar cale

像下面这样

LocalDateTime currentUTCTime = LocalDateTime.now(ZoneId.of("UTC"));
String reqPattern = currentUTCTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss:SSS"));
System.out.println("Required pattern: " + reqPattern);
GregorianCalendar calendar = GregorianCalendar.from(currentUTCTime.atZone(ZoneId.systemDefault()));
XMLGregorianCalendar xcal = DatatypeFactory.newInstance().newXMLGregorianCalendar(calendar);
System.out.println("But Showing As :" + xcal);
我希望输出为
2015-06-18 11:59:15:135
,但当我将
xcal
设置为 一个XML标记,它接受
XMLGregorianCalendar
,显示为
2015-06-18T11:59:15.135+05:30


如何删除
+05:30
部分?

使用此代码:

LocalDateTime currentUTCTime = LocalDateTime.now(); // using system timezone
String iso = currentUTCTime.toString();
if (currentUTCTime.getSecond() == 0 && currentUTCTime.getNano() == 0) {
    iso += ":00"; // necessary hack because the second part is not optional in XML
}
XMLGregorianCalendar xml =
  DatatypeFactory.newInstance().newXMLGregorianCalendar(iso‌​);
说明:

LocalDateTime currentUTCTime = LocalDateTime.now(); // using system timezone
String iso = currentUTCTime.toString();
if (currentUTCTime.getSecond() == 0 && currentUTCTime.getNano() == 0) {
    iso += ":00"; // necessary hack because the second part is not optional in XML
}
XMLGregorianCalendar xml =
  DatatypeFactory.newInstance().newXMLGregorianCalendar(iso‌​);
该代码使用给定的ISO-8601格式的本地时间戳的词典表示。由于
LocalDateTime
不引用任何时区,因此通过
toString()
的输出不能包含时区偏移量。结果:
XMLGregorianCalendar
将时区偏移视为“未设置”

更正:

LocalDateTime currentUTCTime = LocalDateTime.now(); // using system timezone
String iso = currentUTCTime.toString();
if (currentUTCTime.getSecond() == 0 && currentUTCTime.getNano() == 0) {
    iso += ":00"; // necessary hack because the second part is not optional in XML
}
XMLGregorianCalendar xml =
  DatatypeFactory.newInstance().newXMLGregorianCalendar(iso‌​);

原始代码并不特别关注
currentutcime.toString()
格式化输出的ISO变量。但是,如果这些部分等于零,则
java.time API
将生成无秒或纳秒的输出。这在ISO中是完全合法的,但W3C联盟已将第二部分定为非可选部分。类
XMLGregorianCalendar
严格遵循这个偏差规范。因此,上面所示的hack在这个特殊的边缘情况下使用简单的字符串连接。非常感谢@Dave的评论。顺便说一句,使用本文中建议的
currentUTCTime.format(DateTimeFormatter.ISO_DATE_TIME)
也是可能的(而不是所示的hack)。

您是否尝试过
DatatypeFactory.newInstance().newxmlgoriancalendar(currentUTCTime.toString())
使用ISO-8601中的词汇表示法?@Meno Hochschild:没有。但是现在试过了,效果很好。非常感谢你。你能把这个答案贴出来吗?这样我就可以把它标记为解决了,然后把它选为解决方案。这不行。您需要的是DatatypeFactory.newInstance().newXMLGregorianCalendar(currentUTCTime.format(DateTimeFormatter.ISO_DATE_TIME))。newXMLGregorianCalendar(字符串)要求其参数填充秒数。If seconds==0 LocalDateTime.toString()将完全忽略秒及其前面的:分隔符,从而导致日历工厂方法抛出IllegalArgumentException。@Dave现在已调整了我的答案。使用格式化程序比使用If语句更好