특정 표준 시간대로 Calendar 객체를 직렬화 및 비 직렬화하는 방법을 알아 내려고하고 있습니다. Jackson이 내 시간대를 존중하고 지정된 시간대에 Calendar 인스턴스를 유지할 수 있다고 생각합니다. 직렬화를 위해 작동하지만 비 직렬화는하지 않습니다. 내가 도대체 뭘 잘못하고있는 겁니까?Jackson을 사용하여 특정 시간대에서 캘린더를받는 방법은 무엇입니까?
import com.fasterxml.jackson.databind.ObjectMapper;
import java.util.Calendar;
import java.text.SimpleDateFormat;
import java.util.TimeZone;
import java.io.ByteArrayOutputStream;
public class JacksonTimeZoneTest2 {
public Calendar time;
public static void main(String[] args) throws java.io.IOException {
JacksonTimeZoneTest2 test = new JacksonTimeZoneTest2();
test.time = new java.util.GregorianCalendar(2014, Calendar.SEPTEMBER, 1);
ObjectMapper mapper = new ObjectMapper();
SimpleDateFormat sdf = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z");
sdf.setTimeZone(TimeZone.getTimeZone("CET"));
mapper.setDateFormat(sdf);
ByteArrayOutputStream serialized = new ByteArrayOutputStream();
mapper.writeValue(serialized, test);
String json = serialized.toString();
if (json.contains("CEST")) {
System.out.println("It serialized to CET timezone (CEST) [" + serialized.toString() + "]");
}
JacksonTimeZoneTest2 test2 = mapper.readValue(serialized.toString(), JacksonTimeZoneTest2.class);
if (! "CET".equals(test2.time.getTimeZone().getID())) {
System.out.println("Problem! It deserialized to [" + test2.time.getTimeZone().getID() + "] instead of CET");
}
}
}
나는 다음과 같은 출력을 얻을 코드를 실행하면 : 나는 또한 시간대를 지정하는 JsonFormat 주석을 시도
It serialized to CET timezone (CEST) [{"time":"Mon, 01 Sep 2014 08:00:00 CEST"}]
Problem! It deserialized to [UTC] instead of CET
를, 나는 같은 결과를 얻었다. 나는 an issue against Jackson로 기록했다.
평소처럼 가장 먼저하는 것은 최신 버전의 Jackson을 사용하고 있는지 확인하는 것입니다. 현재 2.7 이상 (0.7), 그렇지 않은 경우 2.8 (0.3). – StaxMan
죄송합니다. 2.8.3으로 테스트 중이라는 것을 잊어 버렸습니다. –