• [转]使用 google gson 转换Timestamp或Date类型为JSON字符串.


    创建类型适配类:

    import java.lang.reflect.Type;  
    import java.sql.Timestamp;  
    import java.text.DateFormat;  
    import java.text.ParseException;  
    import java.text.SimpleDateFormat;  
    import java.util.Date;  
      
    import com.google.gson.JsonDeserializationContext;  
    import com.google.gson.JsonDeserializer;  
    import com.google.gson.JsonElement;  
    import com.google.gson.JsonParseException;  
    import com.google.gson.JsonPrimitive;  
    import com.google.gson.JsonSerializationContext;  
    import com.google.gson.JsonSerializer;  
      
    public class TimestampTypeAdapter implements JsonSerializer<Timestamp>, JsonDeserializer<Timestamp>{  
        private final DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
        public JsonElement serialize(Timestamp src, Type arg1, JsonSerializationContext arg2) {  
            String dateFormatAsString = format.format(new Date(src.getTime()));  
            return new JsonPrimitive(dateFormatAsString);  
        }  
      
        public Timestamp deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {  
            if (!(json instanceof JsonPrimitive)) {  
                throw new JsonParseException("The date should be a string value");  
            }  
      
            try {  
                Date date = format.parse(json.getAsString());  
                return new Timestamp(date.getTime());  
            } catch (ParseException e) {  
                throw new JsonParseException(e);  
            }  
        }  
      
    }  
    

     

      类型适配类

       应用类型适配器 写道

    Gson gson = new GsonBuilder().registerTypeAdapter(Timestamp.class,new TimestampTypeAdapter()).setDateFormat("yyyy-MM-dd HH:mm:ss").create();
    String jsonString = gson.toJson(resourceInfo,ResourceGeoInfo.class);
    

       输出结果

    {"positionTime":"2010-01-07 10:57:27"}
    

    Date 类型的时间转换第二种方式;  

    Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create();  
    String jsonString = gson.toJson(new Date(System.currentTimeMillis()),Date.class);  
    System.out.println(jsonString);  
    

     输出结果: 

    "2010-01-07 12:24:34"
    

      

     

  • 相关阅读:
    [javaSE] 看博客学习多线程的创建方式和优劣比较和PHP多线程
    [javaSE] 看知乎学习反射
    [javaSE] 看知乎学习工厂模式
    [android] 新闻客户端主界面部分
    [PHP] 看博客学习观察者模式
    [javascript] 看知乎学习js事件触发过程
    [javascript] 看知乎学习js闭包
    [PHP] 看博客学习插入排序
    [android] ndk环境的搭建
    [android] 看博客学习hashCode()和equals()
  • 原文地址:https://www.cnblogs.com/goyier/p/4675357.html
Copyright © 2020-2023  润新知