2016-12-06 114 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)