Skip to content

Commit d17e0e4

Browse files
author
Vadim Platonov
committed
fix: to-sql-date returns a java.sql.Date
1 parent 4d83e2c commit d17e0e4

File tree

3 files changed

+19
-4
lines changed

3 files changed

+19
-4
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
## 0.3.0
2+
3+
### Fixed
4+
5+
* *Breaking*: `to-sql-date` converts anything convertible to `LocalDate` into a `java.sql.Date`.
6+
Previously `to-sql-date` worked the same as `to-java-date`.
7+
18
## 0.2.2
29

310
### Fixed

src/java_time/convert.clj

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
(ns java-time.convert
22
(:require [java-time.core :as jt.c]
33
[java-time.util :as jt.u]
4+
[java-time.local :as jt.l]
45
[java-time.properties :as jt.p]
56
[java-time.temporal :as jt.t])
67
(:import [java.time.temporal TemporalUnit ChronoUnit]
@@ -88,9 +89,12 @@
8889
(Date/from (jt.t/instant o))))
8990

9091
(defn ^java.sql.Date to-sql-date
91-
"Converts a date entity to a `java.sql.Date`."
92-
[o]
93-
(java.sql.Date/from (jt.t/instant o)))
92+
"Converts a local date entity to a `java.sql.Date`.
93+
94+
This function only has a single arity and works for entities directly
95+
convertible to `java.time.LocalDate`. Please consider using `sql-date`
96+
instead."
97+
[o] (java.sql.Date/valueOf (jt.l/local-date o)))
9498

9599
(defn ^java.sql.Timestamp to-sql-timestamp
96100
"Converts a date entity to a `java.sql.Timestamp`."

test/java_time_test.clj

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -605,13 +605,17 @@
605605
(testing "converts through instant"
606606
(is (= (j/instant 1000) (j/instant (java.util.Date. 1000))))
607607
(is (= (java.util.Date. 1000) (j/to-java-date 1000)))
608-
(is (= (java.sql.Date. 1000) (j/to-sql-date 1000)))
608+
(is (= (java.sql.Date/valueOf (j/local-date 1000)) (j/to-sql-date 1000)))
609609
(is (= (java.sql.Timestamp. 1000) (j/to-sql-timestamp 1000)))
610610
(is (= 1000
611611
(j/to-millis-from-epoch 1000)
612612
(j/to-millis-from-epoch (java.util.Date. 1000))
613613
(j/to-millis-from-epoch (j/offset-date-time (j/instant 1000) +0)))))
614614

615+
(testing "converts to java.util/sql Dates"
616+
(is (= (java.util.Date. 1000) (j/to-java-date (j/instant 1000))))
617+
(is (= (java.sql.Date/valueOf (j/local-date 2016)) (j/to-sql-date (j/local-date 2016)))))
618+
615619
(testing "from java.util Date types"
616620
(is (= (j/zone-id "UTC") (j/zone-id (java.util.TimeZone/getTimeZone "UTC"))))))
617621

0 commit comments

Comments
 (0)