java Joda LocalDate 到 LocalDateTime
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29272356/
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
Joda LocalDate To LocalDateTime
提问by Suganthan Madhavan Pillai
I am trying to convert Joda LocalDate
to Joda LocalDateTime
, for that I am using the method toLocalDateTime(LocalTime.MIDNIGHT)
, as of now it is working
fine for example: for the given joda Localdate 2025-02-28
I getting the expected joda LocalDateTime 2025-02-28T00:00:00.000
, But my fear is, whether this method works fine at all situation. For example during dayLight saving
time zone anomalies
..etc..
我正在尝试转换Joda LocalDate
为Joda LocalDateTime
,因为我正在使用该方法toLocalDateTime(LocalTime.MIDNIGHT)
,到目前为止它工作正常,例如:对于给定的 joda Localdate2025-02-28
我得到了预期的 joda LocalDateTime 2025-02-28T00:00:00.000
,但我担心的是,这种方法是否在所有情况下都能正常工作. 例如during dayLight saving
time zone anomalies
..等..
Update:I did a small research on this question, here it is
更新:我对这个问题做了一个小研究,这里是
toLocalDateTime(LocalTime time)
Documentationsaying that: Converts LocalDate object to a LocalDateTime with LocalTime
to fill in the missing fields.
toLocalDateTime(LocalTime time)
文档说:将 LocalDate 对象转换为 LocalDateTimeLocalTime
以填充缺少的字段。
As I initializing LocalTime
with LocalTime.MIDNIGHT
, from hereLocalTime.MIDNIGHT
is a static final field initialized to new LocalTime(0, 0, 0, 0);
, you can see that it is a time values are hardcode to zero values with ISOChronology getInstanceUTC()
, so I think I will get the required output without any issue.
当我用 初始化LocalTime
时LocalTime.MIDNIGHT
,这里LocalTime.MIDNIGHT
是一个初始化为 的静态最终字段new LocalTime(0, 0, 0, 0);
,您可以看到这是一个时间值被硬编码为零值ISOChronology getInstanceUTC()
,所以我想我会毫无问题地获得所需的输出。
回答by Ortomala Lokni
From the documentation, we know that
从文档中,我们知道
LocalDate is an immutable datetime class representing a date without a time zone.
LocalDateTime is an unmodifiable datetime class representing a datetime without a time zone.
Internally, LocalDateTime uses a single millisecond-based valueto represent the local datetime. This value is only used internally and is not exposed to applications.
Calculations on LocalDate are performed using a Chronology. This chronology will be set internally to be in the UTC time zone for all calculations.
LocalDate 是一个不可变的日期时间类,表示没有时区的日期。
LocalDateTime 是一个不可修改的日期时间类,表示没有时区的日期时间。
在内部,LocalDateTime 使用一个基于毫秒的值来表示本地日期时间。此值仅在内部使用,不会向应用程序公开。
LocalDate 的计算是使用 Chronology 进行的。此年表将在内部设置为所有计算的 UTC 时区。
We also know that the toLocalDateTime
method of LocalDate
class is implemented like this:
我们也知道,toLocalDateTime
方法LocalDate
类等来实现此:
public LocalDateTime toLocalDateTime(LocalTime time) {
if (time == null) {
throw new IllegalArgumentException("The time must not be null");
}
if (getChronology() != time.getChronology()) {
throw new IllegalArgumentException("The chronology of the time does not match");
}
long localMillis = getLocalMillis() + time.getLocalMillis();
return new LocalDateTime(localMillis, getChronology());
}
Considering also that UTC has no Daylight saving time, we can conclude that you don't have to fear daylight saving problems nor time zone anomalies using the toLocalDateTime
method because this method dont deal with timezones.
还考虑到UTC 没有夏令时,我们可以得出结论,您不必担心使用该toLocalDateTime
方法的夏令时问题或时区异常,因为该方法不处理时区。