Xstream的jodatime本地日期显示

发布于 2024-12-04 22:28:36 字数 343 浏览 1 评论 0原文

我正在使用 xstrem 将 jodatime 本地日期序列化为 xml。 然而,当输出生成的 xml 时,LocalDate 的格式并不易于阅读。 见下文:

<date>
    <iLocalMillis>1316563200000</iLocalMillis>
    <iChronology class="org.joda.time.chrono.ISOChronology" reference="../../tradeDate/iChronology"/>

有什么想法可以让 xstream 以不会让我烦恼的格式显示日期吗?

I'm using xstrem to serialise a jodatime local date into xml.
However when output the generated xml the LocalDate is not in an easily readable format.
See below:

<date>
    <iLocalMillis>1316563200000</iLocalMillis>
    <iChronology class="org.joda.time.chrono.ISOChronology" reference="../../tradeDate/iChronology"/>

Any ideas how I can get xstream to display the date in a format that won't drive me up the wall?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

感情旳空白 2024-12-11 22:28:36

这是我已经成功使用的。我相信我使用了第一篇文章中提到的链接中的信息。

import java.lang.reflect.Constructor;

import org.joda.time.DateTime;

import com.thoughtworks.xstream.converters.Converter;
import com.thoughtworks.xstream.converters.MarshallingContext;
import com.thoughtworks.xstream.converters.UnmarshallingContext;
import com.thoughtworks.xstream.io.HierarchicalStreamReader;
import com.thoughtworks.xstream.io.HierarchicalStreamWriter;


public final class JodaTimeConverter implements Converter {

    @Override
    @SuppressWarnings("unchecked")
    public boolean canConvert(final Class type) {
            return (type != null) && DateTime.class.getPackage().equals(type.getPackage());
    }

    @Override
    public void marshal(final Object source, final HierarchicalStreamWriter writer,
            final MarshallingContext context) {
            writer.setValue(source.toString());
    }

    @Override
    @SuppressWarnings("unchecked")
    public Object unmarshal(final HierarchicalStreamReader reader,
            final UnmarshallingContext context) {
            try {
                    final Class requiredType = context.getRequiredType();
                    final Constructor constructor = requiredType.getConstructor(Object.class);
                    return constructor.newInstance(reader.getValue());
            } catch (final Exception e) {
                throw new RuntimeException(String.format(
                 "Exception while deserializing a Joda Time object: %s", context.getRequiredType().getSimpleName()), e);
            }
    }

}

你可以这样注册:

XStream xstream = new XStream(new StaxDriver());
xstream.registerConverter(new JodaTimeConverter());

Here's what I have used successfully. I believe I used the info at the link mentioned in the first post.

import java.lang.reflect.Constructor;

import org.joda.time.DateTime;

import com.thoughtworks.xstream.converters.Converter;
import com.thoughtworks.xstream.converters.MarshallingContext;
import com.thoughtworks.xstream.converters.UnmarshallingContext;
import com.thoughtworks.xstream.io.HierarchicalStreamReader;
import com.thoughtworks.xstream.io.HierarchicalStreamWriter;


public final class JodaTimeConverter implements Converter {

    @Override
    @SuppressWarnings("unchecked")
    public boolean canConvert(final Class type) {
            return (type != null) && DateTime.class.getPackage().equals(type.getPackage());
    }

    @Override
    public void marshal(final Object source, final HierarchicalStreamWriter writer,
            final MarshallingContext context) {
            writer.setValue(source.toString());
    }

    @Override
    @SuppressWarnings("unchecked")
    public Object unmarshal(final HierarchicalStreamReader reader,
            final UnmarshallingContext context) {
            try {
                    final Class requiredType = context.getRequiredType();
                    final Constructor constructor = requiredType.getConstructor(Object.class);
                    return constructor.newInstance(reader.getValue());
            } catch (final Exception e) {
                throw new RuntimeException(String.format(
                 "Exception while deserializing a Joda Time object: %s", context.getRequiredType().getSimpleName()), e);
            }
    }

}

You can register it like:

XStream xstream = new XStream(new StaxDriver());
xstream.registerConverter(new JodaTimeConverter());
第七度阳光i 2024-12-11 22:28:36

如果您的对象树包含与 DateTime 相同的包中的其他类,则 @Ben Carlson 的版本会出现问题。

用于将 DateTime 与 XML 相互转换的更强大版本,也不需要反射:

public static class JodaTimeConverter implements Converter
{
    @Override
    @SuppressWarnings("unchecked")
    public boolean canConvert( final Class type )
    {
        return DateTime.class.isAssignableFrom( type );
    }

    @Override
    public void marshal( Object source, HierarchicalStreamWriter writer, MarshallingContext context )
    {
        writer.setValue( source.toString() );
    }

    @Override
    @SuppressWarnings("unchecked")
    public Object unmarshal( HierarchicalStreamReader reader,
                             UnmarshallingContext context )
    {
        return new DateTime( reader.getValue() );
    }
}

向 XStream 注册转换器以使用它:

XStream xstream = new XStream();
xstream.registerConverter(new JodaTimeConverter());

The version from @Ben Carlson has an issue if your object tree contains other classes from the same package as DateTime.

A more robust version for converting DateTime to XML and back that does not require reflection as well:

public static class JodaTimeConverter implements Converter
{
    @Override
    @SuppressWarnings("unchecked")
    public boolean canConvert( final Class type )
    {
        return DateTime.class.isAssignableFrom( type );
    }

    @Override
    public void marshal( Object source, HierarchicalStreamWriter writer, MarshallingContext context )
    {
        writer.setValue( source.toString() );
    }

    @Override
    @SuppressWarnings("unchecked")
    public Object unmarshal( HierarchicalStreamReader reader,
                             UnmarshallingContext context )
    {
        return new DateTime( reader.getValue() );
    }
}

Register the converter with XStream to use it:

XStream xstream = new XStream();
xstream.registerConverter(new JodaTimeConverter());
淡墨 2024-12-11 22:28:36

我们需要将 Joda DateTime 与 XML 属性相互转换。为此,转换器需要实现 SingleValueConverter 接口。我们最终的实现:

package com.squins.xstream.joda;

import org.joda.time.DateTime;

import com.thoughtworks.xstream.converters.ConversionException;
import com.thoughtworks.xstream.converters.basic.AbstractSingleValueConverter;


public final class JodaDateTimeConverter extends AbstractSingleValueConverter
{

    @Override
    public boolean canConvert(final Class type)
    {
    return DateTime.class.equals(type);
    }

    @Override
    public Object fromString(String str)
    {
    try
    {
        return new DateTime(str);
    }
    catch (final Exception e)
    {
        throw new ConversionException("Cannot parse date " + str);
    }
    }
}

We needed a to convert a Joda DateTime to / from an XML attribute. For that, converters need to implement interface SingleValueConverter. Our final implementation:

package com.squins.xstream.joda;

import org.joda.time.DateTime;

import com.thoughtworks.xstream.converters.ConversionException;
import com.thoughtworks.xstream.converters.basic.AbstractSingleValueConverter;


public final class JodaDateTimeConverter extends AbstractSingleValueConverter
{

    @Override
    public boolean canConvert(final Class type)
    {
    return DateTime.class.equals(type);
    }

    @Override
    public Object fromString(String str)
    {
    try
    {
        return new DateTime(str);
    }
    catch (final Exception e)
    {
        throw new ConversionException("Cannot parse date " + str);
    }
    }
}
丑丑阿 2024-12-11 22:28:36

您必须为 xstream 实现(或找到)一个自定义转换器,它将以您认为合适的方式处理 JodaTime 对象。

这是此类转换器的一个小示例: http://x-stream.github.io/转换器教程.html

You have to implement (or find) a custom converter for xstream, which will handle JodaTime object in a way you find appropriate.

Here is a small example of such converter: http://x-stream.github.io/converter-tutorial.html

百思不得你姐 2024-12-11 22:28:36

我已经使用了 此处。为简单起见粘贴它:

public class JodaTimeConverter implements Converter
{
    @Override
    public boolean canConvert(Class type) {
        return type != null && DateTime.class.getPackage().equals(type.getPackage());
    }

    @Override
    public void marshal(Object source, HierarchicalStreamWriter writer, MarshallingContext context) {
        writer.setValue(source.toString());
    }

    @Override
    public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) {
        try {
            Constructor constructor = context.getRequiredType().getConstructor(Object.class);
            return constructor.newInstance(reader.getValue());
        } catch (Exception e) { // NOSONAR
            throw new SerializationException(String.format(
            "An exception occurred while deserializing a Joda Time object: %s",
            context.getRequiredType().getSimpleName()), e);
        }
    }
}

其他示例不起作用。
干杯!

I've used the one that it is here. Pasting it for simplicity:

public class JodaTimeConverter implements Converter
{
    @Override
    public boolean canConvert(Class type) {
        return type != null && DateTime.class.getPackage().equals(type.getPackage());
    }

    @Override
    public void marshal(Object source, HierarchicalStreamWriter writer, MarshallingContext context) {
        writer.setValue(source.toString());
    }

    @Override
    public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) {
        try {
            Constructor constructor = context.getRequiredType().getConstructor(Object.class);
            return constructor.newInstance(reader.getValue());
        } catch (Exception e) { // NOSONAR
            throw new SerializationException(String.format(
            "An exception occurred while deserializing a Joda Time object: %s",
            context.getRequiredType().getSimpleName()), e);
        }
    }
}

The other samples didn't work.
Cheers!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文