我怎么能转换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
的发生+