我怎么能转换Json的日期为Java日期 [英] How can I convert Json Date to Java Date

查看:234
本文介绍了我怎么能转换Json的日期为Java日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要的Json日期字符串转换成Java日期格式。然而,它给了错误,当谈到回归df.parse(tarih)行。

I am trying to convert Json date string to java date format. However, it gives error when it comes to "return df.parse( tarih )" line.

JSON:

{"DateFrom":"\/Date(1323087840000+0200)\/"}

Java的code:

Java code :

private Date JSONTarihConvert(String tarih) throws ParseException
{


    SimpleDateFormat df = new SimpleDateFormat( "yyyy-MM-dd'T'HH:mm:ssz" );


    if ( tarih.endsWith( "Z" ) ) {
        tarih = tarih.substring( 0, tarih.length() - 1) + "GMT-00:00";
    } else {
        int inset = 6;


        String s0 = tarih.substring( 6, tarih.length()-1 - inset );
        String s1 = tarih.substring( tarih.length()- inset,tarih.length()-2 );

        tarih = s0 + "GMT" + s1;
    }


        return df.parse( tarih );

}

当我把这种方法,tarih参数为:/日期(1323087840000 + 0200)/

When I call this method, tarih parameter is: /Date(1323087840000+0200)/

推荐答案

你有兴趣在日期对象和JSON发生,我是一个unix时间戳。结果
因此,我建议你日期(长毫秒)构造:)

As you're interested in a Date object and the JSON occurs to me to be a unix timestamp.
Therefore I'd recommend you the Date(long milliseconds) constructor :)

private Date JSONTarihConvert(String tarih) throws ParseException{
    long timestamp = getTimeStampFromTarih(tarih);
    return new Date(timestamp);
}

其中, getTimeStampFromTarih 的发生+

这篇关于我怎么能转换Json的日期为Java日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆