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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With