잭슨 Java 기반 라이브러리이며 Java 개체를 JSON으로 변환하고 JSON을 Java 개체로 변환하는 데 유용할 수 있습니다. Jackson API는 다른 API보다 빠르고 메모리 공간이 덜 필요하며 큰 개체에 적합합니다. setDateFormat()을 사용하여 날짜 형식을 지정할 수 있습니다. ObjectMapper 수업. 이 방법은 기본 DateFormat 을 구성하는 데 사용할 수 있습니다. 시간 값을 문자열로 직렬화하고 JSON 문자열에서 역직렬화할 때
구문
public ObjectMapper setDateFormat(DateFormat dateFormat)
예
import java.io.*;
import java.text.*;
import java.util.*;
import com.fasterxml.jackson.databind.*;
public class JacksonDateformatTest {
final static ObjectMapper mapper = new ObjectMapper();
public static void main(String[] args) throws Exception {
JacksonDateformatTest jacksonDateformat = new JacksonDateformatTest();
DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
mapper.setDateFormat(df);
jacksonDateformat.dateformat();
}
public void dateformat() throws Exception {
String json = "{\"birthDate\":\"1980-12-08\"}";
Reader reader = new StringReader(json);
Employee emp = mapper.readValue(reader, Employee.class);
System.out.println(emp);
}
}
// Employee class
class Employee implements Serializable {
private Date birthDate;
public Date getBirthDate() {
return birthDate;
}
public void setBirthDate(Date birthDate) {
this.birthDate = birthDate;
}
@Override
public String toString() {
return "Employee [birthDate=" + birthDate + "]";
}
} 출력
Employee [birthDate=Mon Dec 08 00:00:00 IST 1980]