/Date(1296576000000+0800)/
Date now = new Date();
String nowStr = String.format("\/Date(%s+0800)\/", now.getTime());
compile group: 'com.google.code.gson', name: 'gson', version: '2.8.1' compile group: 'com.alibaba', name: 'fastjson', version: '1.2.36' compile group: 'com.fasterxml.jackson.core', name: 'jackson-databind', version: '2.9.0'
public class StringSmallUtils {
/**
* 时间类型格式转换为指定的String类型
*
* @param date
* @return
*/
protected static String DateToSpecialString(Date date) {
if (date == null)
return null;
return String.format("\/Date(%s+0800)\/", date.getTime());
}
/**
* 指定的String类型转换为时间类型格式
*
* @param str
* @return
*/
protected static Date SpecialStringToDate(String str) {
if (isEmpty(str))
return null;
if (!contains(str,"Date"))
return null;
str = str.replace("\/Date(", "").replace("+0800)\/", "").trim();
return new Date(Long.parseLong(str));
}
/**
* 判断字符串是否包含输入的字符串
*
* @param str
* @param searchStr
* @return
*/
public static boolean contains(String str, String searchStr) {
if (str == null || searchStr == null) {
return false;
}
return str.contains(searchStr);
}
/**
* 判断字符串是否为空
*
* @param str
* @return
*/
public static boolean isEmpty(String str) {
return ((str == null) || (str.trim().isEmpty()));
}
}
public class GsonCustomerDateJsonSerializer implements JsonSerializer<Date>, JsonDeserializer<Date> {
@Override
public JsonElement serialize(Date src, Type typeOfSrc, JsonSerializationContext context) {
return new JsonPrimitive(StringSmallUtils.DateToSpecialString(src));
}
@Override
public Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
return StringSmallUtils.SpecialStringToDate(json.getAsString());
}
}
public class Program {
public static void main(String[] args) throws JsonProcessingException {
Date start = new Date();
Gson gson = new GsonBuilder().registerTypeAdapter(Date.class, new GsonCustomerDateJsonSerializer()).create();
String gsonStr = gson.toJson(createUser());
Date end = new Date();
long interval = (end.getTime() - start.getTime());
System.out.println(String.format("Gson序列化之后的字符串:%s,花费时间%d毫秒", gsonStr, interval));
}
private static User createUser() {
User user = new User();
user.setName("张三");
user.setAge(21);
user.setLastlogintime(new Date());
return user;
}
}
public class FastJsonCustomerDateJsonSerializer implements ObjectSerializer, ObjectDeserializer {
@Override
public void write(JSONSerializer serializer, Object object, Object fieldName, Type fieldType, int features) throws IOException {
SerializeWriter out = serializer.getWriter();
out.write(StringSmallUtils.DateToSpecialString((Date) object));
}
@Override
public <T> T deserialze(DefaultJSONParser parser, Type type, Object fieldName) {
return (T) StringSmallUtils.SpecialStringToDate(parser.getInput());
}
@Override
public int getFastMatchToken() {
return 0;
}
}
public class Program {
public static void main(String[] args) throws JsonProcessingException {
Date start1 = new Date();
SerializeConfig mapping = new SerializeConfig();
mapping.put(Date.class, new FastJsonCustomerDateJsonSerializer());
String fastjsonStr = JSON.toJSONString(createUser(), mapping);
Date end1 = new Date();
long interval1 = (end1.getTime() - start1.getTime());
System.out.println(String.format("FastJSON序列化之后的字符串:%s,花费时间%d毫秒", fastjsonStr, interval1));
}
private static User createUser() {
User user = new User();
user.setName("张三");
user.setAge(21);
user.setLastlogintime(new Date());
return user;
}
}
public class JacksonCustomerDateJsonSerializer extends JsonSerializer<Date> {
@Override
public void serialize(Date value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
gen.writeString(StringSmallUtils.DateToSpecialString(value));
}
}
public class JacksonCustomerDateJsonDeserializer extends JsonDeserializer<Date> {
@Override
public Date deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JsonProcessingException {
return StringSmallUtils.SpecialStringToDate(p.getText());
}
}
public class Program {
public static void main(String[] args) throws JsonProcessingException {
Date start2 = new Date();
ObjectMapper mapper = new ObjectMapper();
SimpleModule module = new SimpleModule();
module.addSerializer(Date.class, new JacksonCustomerDateJsonSerializer());
module.addDeserializer(Date.class, new JacksonCustomerDateJsonDeserializer());
mapper.registerModule(module);
String jacksonStr = mapper.writeValueAsString(createUser());
Date end2 = new Date();
long interval2 = (end2.getTime() - start2.getTime());
System.out.println(String.format("Jackson序列化之后的字符串:%s,花费时间%d毫秒", jacksonStr, interval2));
}
private static User createUser() {
User user = new User();
user.setName("张三");
user.setAge(21);
user.setLastlogintime(new Date());
return user;
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有