Gson日期格式|将1589784547000转换为Java日期,并将其重新转换为格式化日期(2020.05.07 10:57:16'

zf2sa74q  于 2022-11-06  发布在  Java
关注(0)|答案(2)|浏览(197)

API正在返回以下格式的日期,需要将其转换回日期格式:
"lastUpdatedDate":1589784547000

  1. [
  2. {
  3. "code": "-1",
  4. "name": "No Key Customer",
  5. "parentCode": null,
  6. "oaeCode": "O10712",
  7. "oaeName": "ZCT Countries",
  8. "creationDate": 1589446815000,
  9. "lastUpdatedDate": 1589447438000,
  10. "countryCode": null,
  11. "lastModifiedDate": 1602280800000,
  12. "deletedInTerp": 0,
  13. "hlevel": "L1"
  14. },
  15. {
  16. "code": "-2",
  17. "name": "Not Mapped Key Customer",
  18. "parentCode": null,
  19. "oaeCode": "O10712",
  20. "oaeName": "ZCT Countries",
  21. "creationDate": 1589784547000,
  22. "lastUpdatedDate": 1589784547000,
  23. "countryCode": null,
  24. "lastModifiedDate": 1602280800000,
  25. "deletedInTerp": 0,
  26. "hlevel": "L1"
  27. }
  28. ]

以下是实体:

  1. Child Entity:
  2. @SerializedName(value = "CODE", alternate = "code")
  3. @Expose
  4. private String CODE;
  5. @SerializedName(value = "HLEVEL", alternate = "hlevel")
  6. @Expose
  7. private String HLEVEL;
  8. @SerializedName(value = "NAME", alternate = "name")
  9. @Expose
  10. private String NAME;
  11. @SerializedName(value = "PARENT_CODE", alternate = "parentCode")
  12. @Expose
  13. private String PARENT_CODE;
  14. @SerializedName(value = "DELETED", alternate = "deletedInTerp")
  15. @Expose
  16. private Integer DELETED;
  17. @SerializedName(value = "LAST_UPDATE_DATE", alternate = "lastUpdatedDate")
  18. @Expose
  19. private Date LAST_UPDATE_DATE;
  20. @SerializedName(value = "COUNTRY_CODE", alternate = "countryCode")
  21. @Expose
  22. private Integer COUNTRY_CODE;
  23. Parent Entity has only following
  24. @Expose
  25. List<KeyHierarchy> account = new ArrayList<KeyHierarchy>();

我用于转换的代码:

  1. public String transformToDDMRequest(String dataFromIngester) {
  2. Gson gson1 = new Gson();
  3. Type listOfMyClassObject = new TypeToken<ArrayList<KeyHierarchy>>() {}.getType();
  4. List<KeyHierarchies> ca = gson1.fromJson(dataFromIngester,listOfMyClassObject);
  5. Gson gson2 = new GsonBuilder().setDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ").create();
  6. return gson2.toJson(ca);
  7. }

要求以下输出:

  1. {
  2. 'HLEVEL':'L1',
  3. 'CODE':'C00ZCI',
  4. 'NAME':'ZCI Other Countries',
  5. 'PARENT_CODE':null,
  6. 'COUNTRY_CODE':'',
  7. 'LAST_UPDATE_DATE':'2020.05.07 10:57:16',
  8. 'DELETED': '0'
  9. },

你能详细说明如何解决这个问题的格式,因为我得到以下:

  1. Exception in thread "main" com.google.gson.JsonSyntaxException: 1589447438000
  2. at com.google.gson.internal.bind.DateTypeAdapter.deserializeToDate(DateTypeAdapter.java:87)
  3. at com.google.gson.internal.bind.DateTypeAdapter.read(DateTypeAdapter.java:75)
  4. at com.google.gson.internal.bind.DateTypeAdapter.read(DateTypeAdapter.java:46)
  5. at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.read(ReflectiveTypeAdapterFactory.java:131)
  6. at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:222)
  7. at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.read(TypeAdapterRuntimeTypeWrapper.java:41)
  8. at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:82)
  9. at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:61)
  10. at com.google.gson.Gson.fromJson(Gson.java:927)
  11. at com.google.gson.Gson.fromJson(Gson.java:892)
  12. at com.google.gson.Gson.fromJson(Gson.java:841)
  13. at com.evry.integrator.tedbroker.utils.RestTest.transformToCRMRequest(RestTest.java:70)
  14. at com.evry.integrator.tedbroker.utils.RestTest.main(RestTest.java:82)
  15. Caused by: java.text.ParseException: Failed to parse date ["1589447438000"]: Invalid time zone indicator '3'
htrmnn0y

htrmnn0y1#

  1. Unix time是时间点,它不包含时区,但您使用的日期格式包含时区。
    1.不要使用java.util.Date。请检查this以了解原因。
epfja78i

epfja78i2#

我不是很确定,但值得一试
不要使用Gson gson1 = new Gson();,请尝试使用Gson gson1 = new GsonBuilder().setDateFormat("dd-MM-yyyy").create();

相关问题