# ISO8601 with milliseconds in json using Jackson
# Assignment
import com.fasterxml.jackson.databind.util.ISO8601DateFormat;
objectMapper.setDateFormat(new ISO8601DateFormat());
1
2
3
2
3
ISO8601Date ํฌ๋งท์ผ๋ก ํ์ ๋ milliseconds๊ฐ ๋ฌด์๋๋ค.
# Solution
public class ISO8601WithMillisFormat extends ISO8601DateFormat {
@Override
public StringBuffer format(Date date, StringBuffer toAppendTo, FieldPosition fieldPosition) {
String value = ISO8601Utils.format(date, true); // "true" to include milliseconds
toAppendTo.append(value);
return toAppendTo;
}
}
1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
์๋ก์ด ํด๋์ค๋ฅผ ์ ์ํด์ Object Mapper์ ์ด์ฉํ๋ฉด ๋๋ค.
ObjectMapper objectMapper = new ObjectMapper();
ISO8601DateFormat dateFormat = new ISO8601WithMillisFormat();
objectMapper.setDateFormat(dateFormat);
1
2
3
2
3
์ ์ฝ๋๋ฅผ ์์ฑํ๊ณ new Date()
๋ฅผ ํด๋ณด๋ฉด ๋ค์ ์ฒ๋ผ ๋ํ๋๋ค. 2017-11-13T10:50:59.555Z
๐