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

Gson日期格式化|将1589784547000转换为Java日期并重新转换为格式化日期(2020.05.07 10:57:16';,java,json,gson,date-formatting,Java,Json,Gson,Date Formatting,API正在以以下格式返回日期,需要转换回日期格式: “LastUpdateDate”:1589784547000 [ { "code": "-1", "name": "No Key Customer", "parentCode": null, "oaeCode": "O10712", "oaeName&q

API正在以以下格式返回日期,需要转换回日期格式:

“LastUpdateDate”:1589784547000

[
  {
    "code": "-1",
    "name": "No Key Customer",
    "parentCode": null,
    "oaeCode": "O10712",
    "oaeName": "ZCT Countries",
    "creationDate": 1589446815000,
    "lastUpdatedDate": 1589447438000,
    "countryCode": null,
    "lastModifiedDate": 1602280800000,
    "deletedInTerp": 0,
    "hlevel": "L1"
  },
  {
    "code": "-2",
    "name": "Not Mapped Key Customer",
    "parentCode": null,
    "oaeCode": "O10712",
    "oaeName": "ZCT Countries",
    "creationDate": 1589784547000,
    "lastUpdatedDate": 1589784547000,
    "countryCode": null,
    "lastModifiedDate": 1602280800000,
    "deletedInTerp": 0,
    "hlevel": "L1"
  }
]
以下是实体:

Child Entity:
@SerializedName(value = "CODE", alternate = "code")
@Expose
private String CODE;

@SerializedName(value = "HLEVEL", alternate = "hlevel")
@Expose
private String HLEVEL;

@SerializedName(value = "NAME", alternate = "name")
@Expose
private String NAME;

@SerializedName(value = "PARENT_CODE", alternate = "parentCode")
@Expose
private String PARENT_CODE;

@SerializedName(value = "DELETED", alternate = "deletedInTerp")
@Expose
private Integer DELETED;

@SerializedName(value = "LAST_UPDATE_DATE", alternate = "lastUpdatedDate")
@Expose
private Date LAST_UPDATE_DATE;

@SerializedName(value = "COUNTRY_CODE", alternate = "countryCode")
@Expose
private Integer COUNTRY_CODE;


Parent Entity has only following

@Expose
List<KeyHierarchy> account = new ArrayList<KeyHierarchy>();
请您详细说明如何解决此格式问题,因为我得到以下信息:

Exception in thread "main" com.google.gson.JsonSyntaxException: 1589447438000
    at com.google.gson.internal.bind.DateTypeAdapter.deserializeToDate(DateTypeAdapter.java:87)
    at com.google.gson.internal.bind.DateTypeAdapter.read(DateTypeAdapter.java:75)
    at com.google.gson.internal.bind.DateTypeAdapter.read(DateTypeAdapter.java:46)
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.read(ReflectiveTypeAdapterFactory.java:131)
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:222)
    at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.read(TypeAdapterRuntimeTypeWrapper.java:41)
    at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:82)
    at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:61)
    at com.google.gson.Gson.fromJson(Gson.java:927)
    at com.google.gson.Gson.fromJson(Gson.java:892)
    at com.google.gson.Gson.fromJson(Gson.java:841)
    at com.evry.integrator.tedbroker.utils.RestTest.transformToCRMRequest(RestTest.java:70)
    at com.evry.integrator.tedbroker.utils.RestTest.main(RestTest.java:82)
Caused by: java.text.ParseException: Failed to parse date ["1589447438000"]: Invalid time zone indicator '3'
  • 是时间点,不包含时区,但您使用的日期格式具有时区
  • 不要使用
    java.util.Date
    。检查原因

  • 您是否尝试过使用
    yyyy-MM-dd'HH:MM:ss.SSS
    { 
    
            'HLEVEL':'L1', 
    
            'CODE':'C00ZCI', 
    
            'NAME':'ZCI Other Countries', 
    
            'PARENT_CODE':null, 
    
            'COUNTRY_CODE':'', 
    
            'LAST_UPDATE_DATE':'2020.05.07 10:57:16', 
    
            'DELETED': '0' 
    
        }, 
    
    Exception in thread "main" com.google.gson.JsonSyntaxException: 1589447438000
        at com.google.gson.internal.bind.DateTypeAdapter.deserializeToDate(DateTypeAdapter.java:87)
        at com.google.gson.internal.bind.DateTypeAdapter.read(DateTypeAdapter.java:75)
        at com.google.gson.internal.bind.DateTypeAdapter.read(DateTypeAdapter.java:46)
        at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.read(ReflectiveTypeAdapterFactory.java:131)
        at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:222)
        at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.read(TypeAdapterRuntimeTypeWrapper.java:41)
        at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:82)
        at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:61)
        at com.google.gson.Gson.fromJson(Gson.java:927)
        at com.google.gson.Gson.fromJson(Gson.java:892)
        at com.google.gson.Gson.fromJson(Gson.java:841)
        at com.evry.integrator.tedbroker.utils.RestTest.transformToCRMRequest(RestTest.java:70)
        at com.evry.integrator.tedbroker.utils.RestTest.main(RestTest.java:82)
    Caused by: java.text.ParseException: Failed to parse date ["1589447438000"]: Invalid time zone indicator '3'