日韩久久久精品,亚洲精品久久久久久久久久久,亚洲欧美一区二区三区国产精品 ,一区二区福利

使用 google gson 轉換Timestamp或Date類型為JS

系統 1994 0

gson lib 包為1.4版本.

創建類型適配類:

Timestamp類型適配類代碼 復制代碼
  1. import?java.lang.reflect.Type; ??
  2. import?java.sql.Timestamp; ??
  3. import?java.text.DateFormat; ??
  4. import?java.text.ParseException; ??
  5. import?java.text.SimpleDateFormat; ??
  6. import?java.util.Date; ??
  7. ??
  8. import?com.google.gson.JsonDeserializationContext; ??
  9. import?com.google.gson.JsonDeserializer; ??
  10. import?com.google.gson.JsonElement; ??
  11. import?com.google.gson.JsonParseException; ??
  12. import?com.google.gson.JsonPrimitive; ??
  13. import?com.google.gson.JsonSerializationContext; ??
  14. import?com.google.gson.JsonSerializer; ??
  15. ??
  16. public?class?TimestampTypeAdapter?implements?JsonSerializer<Timestamp>,?JsonDeserializer<Timestamp>{ ??
  17. ????private?final?DateFormat?format?=?new?SimpleDateFormat( "yyyy-MM-dd?HH:mm:ss" ); ??
  18. ????public?JsonElement?serialize(Timestamp?src,?Type?arg1,?JsonSerializationContext?arg2)?{ ??
  19. ????????String?dateFormatAsString?=?format.format(new?Date(src.getTime())); ??
  20. ????????return?new?JsonPrimitive(dateFormatAsString); ??
  21. ????} ??
  22. ??
  23. ????public?Timestamp?deserialize(JsonElement?json,?Type?typeOfT,?JsonDeserializationContext?context)?throws?JsonParseException?{ ??
  24. ????????if?(!(json?instanceof?JsonPrimitive))?{ ??
  25. ????????????throw?new?JsonParseException( "The?date?should?be?a?string?value" ); ??
  26. ????????} ??
  27. ??
  28. ????????try?{ ??
  29. ????????????Date?date?=?format.parse(json.getAsString()); ??
  30. ????????????return?new?Timestamp(date.getTime()); ??
  31. ????????}?catch?(ParseException?e)?{ ??
  32. ????????????throw?new?JsonParseException(e); ??
  33. ????????} ??
  34. ????} ??
  35. ??
  36. }??

??類型適配類

??? 應用類型適配器 寫道

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 類型的時間轉換第二種方式;

?

Java代碼 復制代碼
  1. Gson?gson?=? new ?GsonBuilder().setDateFormat( "yyyy-MM-dd?HH:mm:ss" ).create(); ??
  2. String?jsonString?=?gson.toJson( new ?Date(System.currentTimeMillis()),Date. class ); ??
  3. System.out.println(jsonString);??
        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"

?

?

使用 google gson 轉換Timestamp或Date類型為JSON字符串.


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 高唐县| 东港市| 武冈市| 友谊县| 会宁县| 炉霍县| 广东省| 乌鲁木齐县| 嘉定区| 江都市| 囊谦县| 曲周县| 澄江县| 米易县| 秦安县| 容城县| 达拉特旗| 济阳县| 滨海县| 烟台市| 奉新县| 马尔康县| 广灵县| 兴仁县| 东光县| 锡林浩特市| 天台县| 六枝特区| 永春县| 石嘴山市| 鞍山市| 万年县| 湘潭县| 错那县| 巧家县| 隆德县| 龙山县| 汉阴县| 嘉兴市| 阿坝| 丰原市|