diff --git a/UPDATE.md b/UPDATE.md index aef15f4..37be89b 100644 --- a/UPDATE.md +++ b/UPDATE.md @@ -1,6 +1,12 @@ yexuejc-base 更新记录 ------------------ +#### version :1.4.4 +**time:2021-4-24 00:41:31**
+**branch:** master
+**update:**
+>1. JsonUtil类修复格式化出现的时区差:现使用TimeZone.getDefault()时区 +# #### version :1.4.3 **time:2021-2-6 11:42:49**
**branch:** master
diff --git a/pom.xml b/pom.xml index 1163c67..fb9b02a 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ top.yexuejc yexuejc-base - 1.4.3 + 1.4.4 ${project.artifactId} https://github.com/yexuejc/yexuejc-base diff --git a/src/main/java/com/yexuejc/base/util/JsonUtil.java b/src/main/java/com/yexuejc/base/util/JsonUtil.java index b7a4c6d..2225370 100644 --- a/src/main/java/com/yexuejc/base/util/JsonUtil.java +++ b/src/main/java/com/yexuejc/base/util/JsonUtil.java @@ -10,6 +10,7 @@ import com.fasterxml.jackson.databind.type.MapType; import java.io.IOException; import java.io.InputStream; import java.util.Map; +import java.util.TimeZone; import java.util.logging.Logger; /** @@ -46,6 +47,8 @@ public class JsonUtil { objectMapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true); objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); objectMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false); + //设置一下时区,可以和程序同步避免时区问题 + objectMapper.setTimeZone(TimeZone.getDefault()); objectMapper.setDateFormat(DateUtil.DATE_TIME_FORMAT); } @@ -55,6 +58,15 @@ public class JsonUtil { objectMapper.setPropertyNamingStrategy(PropertyNamingStrategy.SNAKE_CASE); } + /** + * 这个设置不能改变JsonUtil自带的objectMapper设置,只能修改传入objMapper的设置 + * @param objMapper + */ + public static void initSnakeCase(ObjectMapper objMapper) { + //驼峰下划线互转 + objMapper.setPropertyNamingStrategy(PropertyNamingStrategy.SNAKE_CASE); + } + /** * 每调用一次生成一个全新的ObjectMapper供特殊场景使用,与通用ObjectMapper没有关系 * @@ -66,6 +78,15 @@ public class JsonUtil { return objectMapper; } + /** + * 返回 ObjectMapper对象,供外部设置特定参数 + * + * @return + */ + public static ObjectMapper getObjectMapper() { + return objectMapper; + } + /** * 将json转换为某个类 * @@ -106,7 +127,7 @@ public class JsonUtil { log.warning("json to Object JsonMappingException.\n" + e.getMessage()); } catch (IOException e) { log.warning("json to Object IOException.\n" + e.getMessage()); - }catch (Exception e){ + } catch (Exception e) { e.printStackTrace(); }