turcsanyip commented on a change in pull request #4781:
URL: https://github.com/apache/nifi/pull/4781#discussion_r564138178



##########
File path: 
nifi-commons/nifi-record/src/main/java/org/apache/nifi/serialization/record/util/DataTypeUtils.java
##########
@@ -1085,6 +1087,28 @@ 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);
     }
 
+    /**
+     * Converts a java.sql.Date object with 00:00:00 in local time zone 
(typically coming from a java.sql.ResultSet)
+     * to UTC normalized form (storing epoch corresponding to UTC 00:00:00 on 
the given day).
+     *
+     * @param date java.sql.Date with local time zone 00:00:00
+     * @return java.sql.Date with UTC 00:00:00
+     */
+    public static Date convertDateToUTC(Date date) {
+        return new 
Date(date.toLocalDate().atStartOfDay(ZoneId.of("UTC")).toInstant().toEpochMilli());
+    }
+
+    /**
+     * Converts a java.sql.Date object with 00:00:00 in UTC
+     * to local time zone normalized form (storing epoch corresponding to 
00:00:00 in local time zone on the given day).
+     *
+     * @param date java.sql.Date with UTC 00:00:00
+     * @return java.sql.Date with local time zone 00:00:00
+     */
+    public static Date convertDateToLocalTZ(Date date) {
+        return 
Date.valueOf(Instant.ofEpochMilli(date.getTime()).atZone(ZoneId.of("UTC")).toLocalDate());

Review comment:
       Indeed... what do you think about this?
   ```
       public static Date convertDateToUTC(Date dateLocalTZ) {
           ZonedDateTime zdtLocalTZ = 
ZonedDateTime.ofInstant(Instant.ofEpochMilli(dateLocalTZ.getTime()), 
ZoneId.systemDefault());
           ZonedDateTime zdtUTC = 
zdtLocalTZ.withZoneSameLocal(ZoneId.of("UTC"));
           Date dateUTC = new Date(zdtUTC.toInstant().toEpochMilli());
           return dateUTC;
       }
   ```
   No `LocaDate` and I believe `withZoneSameLocal()` in the middle of the 
method really grasps what is going on here.
   The same can be applied to `convertDateToLocalTZ()` similarly.




----------------------------------------------------------------
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]


Reply via email to