瀏覽代碼

fix时间序列化

Eric 1 年之前
父節點
當前提交
69183fd780

+ 0 - 50
cooleshow-app/src/main/java/com/yonge/cooleshow/config/CustomStringDateConverter.java

@@ -1,50 +0,0 @@
-package com.yonge.cooleshow.config;
-
-import org.apache.commons.lang3.StringUtils;
-import org.springframework.context.annotation.Configuration;
-import org.springframework.core.convert.converter.Converter;
-
-import java.text.SimpleDateFormat;
-import java.util.Date;
-
-/**
- * 字符串格式转日期
- */
-@Configuration
-public class CustomStringDateConverter implements Converter<String, Date> {
-	private static final String dateFormat = "yyyy-MM-dd HH:mm:ss";
-	private static final String shortDateFormat = "yyyy-MM-dd";
-	private static final String dateFormat2 = "yyyy/MM/dd HH:mm:ss";
-	private static final String shortDateFormat2 = "yyyy/MM/dd";
-
-	@Override
-	public Date convert(String source) {
-		if (StringUtils.isBlank(source)) {
-			return null;
-		}
-		source = source.trim();
-		try {
-			SimpleDateFormat formatter;
-			if (source.contains("-")) {
-				if (source.contains(":")) {
-					formatter = new SimpleDateFormat(dateFormat);
-				} else {
-					formatter = new SimpleDateFormat(shortDateFormat);
-				}
-                return formatter.parse(source);
-			} else if (source.contains("/")) {
-				if (source.contains(":")) {
-					formatter = new SimpleDateFormat(dateFormat2);
-				} else {
-					formatter = new SimpleDateFormat(shortDateFormat2);
-				}
-                return formatter.parse(source);
-			}
-		} catch (Exception e) {
-			throw new RuntimeException(String.format("parser %s to Date fail", source));
-		}
-
-		throw new RuntimeException(String.format("parser %s to Date fail", source));
-	}
-}
-

+ 42 - 0
cooleshow-app/src/main/java/com/yonge/cooleshow/config/JacksonConfig.java

@@ -30,6 +30,7 @@ import org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilde
 import org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration;
 import org.springframework.context.annotation.Bean;
 import org.springframework.context.annotation.Configuration;
+import org.springframework.core.convert.converter.Converter;
 import org.springframework.util.StringUtils;
 
 import java.io.IOException;
@@ -257,4 +258,45 @@ public class JacksonConfig {
             }
         }
     }
+
+    /**
+     * 字符串格式转日期
+     */
+    public static class CustomStringDateConverter implements Converter<String, Date> {
+        private static final String dateFormat = "yyyy-MM-dd HH:mm:ss";
+        private static final String shortDateFormat = "yyyy-MM-dd";
+        private static final String dateFormat2 = "yyyy/MM/dd HH:mm:ss";
+        private static final String shortDateFormat2 = "yyyy/MM/dd";
+
+        @Override
+        public Date convert(String source) {
+            if (org.apache.commons.lang3.StringUtils.isBlank(source)) {
+                return null;
+            }
+            source = source.trim();
+            try {
+                SimpleDateFormat formatter;
+                if (source.contains("-")) {
+                    if (source.contains(":")) {
+                        formatter = new SimpleDateFormat(dateFormat);
+                    } else {
+                        formatter = new SimpleDateFormat(shortDateFormat);
+                    }
+                    return formatter.parse(source);
+                } else if (source.contains("/")) {
+                    if (source.contains(":")) {
+                        formatter = new SimpleDateFormat(dateFormat2);
+                    } else {
+                        formatter = new SimpleDateFormat(shortDateFormat2);
+                    }
+                    return formatter.parse(source);
+                }
+            } catch (Exception e) {
+                throw new RuntimeException(String.format("parser %s to Date fail", source));
+            }
+
+            throw new RuntimeException(String.format("parser %s to Date fail", source));
+        }
+    }
+
 }

+ 1 - 3
cooleshow-app/src/main/java/com/yonge/cooleshow/config/WebMvcConfig.java

@@ -31,8 +31,6 @@ public class WebMvcConfig implements WebMvcConfigurer {
 	private EnumConverterFactory enumConverterFactory;
 	@Autowired
 	private MDCInterceptor mdcInterceptor;
-	@Autowired
-	private CustomStringDateConverter customStringDateConverter;
 
 	/**
 	 * 枚举类的转换器 addConverterFactory
@@ -42,7 +40,7 @@ public class WebMvcConfig implements WebMvcConfigurer {
 		// 枚举转换器
 		registry.addConverterFactory(enumConverterFactory);
 		// 字符串时间转换器
-		registry.addConverter(customStringDateConverter);
+		registry.addConverter(new JacksonConfig.CustomStringDateConverter());
 	}
 
 	@Override