如何将org.threeten.bp.OffsetDateTime转换为java.time.OffsetDateTime?

如何将org.threeten.bp.OffsetDateTime转换为java.time.OffsetDateTime?,java,datetime,java-time,datetime-conversion,threetenbp,Java,Datetime,Java Time,Datetime Conversion,Threetenbp,我正在使用一个客户端库(第三方,不是我的,不能更改),它使用了三个日期类型。我的项目是Java11,使用Java8日期类型。建议用什么方法将ThreeTeen对象转换为Java8对象 似乎没有将一个实例转换为另一个实例的内置方法 我认为您已经编写了自己的转换器,如下所示: 逐部分转换: public static java.time.OffsetDateTime convertFrom(org.threeten.bp.OffsetDateTime ttOdt) { // convert

我正在使用一个客户端库(第三方,不是我的,不能更改),它使用了三个日期类型。我的项目是Java11,使用Java8日期类型。建议用什么方法将ThreeTeen对象转换为Java8对象

似乎没有将一个实例转换为另一个实例的内置方法

我认为您已经编写了自己的转换器,如下所示:

逐部分转换:

public static java.time.OffsetDateTime convertFrom(org.threeten.bp.OffsetDateTime ttOdt) {
    // convert the instance part by part...
    return java.time.OffsetDateTime.of(ttOdt.getYear(), ttOdt.getMonthValue(),
            ttOdt.getDayOfMonth(), ttOdt.getHour(), ttOdt.getMinute(),
            ttOdt.getSecond(), ttOdt.getNano(),
            // ZoneOffset isn't compatible, we need to extract the String-ID
            java.time.ZoneOffset.of(ttOdt.getOffset().toString()));
}
public static java.time.OffsetDateTime convertFrom(org.threeten.bp.OffsetDateTime ttOdt) {
    // convert the instance by parsing the formatted output of the given instance
    return java.time.OffsetDateTime.parse(
            ttOdt.format(org.threeten.bp.format.DateTimeFormatter.ISO_OFFSET_DATE_TIME));
}
解析另一个实例的格式化输出:

public static java.time.OffsetDateTime convertFrom(org.threeten.bp.OffsetDateTime ttOdt) {
    // convert the instance part by part...
    return java.time.OffsetDateTime.of(ttOdt.getYear(), ttOdt.getMonthValue(),
            ttOdt.getDayOfMonth(), ttOdt.getHour(), ttOdt.getMinute(),
            ttOdt.getSecond(), ttOdt.getNano(),
            // ZoneOffset isn't compatible, we need to extract the String-ID
            java.time.ZoneOffset.of(ttOdt.getOffset().toString()));
}
public static java.time.OffsetDateTime convertFrom(org.threeten.bp.OffsetDateTime ttOdt) {
    // convert the instance by parsing the formatted output of the given instance
    return java.time.OffsetDateTime.parse(
            ttOdt.format(org.threeten.bp.format.DateTimeFormatter.ISO_OFFSET_DATE_TIME));
}

还没有测试哪一个更有效…

设计中似乎没有预见到这一需求。对于这个看似简单的需求,没有真正好的、自然的选择

我喜欢有东西可供选择。deHaar已经提供了两个我们能得到的最好的选择。因此,作为补充,这里有第三个:通过秒和纳秒转换,从历元和总偏移秒开始

    org.threeten.bp.OffsetDateTime fromThirdParty
            = org.threeten.bp.OffsetDateTime.of(2020, 1, 25, 23, 34, 56, 123456789,
                    org.threeten.bp.ZoneOffset.ofHours(1));

    java.time.Instant jtInstant = java.time.Instant
            .ofEpochSecond(fromThirdParty.toEpochSecond(), fromThirdParty.getNano());
    java.time.ZoneOffset jtOffset = java.time.ZoneOffset.ofTotalSeconds(
            fromThirdParty.getOffset().getTotalSeconds());
    java.time.OffsetDateTime converted
            = java.time.OffsetDateTime.ofInstant(jtInstant, jtOffset);

    System.out.println("From " + fromThirdParty);
    System.out.println("To   " + converted);
此代码段的输出为:

可能的优势包括:我们正在传输3个数值字段(deHaar的第一次转换中是7个),从而降低了错误传输错误值的风险。而且我们仍然避免格式化成字符串并进行解析(这对我来说是一种浪费,但又好又短)


请将它包装成一个名称很好的方法,就像deHaar所做的那样。

注意,转换这些方法时可能会有细微的差异,因为AFAIK Three Ten使用自己的时区数据库,而JDK可能使用不同的数据库(可能是OS时区数据库或JDK包含的数据库,取决于操作系统)。对此你无能为力。@JoachimSauer虽然我不知道这是否正确,但它与
OffsetDateTime
无关,因为它们不使用时区。@OleV.V:这是一个很好的观点!关于它是独立的TZ-DB,有一个甚至是一个。这就是我最后做的。我希望有一种内置的方法来做这件事。如果你真的想一部分一部分地做,而不需要格式化和解析,只需吹毛求疵,使用
java.time.ZoneOffset.ofTotalSeconds(ttOdt.getOffset().getTotalSeconds())
可以被视为更加一致。你两种选择都给了我,太好了!我没有考虑这个选择…谢谢@deHaar第四个选项是通过
ZoneDateTime
GregoriaCalendar
进行转换。我不想那样做。最好避免使用Gregorianalendar。至少通过
String
(您的第二个选项)转换更好。