ppkarwasz commented on code in PR #1385:
URL: https://github.com/apache/commons-lang/pull/1385#discussion_r2094453585
##########
src/main/java/org/apache/commons/lang3/time/DateUtils.java:
##########
@@ -1625,6 +1628,34 @@ public static Calendar toCalendar(final Date date, final
TimeZone tz) {
return c;
}
+ /**
+ * Converts a {@link Date} into a {@link LocalDateTime}, using the default
time zone.
+ * @param date the date to convert to a LocalDateTime
+ * @return the created LocalDateTime
+ * @throws NullPointerException if {@code date} is null
+ * @since 3.18
+ */
+ public static LocalDateTime toLocalDateTime(final Date date) {
+ return toLocalDateTime(date, TimeZone.getDefault());
+ }
+
+ /**
+ * Converts a {@link Date} into a {@link LocalDateTime}
+ * @param date the date to convert to a LocalDateTime
+ * @param tz the time zone of the {@code date}
+ * @return the created LocalDateTime
+ * @throws NullPointerException if {@code date} is null
+ * @since 3.18
+ */
+ public static LocalDateTime toLocalDateTime(final Date date, final
TimeZone tz) {
+ final LocalDateTime localDateTime =
LocalDateTime.ofInstant(Instant.ofEpochMilli(Objects.requireNonNull(date,
"date").getTime()),
+ Objects.requireNonNull(tz, "tz").toZoneId());
+ if (date instanceof java.sql.Timestamp) {
+ return localDateTime.withNano(((Timestamp) date).getNanos());
+ }
Review Comment:
@yuyu1025, thanks!
> An app depending on Lang behaves magically differently depending on input
data, completely unbeknownst to the developer, who then has to investigate and
tell downstream users or other developers that their app or library needs
additional dependencies.
@garydgregory, the behavior of `toLocalDateTime` will not change based on
which Java 9 modules the application uses—it will depend solely on the type of
the argument provided:
- If the input is a `java.sql.Timestamp`, then the application must include
the `java.sql` module as a dependency.
- If the application does not include `java.sql`, then it simply cannot
construct or pass a `java.sql.Timestamp`, so this scenario doesn't arise.
In other words, the function's behavior is entirely predictable and
consistent: the presence or absence of `java.sql` only matters if you
explicitly use `java.sql.Timestamp`.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]