-
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?
Changes from all commits
e99a544
79b1099
6ad245b
f7dcdb8
45003ab
0d479ab
f7928be
7d1692c
88b7114
36eb042
5d20c0b
eb906ba
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,19 +28,25 @@ | |
import java.text.DateFormat; | ||
import java.text.ParseException; | ||
import java.text.SimpleDateFormat; | ||
import java.time.LocalDateTime; | ||
import java.time.ZoneOffset; | ||
import java.util.Calendar; | ||
import java.util.Date; | ||
import java.util.GregorianCalendar; | ||
import java.util.Iterator; | ||
import java.util.Locale; | ||
import java.util.NoSuchElementException; | ||
import java.util.TimeZone; | ||
import java.util.stream.Stream; | ||
|
||
import org.apache.commons.lang3.AbstractLangTest; | ||
import org.junit.jupiter.api.AfterEach; | ||
import org.junit.jupiter.api.BeforeAll; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.Arguments; | ||
import org.junit.jupiter.params.provider.MethodSource; | ||
import org.junitpioneer.jupiter.DefaultLocale; | ||
import org.junitpioneer.jupiter.ReadsDefaultLocale; | ||
import org.junitpioneer.jupiter.WritesDefaultLocale; | ||
|
@@ -1285,6 +1291,107 @@ public void testToCalendarWithTimeZoneNull() { | |
assertThrows(NullPointerException.class, () -> DateUtils.toCalendar(date1, null)); | ||
} | ||
|
||
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) | ||
), | ||
Comment on lines
+1294
to
+1299
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you add some test cases with |
||
Arguments.of( | ||
java.sql.Date.valueOf("1970-01-01"), | ||
LocalDateTime.of(1970, 1, 1, 0, 0, 0) | ||
), | ||
Arguments.of( | ||
java.sql.Time.valueOf("12:30:45"), | ||
LocalDateTime.of(1970, 1, 1, 12, 30, 45) | ||
), | ||
Arguments.of( | ||
java.sql.Time.valueOf("23:59:59"), | ||
LocalDateTime.of(1970, 1, 1, 23, 59, 59) | ||
), | ||
Arguments.of( | ||
java.sql.Timestamp.valueOf("2000-01-01 12:30:45.123456789"), | ||
LocalDateTime.of(2000, 1, 1, 12, 30, 45, 123_456_789) | ||
), | ||
Arguments.of( | ||
java.sql.Timestamp.valueOf("2000-01-01 12:30:45.987654321"), | ||
LocalDateTime.of(2000, 1, 1, 12, 30, 45, 987_654_321) | ||
) | ||
); | ||
} | ||
|
||
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() | ||
) | ||
), | ||
Comment on lines
+1323
to
+1332
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you add some test cases with There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
OK, I'll add some now. |
||
Arguments.of( | ||
java.sql.Timestamp.valueOf("2023-03-12 02:30:00"), | ||
TimeZone.getTimeZone("America/New_York"), | ||
LocalDateTime.ofInstant( | ||
java.sql.Timestamp.valueOf("2023-03-12 02:30:00").toInstant(), | ||
TimeZone.getTimeZone("America/New_York").toZoneId() | ||
) | ||
), | ||
Arguments.of( | ||
Date.from(LocalDateTime.of(2023, 1, 1, 0, 0) | ||
.atOffset(ZoneOffset.UTC) | ||
.toInstant()), | ||
TimeZone.getTimeZone("America/New_York"), | ||
LocalDateTime.of(2022, 12, 31, 19, 0) | ||
), | ||
Arguments.of( | ||
Date.from(LocalDateTime.of(2023, 3, 12, 7, 0) | ||
.atOffset(ZoneOffset.UTC) | ||
.toInstant()), | ||
TimeZone.getTimeZone("America/New_York"), | ||
LocalDateTime.of(2023, 3, 12, 3, 0) | ||
), | ||
Arguments.of( | ||
Date.from(LocalDateTime.of(2023, 1, 1, 0, 0) | ||
.atOffset(ZoneOffset.UTC) | ||
.toInstant()), | ||
TimeZone.getTimeZone("Pacific/Kiritimati"), | ||
LocalDateTime.of(2023, 1, 1, 14, 0) | ||
) | ||
); | ||
} | ||
|
||
@ParameterizedTest | ||
@MethodSource("dateConversionProvider") | ||
void testToLocalDateTimeWithDate(final Date sqlDate, final LocalDateTime expected) { | ||
final LocalDateTime result = DateUtils.toLocalDateTime(sqlDate); | ||
assertNotNull(result); | ||
assertEquals(expected, result); | ||
} | ||
|
||
@ParameterizedTest | ||
@MethodSource("dateWithTimeZoneProvider") | ||
void testToLocalDateTimeWithDate( | ||
final Date date, | ||
final TimeZone timeZone, | ||
final LocalDateTime expected) { | ||
final LocalDateTime result; | ||
if (timeZone != null) { | ||
result = DateUtils.toLocalDateTime(date, timeZone); | ||
} else { | ||
result = DateUtils.toLocalDateTime(date); | ||
} | ||
assertEquals(expected, result); | ||
} | ||
|
||
@Test | ||
void shouldThrowNullPointerExceptionWhenDateIsNull() { | ||
assertThrows(NullPointerException.class, () -> DateUtils.toLocalDateTime(null)); | ||
assertThrows(NullPointerException.class, () -> DateUtils.toLocalDateTime(null, TimeZone.getDefault())); | ||
assertThrows(NullPointerException.class, () -> DateUtils.toLocalDateTime(new Date(), null)); | ||
} | ||
|
||
/** | ||
* Tests various values with the trunc method | ||
* | ||
|
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:
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.