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有关。在这个项目中,我发现了和的实现。但我还没有决定如何将其组合在一起。

您可以在两种方法中进行选择,正如在这个类似的问题中所讨论的那样:

  • 转换器
  • 转化
相比之下,我不知道每种方法的利弊。但我知道如何实现转换方法。为此,请继续阅读

转换方法 需要三件:

  • 实现了
  • 实现了
  • 转换实现映射到其处理的数据类型的实例
  • 所有这三个都是系统的一部分

    我建议将实现放在项目中的“转换器”包中

    转换实现 您的转换实现可能如下所示

    这里的实现过于简单。它假定您希望输出为DateTime方法生成的默认ISO 8601字符串。它假设每个文本输入都与构造函数中的默认解析器兼容。要处理其他格式,请定义一组实例,依次对每个实例调用该方法,直到格式化程序成功而不引发错误。另一个需要考虑的是时区;您可能希望强制时区为UTC或类似的时间

    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 );