Commit 07fb413
committed
Add Date to LocalDateTime conversion methods and test cases
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.1 parent 4650d39 commit 07fb413
2 files changed
Lines changed: 89 additions & 0 deletions
File tree
- src
- main/java/org/apache/commons/lang3/time
- test/java/org/apache/commons/lang3/time
Lines changed: 25 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
18 | 18 | | |
19 | 19 | | |
20 | 20 | | |
| 21 | + | |
| 22 | + | |
21 | 23 | | |
22 | 24 | | |
23 | 25 | | |
| |||
1625 | 1627 | | |
1626 | 1628 | | |
1627 | 1629 | | |
| 1630 | + | |
| 1631 | + | |
| 1632 | + | |
| 1633 | + | |
| 1634 | + | |
| 1635 | + | |
| 1636 | + | |
| 1637 | + | |
| 1638 | + | |
| 1639 | + | |
| 1640 | + | |
| 1641 | + | |
| 1642 | + | |
| 1643 | + | |
| 1644 | + | |
| 1645 | + | |
| 1646 | + | |
| 1647 | + | |
| 1648 | + | |
| 1649 | + | |
| 1650 | + | |
| 1651 | + | |
| 1652 | + | |
1628 | 1653 | | |
1629 | 1654 | | |
1630 | 1655 | | |
| |||
Lines changed: 64 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
28 | 28 | | |
29 | 29 | | |
30 | 30 | | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
31 | 34 | | |
32 | 35 | | |
33 | 36 | | |
| |||
1285 | 1288 | | |
1286 | 1289 | | |
1287 | 1290 | | |
| 1291 | + | |
| 1292 | + | |
| 1293 | + | |
| 1294 | + | |
| 1295 | + | |
| 1296 | + | |
| 1297 | + | |
| 1298 | + | |
| 1299 | + | |
| 1300 | + | |
| 1301 | + | |
| 1302 | + | |
| 1303 | + | |
| 1304 | + | |
| 1305 | + | |
| 1306 | + | |
| 1307 | + | |
| 1308 | + | |
| 1309 | + | |
| 1310 | + | |
| 1311 | + | |
| 1312 | + | |
| 1313 | + | |
| 1314 | + | |
| 1315 | + | |
| 1316 | + | |
| 1317 | + | |
| 1318 | + | |
| 1319 | + | |
| 1320 | + | |
| 1321 | + | |
| 1322 | + | |
| 1323 | + | |
| 1324 | + | |
| 1325 | + | |
| 1326 | + | |
| 1327 | + | |
| 1328 | + | |
| 1329 | + | |
| 1330 | + | |
| 1331 | + | |
| 1332 | + | |
| 1333 | + | |
| 1334 | + | |
| 1335 | + | |
| 1336 | + | |
| 1337 | + | |
| 1338 | + | |
| 1339 | + | |
| 1340 | + | |
| 1341 | + | |
| 1342 | + | |
| 1343 | + | |
| 1344 | + | |
| 1345 | + | |
| 1346 | + | |
| 1347 | + | |
| 1348 | + | |
| 1349 | + | |
| 1350 | + | |
| 1351 | + | |
1288 | 1352 | | |
1289 | 1353 | | |
1290 | 1354 | | |
| |||
0 commit comments