我想将日期从 Twitter 反序列化为ZonedDateTime. 我的程序在created_at反序列化领域失败。
我的域类
@JsonIgnoreProperties(ignoreUnknown = true)
public final class Tweet {
public final String id;
public final String idStr;
public final ZonedDateTime created_at;
public final String text;
public final long timestamp_ms;
public final User user;
@JsonCreator
public Tweet(@JsonProperty("id") String id,
@JsonProperty("id_str") String idStr,
@JsonProperty("created_at") ZonedDateTime created_at,
@JsonProperty("text") String text,
@JsonProperty("timestamp_ms") long timestamp_ms,
@JsonProperty("user") User user) {
this.id = id;
this.idStr = idStr;
this.created_at = created_at;
this.text = text;
this.timestamp_ms = timestamp_ms;
this.user = user;
}
}
我的解析方法
public class TweetTest {
@Test
public void jackson_parsesDateTime() throws IOException {
DateFormat dateFormat = new SimpleDateFormat("EEE MMM dd HH:mm:ss Z yyyy", Locale.ENGLISH);
ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(new JavaTimeModule());
mapper.setDateFormat(dateFormat);
Tweet tweet = mapper.readValue(TWEET, Tweet.class);
Assert.assertNotNull(tweet);
}
@Test
public void ztd_parse() throws ParseException {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("EEE MMM dd HH:mm:ss Z yyyy");
ZonedDateTime.parse("Mon Aug 20 13:28:07 +0000 2018", formatter);
}
}
相关分类