Jelajahi Sumber

日期规整

xufei 2 tahun lalu
induk
melakukan
d142c1d774
1 mengubah file dengan 42 tambahan dan 0 penghapusan
  1. 42 0
      src/main/java/com/winhc/bigdata/udf/str_date.java

+ 42 - 0
src/main/java/com/winhc/bigdata/udf/str_date.java

@@ -0,0 +1,42 @@
+package com.winhc.bigdata.udf;
+
+import com.aliyun.odps.udf.UDF;
+import com.aliyun.odps.utils.StringUtils;
+import java.text.SimpleDateFormat;
+
+/**
+ * @Author: π
+ * @Description: 字符串转date格式
+ */
+public class str_date extends UDF {
+
+    public static final String FORMAT_YYYY_MM = "yyyy-MM-dd";
+
+    public String evaluate(String date) {
+        if (StringUtils.isBlank(date)) {
+            return null;
+        }
+        String d = date.split(" ")[0];
+        try {
+            SimpleDateFormat dateFormat = new SimpleDateFormat(FORMAT_YYYY_MM);
+            dateFormat.parse(d);
+        } catch (Exception e) {
+            // do nothing
+            e.printStackTrace();
+            return null;
+        }
+        return d;
+    }
+
+
+    public static void main(String[] args) {
+        //String date = "2020-01-15 08:37:13";
+        //String date = "2020-01-15";
+        String date = "2020-01.15";
+        str_date n = new str_date();
+        System.out.println(n.evaluate(date));
+    }
+
+}
+
+