Skip to content

Commit 6fbe283

Browse files
committed
support for conversion from java.util.Date to LocalDate/LocalDateTime
1 parent 85fecc9 commit 6fbe283

File tree

3 files changed

+49
-0
lines changed

3 files changed

+49
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
99

1010
### Changed
1111

12+
- support for conversion from java.util.Date to LocalDate/LocalDateTime
1213
- fj-bom version set to 1.6.5
1314
- DBUtils.indentifyDB() now recognizes h2 (600) and hsqldb (700) databases.
1415
- IdGenerator for h2 is mapped to Postgres by default

fj-core/src/main/java/org/fugerit/java/core/db/daogen/SQLTypeConverter.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import java.sql.SQLException;
55
import java.sql.Time;
66
import java.sql.Timestamp;
7+
import java.time.*;
78
import java.util.function.Function;
89

910
import org.fugerit.java.core.function.SafeFunction;
@@ -33,5 +34,20 @@ public static ByteArrayDataHandler blobToByteHandler( java.sql.Blob s ) throws S
3334
public static CharArrayDataHandler clobToCharHandler( java.sql.Clob s ) throws SQLException {
3435
return SafeFunction.getEx( () -> CharArrayDataHandler.newHandlerDefault( s ) , CONVERT_EX );
3536
}
37+
38+
public static ZonedDateTime toZonedDateTime(java.util.Date dateToConvert) {
39+
return SafeFunction.getIfNotNull( dateToConvert , () ->
40+
new java.util.Date( dateToConvert.getTime() )
41+
.toInstant()
42+
.atZone(ZoneId.systemDefault()) );
43+
}
44+
45+
public static LocalDate utilDateToLocalDate(java.util.Date dateToConvert) {
46+
return SafeFunction.getIfNotNull( dateToConvert , () -> toZonedDateTime( dateToConvert ).toLocalDate() );
47+
}
48+
49+
public static LocalDateTime utilDateToLocalDateTime(java.util.Date dateToConvert) {
50+
return SafeFunction.getIfNotNull( dateToConvert , () -> toZonedDateTime( dateToConvert ).toLocalDateTime() );
51+
}
3652

3753
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package test.org.fugerit.java.core.db.daogein;
2+
3+
import lombok.extern.slf4j.Slf4j;
4+
import org.fugerit.java.core.db.daogen.SQLTypeConverter;
5+
import org.junit.Assert;
6+
import org.junit.Test;
7+
8+
import java.sql.Date;
9+
import java.sql.Timestamp;
10+
import java.time.LocalDate;
11+
import java.time.LocalDateTime;
12+
13+
@Slf4j
14+
public class TestSQLTypeConverter {
15+
16+
private static final int TEST_YEAR = 2024;
17+
18+
@Test
19+
public void testLocalDate() {
20+
LocalDate d = SQLTypeConverter.utilDateToLocalDate(Date.valueOf( TEST_YEAR+"-05-05" ));
21+
log.info( "local date : {}", d );
22+
Assert.assertEquals( TEST_YEAR, d.getYear() );
23+
}
24+
25+
@Test
26+
public void testLocalDateTime() {
27+
LocalDateTime d = SQLTypeConverter.utilDateToLocalDateTime(Timestamp.valueOf( TEST_YEAR+"-05-05 11:30:00.000" ));
28+
log.info( "local date time : {}", d );
29+
Assert.assertEquals( TEST_YEAR, d.getYear() );
30+
}
31+
32+
}

0 commit comments

Comments
 (0)