将 Java 8 的 LocalDateTime 转换为 Joda 的 LocalDateTime 的简单方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33030163/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 14:11:39  来源:igfitidea点击:

Easy way to convert Java 8's LocalDateTime to Joda's LocalDateTime

javadatetimejodatimejava-time

提问by Naresh

Is there any easy way to convert Java 8's LocalDateTime to Joda's LocalDateTime?

有没有什么简单的方法可以将 Java 8 的 LocalDateTime 转换为 Joda 的 LocalDateTime?

One of the ways is to convert it to String and then create Joda's LocalDateTime from that String.

一种方法是将其转换为字符串,然后从该字符串创建 Joda 的 LocalDateTime。

采纳答案by Andreas

Convert through epoch millis (essentially a java.util.Date()):

通过纪元毫秒转换(本质上是 a java.util.Date()):

java.time.LocalDateTime java8LocalDateTime = java.time.LocalDateTime.now();

// Separate steps, showing intermediate types
java.time.ZonedDateTime java8ZonedDateTime = java8LocalDateTime.atZone(ZoneId.systemDefault());
java.time.Instant java8Instant = java8ZonedDateTime.toInstant();
long millis = java8Instant.toEpochMilli();
org.joda.time.LocalDateTime jodaLocalDateTime = new org.joda.time.LocalDateTime(millis);

// Chained
org.joda.time.LocalDateTime jodaLocalDateTime =
        new org.joda.time.LocalDateTime(
            java8LocalDateTime.atZone(ZoneId.systemDefault())
                              .toInstant()
                              .toEpochMilli()
        );

// One-liner
org.joda.time.LocalDateTime jodaLocalDateTime = new org.joda.time.LocalDateTime(java8LocalDateTime.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli());

Single line, but long, so "easy"? It's all relative.

单行,但很长,这么“容易”?都是相对的。

回答by araqnid

Both localDate types consist of (year, month, date), so just copy those values:

两种 localDate 类型都包含(year, month, date),因此只需复制这些值:

public static org.joda.time.LocalDate toJoda(java.time.LocalDate input) {
    return new org.joda.time.LocalDate(input.getYear(),
                                       input.getMonthValue(),
                                       input.getDayOfMonth());
}

回答by Ben Thurley

A slightly shorter method:
If you can get away with using a ZoneOffsetinstead of a ZoneIdthen the code may be shortened slightly.

稍微短一点的方法:
如果您可以使用 aZoneOffset而不是 aZoneId则代码可能会稍微缩短。

java.time.LocalDateTime java8LDT = java.time.LocalDateTime.now();
org.joda.time.LocalDateTime jodaLDT = new org.joda.time.LocalDateTime(
    java8LDT.toInstant(ZoneOffset.UTC).toEpochMilli()
);

The call to the atZone()method can be dropped by providing the timezone offset to the toInstant()method.

atZone()可以通过向方法提供时区偏移量来删除对该方法的调用toInstant()

More detailed explanation
Just to clarify, the step I have removed is with creating the intermediate ZonedDateTime object. This part of the sequence is still with the Java 8 API before anything to do with Joda.

更详细的解释
为了澄清起见,我删除的步骤是创建中间 ZonedDateTime 对象。这部分序列在与 Joda 有任何关系之前仍然使用 Java 8 API。

The process of conversion first involves converting the Java 8 LocalDateTime to the number of millis since the epoch. This can be achieved in a couple of different ways. For example in Andreas' answer:

转换过程首先涉及将 Java 8 LocalDateTime 转换为自纪元以来的毫秒数。这可以通过几种不同的方式实现。例如在 Andreas 的回答中:

LocalDateTime ldt = LocalDateTime.now();
ZonedDateTime zdt = ldt.atZone(ZoneId.systemDefault());
Instant instant = zdt.toInstant();
long millis = instant.toEpochMilli();

Or my alternative:

或者我的选择:

LocalDateTime ldt = LocalDateTime.now();
Instant instant = ldt.toInstant(ZoneOffset.UTC);
long millis = instant.toEpochMilli();

The difference is that I am skipping creating a Java 8 ZonedDateTime and instead passing the timezone offset to the toInstant()method.

不同之处在于我跳过创建 Java 8 ZonedDateTime 而是将时区偏移量传递给toInstant()方法。

From this point on the two answers are the same.

从这一点来看,两个答案是相同的。

org.joda.time.LocalDateTime jodaLocalDateTime = new org.joda.time.LocalDateTime(millis);

Caveats
This works well when converting using a consistent offset where no daylight savings changes apply. For example UTC is always +0:00. If you need to convert to a local timezone where the offset can change then you'll need the slightly longer answer using atZone()

警告
这在使用不适用夏令时更改的一致偏移量进行转换时效果很好。例如 UTC 总是 +0:00。如果您需要转换为偏移量可以改变的本地时区,那么您需要使用稍长的答案atZone()

A LocalDateTime with both API's (Java 8 and Joda) is a DateTime without a timezone. However, if you have the number of millis since the epoch then you need an offset to derive a date and time. The Java 8 API requires either an offset or timezone to be explicitly passed in, whereas the Joda API will use the system default if none is passed.

带有两个 API(Java 8 和 Joda)的 LocalDateTime 是没有时区的 DateTime。但是,如果您有自纪元以来的毫秒数,那么您需要一个偏移量来导出日期和时间。Java 8 API 需要显式传入偏移量或时区,而 Joda API 如果没有传递将使用系统默认值。

A more precise way to construct the Joda LocalDateTime would be:

构建 Joda LocalDateTime 的更精确方法是:

org.joda.time.LocalDateTime jodaLocalDateTime = new org.joda.time.LocalDateTime(millis, DateTimeZone.UTC);

This will only use the timezone during construction to get the correct date and time. Once the constructor has completed the timezone will not form part of the object since LocalDateTime has no timezone.

这只会在构建期间使用时区来获取正确的日期和时间。构造函数完成后,时区将不构成对象的一部分,因为 LocalDateTime 没有时区。