0

ジャクソンマッパーを使用して、json リクエストを Java オブジェクトに直接マップしています。日付をマップするには、それぞれゲッターとセッターで CustomDateSerializer と CustomDateDeSerializer を使用しています。

public class CustomJsonDateSerializer extends JsonSerializer<Date> {
    @Override
    public void serialize(Date date, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
        String dateString = simpleDateFormat.format(date);
        jsonGenerator.writeString(dateString);
    }
}


public class CustomJsonDateDeserializer extends JsonDeserializer<Date> {
    @Override
    public Date deserialize(JsonParser jsonparser,
                            DeserializationContext deserializationcontext) throws IOException {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
        String date = jsonparser.getText();
        try {
            return format.parse(date);
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
    }
}

モデルのゲッターとセッター

@JsonSerialize(using=CustomJsonDateSerializer.class)
    public Date getBirthDate() {
        return birthDate;
    }
@JsonDeserialize(using = CustomJsonDateDeserializer.class)
public void setBirthDate(Date birthDate) {
    this.birthDate = birthDate;
}

例外:

Could not read JSON: java.text.ParseException: Unparseable date: "Fri Sep 12 23:22:46 IST 2014" 

これを修正するのを手伝ってくれる人はいますか..

4

3 に答える 3

2

The format which you have defined is:

SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");

However it looks like the format which it is expecting is not the same(Fri Sep 12 23:22:46 IST 2014).

It should be like:

SimpleDateFormat format = new SimpleDateFormat("EEE MMM dd hh:mm:ss Z yyyy",Locale.ENGLISH);

Check the Oracle docs for SimpleDateFormat

enter image description here

于 2014-09-12T18:10:57.030 に答える
1

Change

SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");

to

SimpleDateFormat dF=new SimpleDateFormat("EEE MMM dd hh:mm:ss z yyyy",Locale.ENGLISH);
于 2014-09-12T18:10:42.813 に答える
0

日付形式を変更すると修正されました-「EEE MMM dd HH:mm:ss Z yyyy」

于 2014-09-12T18:11:18.927 に答える