package com.example.demo.utils; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.SerializationFeature; import java.text.SimpleDateFormat; public class JsonUtil { public static String getJson(Object object){ return getJson(object,"yyyy-MM-dd HH:mm:ss"); } public static String getJson(Object object,String str) { ObjectMapper mapper = new ObjectMapper(); //1.让他不显示时间戳 mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS,false); //2.自定义时间格式 SimpleDateFormat sdf = new SimpleDateFormat(str); //3.让mapper指定时间日期格式为SimpleDateFormat mapper.setDateFormat(sdf); try { return mapper.writeValueAsString(object); } catch (JsonProcessingException e) { e.printStackTrace(); } return null; } }
} }
springboot自定义时间格式工具类
最新推荐文章于 2024-07-30 20:50:32 发布