Java 将Joda Time`DateTime`对象转换为`org.simpleframework.xml`(“简单xml序列化”库)中的字符串
如何为Java 将Joda Time`DateTime`对象转换为`org.simpleframework.xml`(“简单xml序列化”库)中的字符串,java,xml,serialization,jodatime,simple-framework,Java,Xml,Serialization,Jodatime,Simple Framework,如何为org.simpleframework.xml库构建转换器 我正在使用来自的(org.simpleframework.xml包) 我希望将对象序列化为字符串,例如2014-07-16T00:20:36Z。在重新构造Java对象时,我希望从该字符串构造DateTime。并没有真正解释如何构建转换器 我知道这和a和a有关。在这个项目中,我发现了和的实现。但我还没有决定如何将其组合在一起。您可以在两种方法中进行选择,正如在这个类似的问题中所讨论的那样: 转换器 转化 相比之下,我不知道每种方
org.simpleframework.xml
库构建转换器
我正在使用来自的(org.simpleframework.xml
包)
我希望将对象序列化为字符串,例如2014-07-16T00:20:36Z
。在重新构造Java对象时,我希望从该字符串构造DateTime。并没有真正解释如何构建转换器
我知道这和a和a有关。在这个项目中,我发现了和的实现。但我还没有决定如何将其组合在一起。您可以在两种方法中进行选择,正如在这个类似的问题中所讨论的那样:
- 转换器
- 转化
package com.your.package.converters.simplexml;
import org.joda.time.DateTime;
import org.simpleframework.xml.transform.Transform;
import org.slf4j.LoggerFactory;
/**
*
* © 2014 Basil Bourque. This source code may be used freely forever by anyone taking full responsibility for such usage and its consequences.
*/
public class JodaTimeDateTimeTransform implements Transform<DateTime>
{
//static final org.slf4j.Logger logger = LoggerFactory.getLogger( JodaTimeDateTimeTransform.class );
@Override
public DateTime read ( String input ) throws Exception
{
DateTime dateTime = null;
try {
dateTime = new DateTime( input ); // Keeping whatever offset is included. Not forcing to UTC.
} catch ( Exception e ) {
//logger.debug( "Joda-Time DateTime Transform failed. Exception: " + e );
}
return dateTime;
}
@Override
public String write ( DateTime dateTime ) throws Exception
{
String output = dateTime.toString(); // Keeping whatever offset is included. Not forcing to UTC.
return output;
}
}
登记处
将这些付诸实施意味着使用注册表
RegistryMatcher matchers = new RegistryMatcher();
matchers.bind( org.joda.time.DateTime.class , JodaTimeDateTimeTransform.class );
// You could add other data-type handlers, such as the "YearMonth" class in Joda-Time.
//matchers.bind( org.joda.time.YearMonth.class , JodaTimeYearMonthTransform.class );
Strategy strategy = new AnnotationStrategy();
Serializer serializer = new Persister( strategy , matchers );
并继续使用理解Joda时间类型的序列化程序
RegistryMatcher matchers = new RegistryMatcher();
matchers.bind( org.joda.time.DateTime.class , JodaTimeDateTimeTransform.class );
// You could add other data-type handlers, such as the "YearMonth" class in Joda-Time.
//matchers.bind( org.joda.time.YearMonth.class , JodaTimeYearMonthTransform.class );
Strategy strategy = new AnnotationStrategy();
Serializer serializer = new Persister( strategy , matchers );