java - 如何将 org.threeten.bp.OffsetDateTime 转换为 java.time.OffsetDateTime?
问题描述
我正在使用使用 ThreeTen 日期类型的客户端库(第三方,不是我的,无法更改)。我的项目是 Java 11 并使用 Java 8 日期类型。将 ThreeTeen 对象转换为 Java 8 对象的推荐方法是什么?
解决方案
似乎没有将一个实例转换为另一个实例的内置方法。
我认为您已经编写了自己的转换器,例如以下之一:
逐部分转换:
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, create one using the seconds of the given
java.time.ZoneOffset.ofTotalSeconds(ttOdt.getOffset().getTotalSeconds());
}
解析另一个实例的格式化输出:
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));
}
还没有测试哪个更有效...
推荐阅读
- r - 如何动态命名列表的元素?
- swift - 当之前在设备上授权 healthkit 访问但否则崩溃时工作正常
- perl - IO::Socket::SSL - 建立客户端连接
- java - 在另一个带有按钮的小程序中调用一个小程序?
- android - 将 Firebase 链接到我的 android 应用时的问题
- javascript - 如何获得“” 从视图到控制器的“ActionResult”?
- android - 如何使用 gson 将 json 转换为接口类型,但仍有可能在 RoomDatabase 中使用其他类
- caching - 如何使用高速缓存系统找到基本向量/矩阵运算的可实现峰值性能?
- flutter - 部分屏幕在 Flutter 中不滚动
- django - Django:发送验证电子邮件后如何保存新的电子邮件地址