11package com .clickhouse .client .api ;
22
3- import java .time .Instant ;
4- import java .time .ZoneId ;
53import com .clickhouse .client .api .data_formats .internal .BinaryStreamReader ;
4+ import com .clickhouse .data .ClickHouseDataType ;
65
6+ import java .sql .Time ;
7+ import java .sql .Timestamp ;
78import java .time .Instant ;
9+ import java .time .LocalDateTime ;
810import java .time .ZoneId ;
11+ import java .time .ZoneOffset ;
12+ import java .time .ZonedDateTime ;
913import java .time .format .DateTimeFormatter ;
1014import java .time .format .DateTimeFormatterBuilder ;
1115import java .time .temporal .ChronoField ;
16+ import java .time .temporal .Temporal ;
17+ import java .util .Date ;
1218import java .util .Objects ;
1319
14- import com .clickhouse .data .ClickHouseDataType ;
15-
1620import static com .clickhouse .client .api .data_formats .internal .BinaryStreamReader .BASES ;
1721
1822public class DataTypeUtils {
@@ -39,6 +43,19 @@ public class DataTypeUtils {
3943 .appendFraction (ChronoField .NANO_OF_SECOND , 9 , 9 , true )
4044 .toFormatter ();
4145
46+ public static DateTimeFormatter TIME_FORMATTER = new DateTimeFormatterBuilder ()
47+ .appendFraction (ChronoField .HOUR_OF_DAY , 1 , 4 , false )
48+ .appendFraction (ChronoField .MINUTE_OF_HOUR , 1 , 3 , false )
49+ .appendFraction (ChronoField .SECOND_OF_MINUTE , 1 , 3 , false )
50+ .toFormatter ();
51+
52+ public static DateTimeFormatter TIME_WITH_NANOS_FORMATTER = new DateTimeFormatterBuilder ()
53+ .appendFraction (ChronoField .HOUR_OF_DAY , 1 , 4 , false )
54+ .appendFraction (ChronoField .MINUTE_OF_HOUR , 1 , 3 , false )
55+ .appendFraction (ChronoField .SECOND_OF_MINUTE , 1 , 3 , false )
56+ .appendFraction (ChronoField .NANO_OF_SECOND , 9 , 9 , true )
57+ .toFormatter ();
58+
4259 /**
4360 * Formats an {@link Instant} object for use in SQL statements or as query
4461 * parameter.
@@ -138,4 +155,60 @@ public static Instant instantFromTime64Integer(int precision, long value) {
138155
139156 return Instant .ofEpochSecond (value , nanoSeconds );
140157 }
158+
159+ /**
160+ * Converts a Java object to a temporal accessor suitable for date only operations.
161+ * It accepts Time and Date and converts to LocalDateTime
162+ * @param date
163+ * @return
164+ */
165+ public static Temporal toLocalDateTemporal (Object date ) {
166+ if (date instanceof Temporal ) {
167+ return (Temporal ) date ;
168+ } else if (date instanceof java .sql .Date ) {
169+ return ((java .sql .Date ) date ).toLocalDate ();
170+ } else if (date instanceof Timestamp ) {
171+ return ((java .sql .Timestamp ) date ).toLocalDateTime ().toLocalDate ();
172+ } else if (date instanceof java .util .Date ) {
173+ return ((java .util .Date ) date ).toInstant ().atZone (ZoneId .systemDefault ()).toLocalDate ();
174+ }
175+
176+ throw new IllegalArgumentException ("Object of type '" + date .getClass () + "' cannot be converted to local date because has no date part" );
177+ }
178+
179+ /**
180+ * Converts a Java object to a temporal accessor suitable for date and time operations.
181+ * @param date
182+ * @return
183+ */
184+ public static Temporal toLocalDateTimeTemporal (Object date ) {
185+ if (date instanceof Temporal ) {
186+ return (Temporal ) date ;
187+ } else if (date instanceof java .sql .Timestamp ) {
188+ return ((java .sql .Timestamp ) date ).toLocalDateTime ();
189+ } else if (date instanceof java .util .Date ) {
190+ return ((java .util .Date ) date ).toInstant ().atZone (ZoneId .systemDefault ()).toLocalDateTime ();
191+ }
192+
193+ throw new IllegalArgumentException ("Object of type '" + date .getClass () + "' cannot be converted to local date because has no date or time part" );
194+ }
195+
196+ /**
197+ * Converts a Java object to a temporal accessor suitable for time only operations.
198+ * @param date
199+ * @return
200+ */
201+ public static Temporal toLocalTimeTemporal (Object date ) {
202+ if (date instanceof Temporal ) {
203+ return (Temporal ) date ;
204+ } else if (date instanceof Timestamp ) {
205+ return ((Timestamp ) date ).toLocalDateTime ();
206+ } else if (date instanceof Time ) {
207+ return ((Time ) date ).toLocalTime ();
208+ } else if (date instanceof java .util .Date ) {
209+ return ((java .util .Date ) date ).toInstant ().atZone (ZoneId .systemDefault ()).toLocalTime ();
210+ }
211+
212+ throw new IllegalArgumentException ("Object of type '" + date .getClass () + "' cannot be converted to local time because has no time part" );
213+ }
141214}
0 commit comments