9

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..

Update: I did a small research on this question, here it is

toLocalDateTime(LocalTime time) Documentation saying that: Converts LocalDate object to a LocalDateTime with LocalTime to fill in the missing fields.

As I initializing LocalTime with LocalTime.MIDNIGHT, from here LocalTime.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.

  • 2
    You seem to be asking whether there's likely to be a bug in the Joda library. I guess anything is possible, but I'd say it's probably been tested much better than anything you're likely to write for yourself. – Dawood ibn Kareem Mar 26 '15 at 06:34
  • FYI, the [Joda-Time](http://www.joda.org/joda-time/) project is now in [maintenance mode](https://en.wikipedia.org/wiki/Maintenance_mode), with the team advising migration to the [java.time](http://docs.oracle.com/javase/9/docs/api/java/time/package-summary.html) classes. See [Tutorial by Oracle](https://docs.oracle.com/javase/tutorial/datetime/TOC.html). `java.time.LocalDate.parse( "2025-02-28" ).atStartOfDay()` – Basil Bourque Dec 31 '17 at 02:26

1 Answers1

12

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 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.

We also know that the toLocalDateTime method of LocalDate class is implemented like 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());
}

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.

Community
  • 1
  • 1
Ortomala Lokni
  • 56,620
  • 24
  • 188
  • 240