Joda LocalDate 到 LocalDateTime

Joda LocalDate To LocalDateTime

我正在尝试将 Joda LocalDate 转换为 Joda LocalDateTime,因为我正在使用 toLocalDateTime(LocalTime.MIDNIGHT) 方法,目前它正在运行 很好,例如:对于给定的 joda Localdate 2025-02-28,我得到了预期的 joda LocalDateTime 2025-02-28T00:00:00.000,但我担心的是,这种方法是否在所有情况下都能正常工作。例如 during dayLight saving time zone anomalies..等..

更新:我对这个问题做了一个小小的研究,这里是

toLocalDateTime(LocalTime time) Documentation 表示:使用 LocalTime 将 LocalDate 对象转换为 LocalDateTime 以填充缺失的字段。

因为我用LocalTime.MIDNIGHT初始化LocalTime,从here LocalTime.MIDNIGHT是一个初始化为new LocalTime(0, 0, 0, 0);的static final字段,你可以看到它是使用 ISOChronology getInstanceUTC() 将时间值硬编码为零值,因此我认为我将毫无问题地获得所需的输出。

documentation我们知道

  • 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 value to 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.

我们也知道LocalDateclass的toLocalDateTime方法是这样实现的this:

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());
}

同时考虑到 UTC has no Daylight saving time,我们可以得出结论,您不必担心使用 toLocalDateTime 方法的夏令时问题或时区异常,因为此方法不处理时区。