Я нашел свой комментарий выше с 2012 года, и теперь я чувствую себя обязанным добавить ответ. Я делаю некоторые изменения в веб-сервисе, который, к сожалению, должен продолжать работать на Java 6. Меня попросили подавить часть часового пояса / смещения всех полей даты и времени в моих ответах XML. Я сделал это с помощью файла привязки JAXB и 3 пар методов адаптера для типов XML даты, времени и даты и времени. Обратите внимание, что мое решение использует библиотеку JodaTime.
Вот мой файл привязки JAXB 2.1:
<?xml version="1.0" encoding="UTF-8"?>
<bindings
xmlns="http://java.sun.com/xml/ns/jaxb"
version="2.1"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<globalBindings>
<javaType
name="org.joda.time.DateTime"
xmlType="xs:dateTime"
parseMethod="com.jimtough.jaxb.DataTypeCondapter.parseDateTime"
printMethod="com.jimtough.jaxb.DataTypeCondapter.printDateTime" />
<javaType
name="org.joda.time.DateTime"
xmlType="xs:date"
parseMethod="com.jimtough.jaxb.DataTypeCondapter.parseDate"
printMethod="com.jimtough.jaxb.DataTypeCondapter.printDate" />
<javaType
name="org.joda.time.LocalTime"
xmlType="xs:time"
parseMethod="com.jimtough.jaxb.DataTypeCondapter.parseTime"
printMethod="com.jimtough.jaxb.DataTypeCondapter.printTime" />
</globalBindings>
</bindings>
Вот мой Java 6-совместимый служебный класс с методами адаптера:
package com.jimtough.jaxb;
import java.util.Date;
import javax.xml.bind.DatatypeConverter;
import org.joda.time.DateTime;
import org.joda.time.LocalTime;
import org.joda.time.format.DateTimeFormatter;
import org.joda.time.format.ISODateTimeFormat;
/**
* My bizarrely named 'condapter' is a blend of the Java {@code DatatypeConverter}
* and the Apache CXF {@code DataTypeAdapter} that provides Jodatime {@code DateTime}
* support instead of {@code java.util.Date}.
*
* @author jtough
*/
public class DataTypeCondapter {
private DataTypeCondapter() {}
// Jim Tough - 2017-02-22
// JodaTime formatters claim to be threadsafe
private static final DateTimeFormatter DTF_DATE = ISODateTimeFormat.date();
private static final DateTimeFormatter DTF_DATETIME = ISODateTimeFormat.dateHourMinuteSecondMillis();
private static final DateTimeFormatter DTF_TIME = ISODateTimeFormat.hourMinuteSecondMillis();
public static DateTime parseDate(String s) {
if (s == null) {
return null;
}
Date date = DatatypeConverter.parseDate(s).getTime();
return new DateTime(date);
}
public static String printDate(DateTime dt) {
if (dt == null) {
return null;
}
return DTF_DATE.print(dt);
}
public static LocalTime parseTime(String s) {
if (s == null) {
return null;
}
Date date = DatatypeConverter.parseTime(s).getTime();
DateTime dt = new DateTime(date);
return dt.toLocalTime();
}
public static String printTime(LocalTime lt) {
if (lt == null) {
return null;
}
return DTF_TIME.print(lt);
}
public static DateTime parseDateTime(String s) {
if (s == null) {
return null;
}
Date date = DatatypeConverter.parseDateTime(s).getTime();
return new DateTime(date);
}
public static String printDateTime(DateTime dt) {
if (dt == null) {
return null;
}
return DTF_DATETIME.print(dt);
}
}