xufei 2 years ago
parent
commit
76d51eab44
1 changed files with 52 additions and 0 deletions
  1. 52 0
      src/main/java/com/winhc/bigdata/udf/ObjectId2Date.java

+ 52 - 0
src/main/java/com/winhc/bigdata/udf/ObjectId2Date.java

@@ -0,0 +1,52 @@
+package com.winhc.bigdata.udf;
+
+import com.aliyun.odps.udf.UDF;
+
+import java.text.DateFormat;
+import java.text.SimpleDateFormat;
+import java.time.Instant;
+import java.util.Date;
+import java.util.List;
+import java.util.stream.Collectors;
+import java.util.stream.Stream;
+
+/**
+ * @Author: π
+ * @Description: 公司名转换
+ */
+public class ObjectId2Date extends UDF {
+
+
+
+    public String evaluate(String objectId) {
+        return toDateString(convertToDateFrom(objectId));
+    }
+
+    public static Instant convertToInstantFrom(String objectId) {
+        return convertToDateFrom(objectId).toInstant();
+    }
+
+    public static Date convertToDateFrom(String objectId) {
+        return new Date(convertToTimestampFrom(objectId));
+    }
+
+    public static long convertToTimestampFrom(String objectId) {
+        return Long.parseLong(objectId.substring(0, 8), 16) * 1000;
+    }
+
+    public static String toDateString(Date date) {
+        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
+        return dateFormat.format(date);
+    }
+
+    public static void main(String[] args) {
+            Stream.of("61bab6735c53fc56ba0ed815", "622ac651dae2eb5aa9510f18", "598ad932e4b0f0f169b42d5f")
+                    .map(ObjectId2Date::convertToInstantFrom).forEach(System.out::println);
+
+        Stream.of("6220972eaaa0264735adc8cb", "622ac651dae2eb5aa9510f18", "598ad932e4b0f0f169b42d5f")
+                .map(x->new ObjectId2Date().evaluate(x)).forEach(System.out::println);
+    }
+
+}
+
+