简体   繁体   English

java.text.ParseException:无法解析的日期“ IST 2014年9月12日23:22:46

[英]java.text.ParseException: Unparseable date “Fri Sep 12 23:22:46 IST 2014”

I am using jackson mapper to map the json request to an java object directly. 我正在使用杰克逊映射器将json请求直接映射到java对象。 To map the date i am using CustomDateSerializer and CustomDateDeSerializer in the getter and setter respectively. 为了映射日期,我分别在getter和setter中使用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);
        }
    }
}

my getter and setter in the model 我在模型中的getter和setter

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

Exception: 例外:

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

Can any one help me fixing this.. 谁能帮我解决这个问题。

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 ). 但是,看起来它所期望的格式并不相同( 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 Oracle文档中查看SimpleDateFormat

在此处输入图片说明

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);

更改日期格式可以解决问题-“ EEE MMM dd HH:mm:ss Z yyyy”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM