How do I represent dates without the timezone using Apache CXF?

后端 未结 4 2131
暗喜
暗喜 2021-02-04 05:52

I have a WSDL that specifies an element\'s type to be xs:date.

When I use Apache CXF to generate the Java classes, it renders the variable as an javax.xml.datatype.XMLGr

4条回答
  •  感动是毒
    2021-02-04 06:11

    I found my comment above from 2012 and now I feel compelled to add an answer. I'm making some changes to a web service that, unfortunately, must continue to run on Java 6. I was asked to suppress the timezone/offset portion of all date and time fields in my XML responses. I did this with a JAXB binding file and 3 pairs of adapter methods for date, time and datetime XML types. Note that my solution uses the JodaTime library.

    Here is my JAXB 2.1 binding file:

    
    
        
            
            
            
        
    
    

    Here is my Java 6 compatible utility class with the adapter methods:

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

提交回复
热议问题