|
24 | 24 | import com.google.common.io.BaseEncoding; |
25 | 25 | import java.io.Serializable; |
26 | 26 | import java.math.BigDecimal; |
| 27 | +import java.text.DateFormat; |
| 28 | +import java.text.SimpleDateFormat; |
27 | 29 | import java.util.ArrayList; |
| 30 | +import java.util.Date; |
28 | 31 | import java.util.List; |
29 | 32 | import javax.annotation.Nullable; |
30 | 33 | import org.threeten.bp.Instant; |
@@ -67,6 +70,7 @@ public abstract class QueryParameterValue implements Serializable { |
67 | 70 | DateTimeFormatter.ofPattern("HH:mm:ss.SSSSSS"); |
68 | 71 | private static final DateTimeFormatter datetimeFormatter = |
69 | 72 | DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSSSSS"); |
| 73 | + private static final DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); |
70 | 74 |
|
71 | 75 | static final Function< |
72 | 76 | QueryParameterValue, com.google.api.services.bigquery.model.QueryParameterValue> |
@@ -256,6 +260,8 @@ private static <T> StandardSQLTypeName classToType(Class<T> type) { |
256 | 260 | return StandardSQLTypeName.FLOAT64; |
257 | 261 | } else if (BigDecimal.class.isAssignableFrom(type)) { |
258 | 262 | return StandardSQLTypeName.NUMERIC; |
| 263 | + } else if (Date.class.isAssignableFrom(type)) { |
| 264 | + return StandardSQLTypeName.DATE; |
259 | 265 | } |
260 | 266 | throw new IllegalArgumentException("Unsupported object type for QueryParameter: " + type); |
261 | 267 | } |
@@ -310,6 +316,9 @@ private static <T> String valueToStringOrNull(T value, StandardSQLTypeName type) |
310 | 316 | // verify that the String is in the right format |
311 | 317 | checkFormat(value, dateFormatter); |
312 | 318 | return (String) value; |
| 319 | + } else if (value instanceof Date) { |
| 320 | + checkFormat(value, dateFormat); |
| 321 | + return dateFormat.format(value); |
313 | 322 | } |
314 | 323 | break; |
315 | 324 | case TIME: |
@@ -341,6 +350,14 @@ private static void checkFormat(Object value, DateTimeFormatter formatter) { |
341 | 350 | } |
342 | 351 | } |
343 | 352 |
|
| 353 | + private static void checkFormat(Object value, DateFormat format) { |
| 354 | + try { |
| 355 | + format.format(value); |
| 356 | + } catch (Exception e) { |
| 357 | + throw new IllegalArgumentException(e.getMessage(), e); |
| 358 | + } |
| 359 | + } |
| 360 | + |
344 | 361 | /** Returns a builder for a QueryParameterValue object with given value. */ |
345 | 362 | public abstract Builder toBuilder(); |
346 | 363 |
|
|
0 commit comments