File tree Expand file tree Collapse file tree 3 files changed +49
-0
lines changed
main/java/org/fugerit/java/core/db/daogen
test/java/test/org/fugerit/java/core/db/daogein Expand file tree Collapse file tree 3 files changed +49
-0
lines changed Original file line number Diff line number Diff 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
Original file line number Diff line number Diff line change 44import java .sql .SQLException ;
55import java .sql .Time ;
66import java .sql .Timestamp ;
7+ import java .time .*;
78import java .util .function .Function ;
89
910import 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}
Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments