markap14 commented on a change in pull request #4734:
URL: https://github.com/apache/nifi/pull/4734#discussion_r560315700
##########
File path:
nifi-commons/nifi-record/src/main/java/org/apache/nifi/serialization/record/util/DataTypeUtils.java
##########
@@ -1040,6 +1049,93 @@ private static Object toEnum(Object value, EnumDataType
dataType, String fieldNa
throw new IllegalTypeConversionException("Cannot convert value " +
value + " of type " + dataType.toString() + " for field " + fieldName);
}
+ /**
+ * Convert value to Local Date with support for conversion from numbers or
formatted strings
+ *
+ * @param value Value to be converted
+ * @param formatter Supplier for Date Time Formatter can be null values
other than numeric strings
+ * @param fieldName Field Name for value to be converted
+ * @return Local Date or null when value to be converted is null
+ * @throws IllegalTypeConversionException Thrown when conversion from
string fails or unsupported value provided
+ */
+ public static LocalDate toLocalDate(final Object value, final
Supplier<DateTimeFormatter> formatter, final String fieldName) {
+ LocalDate localDate;
+
+ if (value == null) {
+ return null;
+ } else if (value instanceof LocalDate) {
+ localDate = (LocalDate) value;
+ } else if (value instanceof java.util.Date) {
+ final java.util.Date date = (java.util.Date) value;
+ localDate = parseLocalDateEpochMillis(date.getTime());
+ } else if (value instanceof Number) {
+ final long epochMillis = ((Number) value).longValue();
+ localDate = parseLocalDateEpochMillis(epochMillis);
+ } else if (value instanceof String) {
+ try {
+ localDate = parseLocalDate((String) value, formatter);
+ } catch (final RuntimeException e) {
+ final String message = String.format("Failed Conversion of
Field [%s] from String [%s] to LocalDate with Formatter [%s]", fieldName,
value, formatter, e);
Review comment:
Probably need to use `formatter.get()` here because the toString() of
the Supplier is unlikely to be implemented.
##########
File path:
nifi-commons/nifi-record/src/main/java/org/apache/nifi/serialization/record/util/DataTypeUtils.java
##########
@@ -1085,6 +1180,31 @@ private static Object toEnum(Object value, EnumDataType
dataType, String fieldNa
throw new IllegalTypeConversionException("Cannot convert value [" +
value + "] of type " + value.getClass() + " to Date for field " + fieldName);
}
+ private static Date parseDate(final String string, final DateFormat
dateFormat) throws ParseException {
+ // DateFormat.parse() creates java.util.Date with System Default Time
Zone
+ final java.util.Date parsed = dateFormat.parse(string);
+
+ Instant parsedInstant = parsed.toInstant();
+ if (isTimeZoneAdjustmentRequired(dateFormat)) {
+ // Adjust parsed date using System Default Time Zone offset
milliseconds when time zone format not found
+ parsedInstant =
parsedInstant.minus(TimeZone.getDefault().getRawOffset(), ChronoUnit.MILLIS);
+ }
+
+ return new Date(parsedInstant.toEpochMilli());
+ }
+
+ private static boolean isTimeZoneAdjustmentRequired(final DateFormat
dateFormat) {
+ boolean adjustmentRequired = false;
+
+ if (dateFormat instanceof SimpleDateFormat) {
+ final SimpleDateFormat simpleDateFormat = (SimpleDateFormat)
dateFormat;
+ final String pattern = simpleDateFormat.toPattern();
+ adjustmentRequired = !pattern.contains(TIME_ZONE_PATTERN);
Review comment:
I don't believe this is sufficient. According to
https://docs.oracle.com/javase/8/docs/api/java/text/SimpleDateFormat.html, the
timezone can be represented using `z`, `Z`, or `X`, each having a different
meaning. I think we have to account for all of these.
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]