2016-12-06 112 views
-2

當我運行下面的測試時,它打印出「2016-11-11」。這裏發生了什麼?我怎樣才能得到它打印我提供作爲輸入的DateTime字符串?Joda ISODateTimeFormat序列化/反序列化

import org.joda.time.DateTime; 
 
import org.junit.Test; 
 

 
public class Adapter2 
 
    extends XmlAdapter<String, DateTime> 
 
{ 
 

 

 
    public DateTime unmarshal(String value) { 
 
     return (org.joda.time.format.ISODateTimeFormat.dateTimeParser().parseDateTime(value)); 
 
    } 
 

 
    public String marshal(DateTime value) { 
 
     return (org.joda.time.format.ISODateTimeFormat.dateTime().print(value)); 
 
    } 
 
    
 
    @Test 
 
    public void test() 
 
    { 
 
     System.out.println(new Adapter3().marshal(new Adapter3().unmarshal("2016-11-12T00:00:00.000+08:00"))); 
 
    } 
 

 
}

+0

該測試使用Adapter3。您發佈了Adapter2的代碼。 –

+0

@JBNizet感謝您指出。更改爲Adaptor2也會得到相同的結果。 (我無法編輯我原來的帖子來解決這個問題) – vivid

+0

究竟是什麼問題?當我運行這個測試時,它不打印「2016-11-11」。考慮到我的時區是歐洲/巴黎,這比UTC早一個小時,因此它打印2016-11-11T17:00:00.000 + 01:00,這是預期的輸出。你真的**作爲輸出是什麼,你期望什麼,爲什麼? –

回答

1

使用DateTime.parse()而不是ISODateTimeFormat.dateTimeParser().parseDateTime()。或使用什麼DateTime.parse()用途:

ISODateTimeFormat.dateTimeParser().withOffsetParsed().parseDateTime(value)