【问题标题】:convert xmlGregorianCalendar to Date and reverse将 xmlGregorianCalendar 转换为日期并反转
【发布时间】:2019-08-20 14:39:06
【问题描述】:

我所有,我有一个弹簧启动应用程序。我具体想要的是在他的关联实体中转换一个类(具有嵌套对象字段)。示例:

 public class example{
String string;
ObjectExample object;
}
public class ObjectExample{
String oneString;
XMLGregorianCalendar date;
}

这 2 个对象也在另一个包中标记为实体,但在 ObjectExampleEntity 中我有 Date 日期而不是 XMLGregorianCalendar,就像示例中这样

@实体

public class example{
String string;
ObjectExample object;
}

@实体

public class ObjectExample{
String oneString;
Date date;
}

因为我有一个带有很多嵌套类的大模型和大实体(以上只是一个示例),所以我使用推土机从模型转换为类。 例如,考虑只为父示例类创建存储库 jpa。 我想知道如何使用推土机将日期(实体)转换为 XMLGregorianCalendar(模型)并反转。我重复一遍,模型和实体是平等的。唯一的区别是日期的类型。谢谢

【问题讨论】:

  • 仅供参考,DateXMLGregorianCalendar 类都是可怕的日期时间类的一部分,这些类在几年前被 java.time 类所取代,并采用了JSR 310。
  • 好的,但是当我尝试使用推土机从一个转换到另一个时,我需要指出如何从 XMLGregorianCalendar 转换为日期或时间
  • 我无法更改 XMLGregorianCalendar 类型,因为我的模型是使用其他人预定义的 wsdl 生成的。我必须遵守这个提交的规则
  • 你的XMLGregorianCalendar 对象是什么样的? XMLGregorianCalendar 支持未定义的字段,因此对象可能有或没有日期,有或没有时间,有或没有 UTC 偏移量。

标签: java spring-boot date jpa xmlgregoriancalendar


【解决方案1】:

我假设:

  • 由于您的变量被命名为date,它包含一个日历日期(没有时间)。
  • 由于您无法控制的 WSDL,您与 XMLGregorianCalendar 绑定,但您可以在实体端更改类型。

基于这些假设,我在实体方面推荐LocalDate。它是现代 Java 日期和时间 API java.time 的一部分,并且准确地表示没有时间的日期。您使用的 Date 类设计不佳,已过时且不推荐使用。此外,尽管名称 a Date 从不代表日期,而是代表时间点。

还有更多选择。我正在展示三个。

选项 1:转移单个字段

XMLGregorianCalendarLocalDate

    DatatypeFactory xmlFactory = DatatypeFactory.newInstance();
    XMLGregorianCalendar wsDate = xmlFactory
            .newXMLGregorianCalendarDate(2019, DatatypeConstants.MARCH, 30,
                    DatatypeConstants.FIELD_UNDEFINED);

    // Validate
    if ((wsDate.getHour() != 0 && wsDate.getHour() != DatatypeConstants.FIELD_UNDEFINED)
            || (wsDate.getMinute() != 0 && wsDate.getMinute() != DatatypeConstants.FIELD_UNDEFINED)
            || (wsDate.getSecond() != 0 && wsDate.getSecond() != DatatypeConstants.FIELD_UNDEFINED)
            || (wsDate.getMillisecond() != 0 && wsDate.getMillisecond() != DatatypeConstants.FIELD_UNDEFINED)) {
        System.out.println("Warning: time of day will be lost in conversion");
    }
    if (wsDate.getTimezone() != DatatypeConstants.FIELD_UNDEFINED) {
        System.out.println("Warning: UTC offset will be lost in conversion");
    }

    // Convert
    LocalDate entityDate = LocalDate.of(wsDate.getYear(), wsDate.getMonth(), wsDate.getDay());
    System.out.println(entityDate);

这种情况下的输出是:

2019-03-30

LocalDateXMLGregorianCalendar

    LocalDate entityDate = LocalDate.of(2019, Month.MARCH, 31);

    XMLGregorianCalendar wsDate = xmlFactory.newXMLGregorianCalendarDate(
            entityDate.getYear(),
            entityDate.getMonthValue(),
            entityDate.getDayOfMonth(),
            DatatypeConstants.FIELD_UNDEFINED);
    System.out.println(wsDate);

2019-03-31

这种方式的优点:非常简单。缺点:您和您的读者需要注意以正确的顺序提及这些字段。

选项 2:通过字符串转换

    // Validate as before

    // Convert
    LocalDate entityDate = LocalDate.parse(wsDate.toXMLFormat());

结果和以前一样。

    XMLGregorianCalendar wsDate
            = xmlFactory.newXMLGregorianCalendar(entityDate.toString());

优点:简短,结果正确也就不足为奇了。缺点:对我来说,将其格式化为字符串只是为了将其解析回来感觉很浪费。

选项 3:通过 GregorianCalendarZonedDateTime 转换

    ZonedDateTime zdt = wsDate.toGregorianCalendar().toZonedDateTime();

    // Validate
    if (! zdt.toLocalTime().equals(LocalTime.MIN)) {
        System.out.println("Warning: time of day will be lost in conversion");
    }
    if (! zdt.getZone().equals(ZoneId.systemDefault())) {
        System.out.println("Warning: UTC offset will be lost in conversion");
    }

    // Finish conversion
    LocalDate entityDate = zdt.toLocalDate();

反之亦然:

    // It doesn’t matter which time zone we pick
    // since we are discarding it after conversion anyway
    ZonedDateTime zdt = entityDate.atStartOfDay(ZoneOffset.UTC);
    GregorianCalendar gCal = GregorianCalendar.from(zdt);
    XMLGregorianCalendar wsDate = xmlFactory.newXMLGregorianCalendar(gCal);
    wsDate.setTime(DatatypeConstants.FIELD_UNDEFINED, DatatypeConstants.FIELD_UNDEFINED,
            DatatypeConstants.FIELD_UNDEFINED, DatatypeConstants.FIELD_UNDEFINED);
    wsDate.setTimezone(DatatypeConstants.FIELD_UNDEFINED);

我在这里展示的验证有点简单,但也没有那么严格。如果你想要严格的验证,你可以使用之前的验证。

优点:我觉得是官方的方式;至少它使用提供的转换方法。我喜欢的是转换本身是直接而简短的。缺点:转换成XMLGregorianCalendar时需要手动将未使用的字段设置为undefined,比较啰嗦。

结论

我提出了三个选项,每个选项都有其优缺点。当然,您也可以混合使用,但两种方式都使用类似的转换最终可能不会那么混乱。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-08-24
    • 1970-01-01
    • 2013-05-18
    • 1970-01-01
    • 2012-09-12
    • 2014-01-25
    • 2019-12-18
    相关资源
    最近更新 更多