Search code examples
javadatetimesoapcalendarwsdl

Java Calendar without TimeZone in Apache Axis 2


I have to use some SOAP web services API that I have no control over it. I am using Apache Axis2 to generate java classes (from WSDL) and Axis2 use'java.util.Calendar' for date time data type.

The problem here is API doesn't recognize the date time with TimeZone 2016-12-31T12:00:00.000+06:30 when Axis2 parse it to String and send request. That API only knows that format 2016-12-31T12:00:00. I have tried calendar.clear(Calendar.ZONE_OFFSET); but it still render TimeZone in XML request. I tested with XMLGregorianCalendar instead of Calendar and it worked but I needed to change Calendar data type to XMLGregorianCalendar in generated classes and those changes might be gone if there would be a newer WSDL version and we generate new classes again.

Any idea? Thanks in advance.


Solution

  • I found the answer from this but not from the first answer. I solved the problem (as Michał Niklas's answer) by creating own CustomConverterUtils by extendingorg.apache.axis2.databinding.utils.ConverterUtil and remove appendTimeZone() method call in convertToString(Calendar value). I also had to change appendTime(Calendar value, StringBuffer dateString) method. My CustomConverterUtils is as follow:

    public class CustomeConverterUtils extends ConverterUtil {
    
         public static String convertToString(Calendar value) {
                    if (value.get(Calendar.ZONE_OFFSET) == -1){
                        value.setTimeZone(TimeZone.getDefault());
                    }
                    StringBuffer dateString = new StringBuffer(28);
                    appendDate(dateString, value);
                    dateString.append("T");
                    //adding hours
                    appendTime(value, dateString);
    
                    return dateString.toString();
            }
    
            public static void appendTime(Calendar value, StringBuffer dateString) {
                if (value.get(Calendar.HOUR_OF_DAY) < 10) {
                    dateString.append("0");
                }
                dateString.append(value.get(Calendar.HOUR_OF_DAY)).append(":");
                if (value.get(Calendar.MINUTE) < 10) {
                    dateString.append("0");
                }
                dateString.append(value.get(Calendar.MINUTE)).append(":");
                if (value.get(Calendar.SECOND) < 10) {
                    dateString.append("0");
                }
               dateString.append(value.get(Calendar.SECOND));
            }
    }
    

    And you also need to put those codes too. I needed to put in my Application class as I am now using Spring Boot.

    public static void main(String[] args) throws Exception {
        String convert_class = "com.ykkh.test.CustomeConverterUtils";
        System.setProperty(ConverterUtil.SYSTEM_PROPERTY_ADB_CONVERTERUTIL, convert_class);
        SpringApplication.run(Application.class, args);
        }