2009-03-24T16:00:00.000+0000时间格式转换,JSON格式返回时间错误。小时消失,时间向前加了一天

这个是json返回的数据的时间:

这个是我在程序中获取的时间2009-03-24T16:00:00.000+0000时间格式转换,JSON格式返回时间错误。小时消失,时间向前加了一天_第1张图片

 

 

我当然试过了最简单的直接在属性上加注解,但是失效了(也可能是我最开始的格式没有搞清楚,后续需要验证)

后来就开始分析时间格式的问题

解决办法:改造get/set方法

首先我是用date接收的,但是发现date没有这种格式,最后是用string接收的,然后转为date

private String createtime;

public Date getCreatetime() throws ParseException {
        SimpleDateFormat  sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        Date date =  sdf.parse(createtime);
        return date;
    }

    public void setCreatetime(String createtime) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");//注意格式化的表达式
        try {
            Date time = format.parse(createtime );
            String date = time.toString();
            SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy", java.util.Locale.US);
            Date datetime = (Date) sdf.parse(date);
            /*datetime = new java.sql.Date(datetime.getTime());*/
            String formatStr2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(datetime);
            this.createtime = formatStr2;
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }

 

你可能感兴趣的:(java)