2

我正在尝试使用将 OffsetDateTime 对象转换为 JSON 格式ObjectMapper。不使用toString()of OffsetDateTime,而是将其ObjectMapper序列化为对象。

换句话说:

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.time.OffsetDateTime;


public class JavaTimerExampleTask{

    public static void main(String[] args) {

        OffsetDateTime time = OffsetDateTime.now();
        System.out.println("The format I want: " + time);

        ObjectMapper mapper = new ObjectMapper();

        String map = "";
        try {
            map = mapper.writeValueAsString(time);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        System.out.println("ObjectMapper returns:");
        System.out.println(map);
    }
}

现在,输出是:

我想要的格式:2015-12-10T15:01:39.117+02:00

ObjectMapper 返回:

    {
    "offset": {
        "totalSeconds": 7200,
        "id": "+02:00",
        "rules": {
            "fixedOffset": true,
            "transitionRules": [],
            "transitions": []
        }
    },
    "dayOfMonth": 10,
    "dayOfWeek": "THURSDAY",
    "dayOfYear": 344,
    "hour": 15,
    "minute": 1,
    "month": "DECEMBER",
    "monthValue": 12,
    "nano": 117000000,
    "second": 39,
    "year": 2015
}

我的问题是如何告诉 ObjectMapper 应用我想要的日期格式(“我想要的格式”)。谢谢!

4

0 回答 0