# ISO8601 with milliseconds in json using Jackson

# Assignment

import com.fasterxml.jackson.databind.util.ISO8601DateFormat;

objectMapper.setDateFormat(new ISO8601DateFormat());
1
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

์ƒˆ๋กœ์šด ํด๋ž˜์Šค๋ฅผ ์ •์˜ํ•ด์„œ Object Mapper์— ์ด์šฉํ•˜๋ฉด ๋œ๋‹ค.

ObjectMapper objectMapper = new ObjectMapper();
ISO8601DateFormat dateFormat = new ISO8601WithMillisFormat();
objectMapper.setDateFormat(dateFormat);
1
2
3

์œ„ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•˜๊ณ  new Date() ๋ฅผ ํ•ด๋ณด๋ฉด ๋‹ค์Œ ์ฒ˜๋Ÿผ ๋‚˜ํƒ€๋‚œ๋‹ค. 2017-11-13T10:50:59.555Z ๐Ÿ‘