Java 重写第三方库类的XmlAdapter

Java 重写第三方库类的XmlAdapter,java,jaxb,xmladapter,Java,Jaxb,Xmladapter,我正在使用jaxbMarshaller为第三方库类生成xml。由于将日历对象转换为字符串的库XmlAdapter没有使用时区字段,所以marshaller为pojo类的每个日历字段生成错误的xml 第三方库XmlAdapter正在使用以下类进行日历到字符串的转换: public class DateConversion { public static String printDate(Calendar value) { if(value != null) {

我正在使用jaxbMarshaller为第三方库类生成xml。由于将日历对象转换为字符串的库XmlAdapter没有使用时区字段,所以marshaller为pojo类的每个日历字段生成错误的xml

第三方库XmlAdapter正在使用以下类进行日历到字符串的转换:

public class DateConversion {
    public static String printDate(Calendar value) {
        if(value != null) {
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            return format.format(value.getTime());
        }
        return null;
    }
}
public class DateConversion {
    public static String printDate(Calendar value, TimeZone timeZone) {
        if(value != null) {
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            format.setTimeZone(timeZone);
            return format.format(value.getTime());
        }
        return null;
    }
}
因此,我想覆盖XmlAdapter for Calendar字段的行为,并尝试了以下示例,但似乎不起作用:

我的自定义XmlAdapter正在使用以下类进行转换:

public class DateConversion {
    public static String printDate(Calendar value) {
        if(value != null) {
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            return format.format(value.getTime());
        }
        return null;
    }
}
public class DateConversion {
    public static String printDate(Calendar value, TimeZone timeZone) {
        if(value != null) {
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            format.setTimeZone(timeZone);
            return format.format(value.getTime());
        }
        return null;
    }
}
然后我做了如下工作:

public @Nullable
    String toPdxmlString(final @NotNull Deals input) {
        try {
            final Marshaller marshaller = jaxbContext.createMarshaller();
            final DateFormatterAdapter dateFormatterAdapter = new DateFormatterAdapter(PdxmlDateTimeUtil.FXONLINE_DEFAULT_DEAL_TIMEZONE);
            marshaller.setAdapter(dateFormatterAdapter);
            StringWriter writer = new StringWriter();
            marshaller.marshal(input, writer);
            return writer.toString();
        } catch (JAXBException exception) {
            LOGGER.error("Unable to marshall the given input Deals: {}, into String using JAXB Context: {}, ... ", input, jaxbContext, exception);
        }
        return null;
    }

有谁能帮我知道这是否可行,如果可行,我哪里出了问题?

所以我找到了解决方案。我扩展了第三方库的XmlAdapter,并在DateConversion中插入了时区字段,如:

public class DateFormatterAdapter extends Adapter2 {
    private final TimeZone timeZone;

    public DateFormatterAdapter(final TimeZone timeZone) {
        this.timeZone = timeZone;
    }

    @Override
    public Calendar unmarshal(String value) {
        return javax.xml.bind.DatatypeConverter.parseDate(value);
    }

    @Override
    public String marshal(Calendar calendar) {
        return DateConversion.printDate(calendar, timeZone);
    }
}
最后,将扩展XmlAdapter注册为:

public @Nullable
    String toPdxmlString(final @NotNull Deals input) {
        try {
            final Marshaller marshaller = jaxbContext.createMarshaller();
            final DateFormatterAdapter dateFormatterAdapter = new DateFormatterAdapter(PdxmlDateTimeUtil.FXONLINE_DEFAULT_DEAL_TIMEZONE);
            marshaller.setAdapter(Adapter2.class, dateFormatterAdapter);
            StringWriter writer = new StringWriter();
            marshaller.marshal(input, writer);
            return writer.toString();
        } catch (JAXBException exception) {
            LOGGER.error("Unable to marshall the given input Deals: {}, into String using JAXB Context: {}, ... ", input, jaxbContext, exception);
        }
        return null;
    }