-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Add Date to LocalDateTime conversion methods to bridge legacy and modern date APIs #1385
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
-1: The implementation doesn't make sense. I suppose the question is: Do we really need a utility method for:
LocalDateTime.ofInstant(date.toInstant(), timeZone.toZoneId());
For experienced developers, this indeed doesn't make much sense. However, for newbies who know nothing about LocalDateTime, it can often be of great help. In project engineering, it can also reduce a lot of repetitive logic. |
If you also consider
When you have to deal with legacy code that uses |
27088ad
to
4d69373
Compare
You raised a very good question, I have handled all subclasses and completed the unit tests, which will greatly improve efficiency for the development of projects involving both old and new systems. |
fe3e661
to
6fb19d6
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you so much for the updates you’ve made to this pull request—I really appreciate the effort you’ve put in so far. Before we dive into the points Gary raised about the method’s utility, let’s take a moment to clean things up a bit more. Once that’s done, we’ll be in a better place to evaluate the next steps together.
I noticed you've enabled "Vigilant mode", but it looks like your GPG key isn’t added to your GitHub account—so your commits are showing as “Unverified.” That’s probably not what you intended. To resolve this, add your GPG key to your GitHub account to verify that you are the author of your commits. |
This commit enhances the DateUtils class by adding two new methods to convert java.util.Date to java.time.LocalDateTime, supporting both default and specified time zones. The implementation leverages Java 8's date-time API for accurate time zone handling, including: - `toLocalDateTime(Date date)`: Uses the system's default time zone - `toLocalDateTime(Date date, TimeZone tz)`: Explicitly specifies time zone via TimeZone parameter Key improvements: - Proper null checking with Objects.requireNonNull() - Direct conversion via Instant and ZoneId for timezone accuracy - Maintains parity with existing DateUtils method naming conventions Test cases added in DateUtilsTest: - Default time zone conversion (Asia/Shanghai example) - Specified time zone conversion (America/New_York) - Null input validation (throws NullPointerException) - Daylight saving time handling (America/New_York DST case) - Extreme time zone test (Pacific/Kiritimati, UTC+14) These changes enable seamless integration between legacy Date APIs and modern LocalDateTime, while ensuring robust timezone handling across different environments.
…atible subclasses (such as java.sql.Date).
…stamp **What** 1. Added extensive unit tests covering all java.sql date/time subclasses (Date, Time, Timestamp) 2. Implemented nanosecond precision support for java.sql.Timestamp conversions 3. Enhanced timezone handling to ensure correct conversion across different time zones 4. Added daylight saving time (DST) edge case testing **Why** 1. Existing tests lacked coverage for sql subclasses and edge cases 2. Timestamp conversions were losing nanosecond precision 3. Timezone handling was inconsistent between Date and Timestamp types
Co-authored-by: Piotr P. Karwasz <[email protected]>
3044828
to
0d479ab
Compare
…rized tests - Remove multiple individual @test methods for SQL date/time/timestamp conversions - Replace with 2 parameterized tests using @ParameterizedTest and @MethodSource - Introduce test data providers: - dateConversionProvider() for basic conversions (SQL Date/Time/Timestamp) - dateWithTimeZoneProvider() for timezone-aware conversions - Maintain full test coverage for: - Epoch date (1970-01-01) - Max time values (23:59:59) - Nano precision timestamps - Default/system time zone handling - Specific time zones (America/New_York, Asia/Shanghai, Pacific/Kiritimati) - Daylight Saving Time transitions - Improve test maintainability by reducing code duplication - Add null argument validation test for time zone parameter Signed-off-by: finger <[email protected]>
e8faf11
to
f7928be
Compare
…dule dependency - Introduce static reflection initialization for Timestamp class/method - Use isTimestamp() to check instance type without direct dependency - Extract nanoseconds via reflection in extractNanosFromSqlTimestamp() - Gracefully handle absence of java.sql module with null checks and exception handling - Maintain existing functionality for converting Timestamps to LocalDateTime with nanosecond precision - Ensure compatibility with both modular (JPMS) and non-modular environments - Remove unused Instant import in test class for cleaner imports Signed-off-by: finger <[email protected]>
I understand, thank you for the reminder. |
<!-- | ||
~ Modifies the inherited Moditect configuration to add `java.sql` as `static` (optional) dependency. | ||
--> | ||
<plugin> | ||
<groupId>org.moditect</groupId> | ||
<artifactId>moditect-maven-plugin</artifactId> | ||
<executions> | ||
<execution> | ||
<id>add-module-infos</id> | ||
<configuration> | ||
<module> | ||
<moduleInfo> | ||
<requires> | ||
static java.sql; | ||
*; | ||
</requires> | ||
</moduleInfo> | ||
</module> | ||
</configuration> | ||
</execution> | ||
</executions> | ||
</plugin> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I attempted to update the inherited Moditect Maven Plugin configuration to include the following directive in the generated module descriptor:
requires static java.sql;
However, due to moditect/moditect#262, this change currently has no effect.
@garydgregory, should we keep the directive in place in anticipation of the issue being fixed, or remove it for now and reintroduce it once the plugin supports it correctly?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ppkarwasz
Well, no, since we shouldn't depend on java[x].sql in the first place.
All,
Maybe this kind of this-to-that conversion code belongs in Commons BeanUtils.
In Lang, DateUtils has only one kind of conversion ATM with 'toCalendar'(...). I'm not sure we want to open the door to more convertions, then there would be endless combinations due to the scale of the Java Time package.
Signed-off-by: finger <[email protected]>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks good to me!
I would just make sure that each test has a test case with java.util.Date
, java.sql.Date
and java.sql.Timestamp
.
private static Stream<Arguments> dateConversionProvider() { | ||
return Stream.of( | ||
Arguments.of( | ||
java.sql.Date.valueOf("2000-01-01"), | ||
LocalDateTime.of(2000, 1, 1, 0, 0, 0) | ||
), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you add some test cases with java.util.Date
?
private static Stream<Arguments> dateWithTimeZoneProvider() { | ||
return Stream.of( | ||
Arguments.of( | ||
java.sql.Timestamp.valueOf("2000-01-01 12:30:45"), | ||
TimeZone.getTimeZone("America/New_York"), | ||
LocalDateTime.ofInstant( | ||
java.sql.Timestamp.valueOf("2000-01-01 12:30:45").toInstant(), | ||
TimeZone.getTimeZone("America/New_York").toZoneId() | ||
) | ||
), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you add some test cases with java.sql.Date
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you add some test cases with
java.sql.Date
?
OK, I'll add some now.
Signed-off-by: finger <[email protected]>
Signed-off-by: finger <[email protected]>
Background and Motivation
With the widespread adoption of Java 8+, the project has gradually moved from the traditional
java.util.Date
to modern date-time APIs (such asLocalDateTime
), but a large amount of existing code still relies onDate
. To reduce migration costs and improve development efficiency, it is necessary to provide officially supported conversion tools in Apache Commons Lang to avoid developers repeating the implementation of underlying logic.Core Change Content
New Conversion Methods
toLocalDateTime(Date date)
:Uses the system's default time zone, simplifying common conversion scenarios.toLocalDateTime(Date date, TimeZone tz)
:Explicitly specifies the time zone, meeting internationalization requirements (e.g., converting UTC time to local time in a specific time zone).Instant
andZoneId
, ensuring time zone accuracy (including daylight saving time, handling of extreme time zones).Improve test coverage
Value Added
LocalDateTime
without modifying the existingDate
interface, reducing refactoring risks.DateUtils
modern support to adapt to Java 8+ ecosystem, responding to the community's need for bridging new and old APIs.Compatibility Note
DateUtils
functionality.