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

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

API正在返回以下格式的日期,需要将其转换回日期格式:
"lastUpdatedDate":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>();

我用于转换的代码:

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

要求以下输出:

{ 

        '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'
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();

相关问题