Axis2 - Date Format
Asked Answered
Y

4

9

Scenario

The date format which is output as a response to the Web Service client by Axis2 is formatted as "2009-08-28+01:00". I would like to change this to show only the date without the timezone information (e.g.:"2009-08-28")

Configuration

Libraries

Axis 2 1.4.1

WSDL

<xsd:element name="StartDate" type="xsd:date" />;

Question

  • Is it possible to change the output format, which is used by Axis 2 to write date information?
  • Can you see any issues for .NET clients reagrding the conversion of this date format?

Constraints

Unfortunately it is not possible to change the "StartDate" element to a xsd:string or xsd:token


Question refinement

As I am using the xsd:date XML Data Type which is defined as

[-]CCYY-MM-DD[Z|(+|-)hh:mm]

Thus if I set

Calendar cal = Calendar.getInstance();
cal.setTimeZone(TimeZone.getTimeZone("UTC");
...

then the output looks like this

2009-01-28Z

You can replace "UTC" by "GMT" or "".

Can I get rid of the "Z"?

Yasmeen answered 28/8, 2009 at 11:24 Comment(0)
O
6

The problem is caused by using Calendar object as a source value for xsd:date field. When you get instance of Calendar it always goes with timezone (default timezone is used if not specified explicitly). To remove timezone use clear() method and restore all fields excluding timezone. Then XML mapping library (I tested with XmlBeans, but I think it's also true for other binding libraries supported by Axis) generates XML without timezone suffix.

Calendar myDate = Calendar.getInstance();   // returns GregorianCalendar
Calendar now = (Calendar)myDate.clone();    // save current timestamp
myDate.clear(); // this clears the fields, including Calendar.ZONE_OFFSET
myDate.set(     //set all fields back from the saved copy
    now.get(Calendar.YEAR),
    now.get(Calendar.MONTH),
    now.get(Calendar.DAY_OF_MONTH),
    now.get(Calendar.HOUR_OF_DAY),
    now.get(Calendar.MINUTE),
    now.get(Calendar.SECOND)
);
Obstruct answered 12/6, 2011 at 13:57 Comment(0)
M
9

I had the same problem and it is possible to remove timezone from dates!

You can use your own created ConvertUtil.

At first you must create class with your customized convert method/methods:

public class myConvertUtil extends org.apache.axis2.databinding.utils.ConverterUtil
    {
    public static String convertToString(Date value)
        {
        // return customized Date format
        }
    }

Then you must set this class as SYSTEM_PROPERTY_ADB_CONVERTERUTIL:

String convert_class = "com.firm.myConvertUtil";
System.setProperty(ConverterUtil.SYSTEM_PROPERTY_ADB_CONVERTERUTIL, convert_class);
Mag answered 30/3, 2011 at 9:11 Comment(1)
I am facing the same issue and trying to apply the above solution but not working for me. It is not calling the customized <conver_class>. Can you please tell me what could be wrong here.Gainey
O
6

The problem is caused by using Calendar object as a source value for xsd:date field. When you get instance of Calendar it always goes with timezone (default timezone is used if not specified explicitly). To remove timezone use clear() method and restore all fields excluding timezone. Then XML mapping library (I tested with XmlBeans, but I think it's also true for other binding libraries supported by Axis) generates XML without timezone suffix.

Calendar myDate = Calendar.getInstance();   // returns GregorianCalendar
Calendar now = (Calendar)myDate.clone();    // save current timestamp
myDate.clear(); // this clears the fields, including Calendar.ZONE_OFFSET
myDate.set(     //set all fields back from the saved copy
    now.get(Calendar.YEAR),
    now.get(Calendar.MONTH),
    now.get(Calendar.DAY_OF_MONTH),
    now.get(Calendar.HOUR_OF_DAY),
    now.get(Calendar.MINUTE),
    now.get(Calendar.SECOND)
);
Obstruct answered 12/6, 2011 at 13:57 Comment(0)
L
0

You can't.

Its value space is described as a combination of date and time of day in Chapter 5.4 of ISO 8601. Its lexical space is the extended format:

[-]CCYY-MM-DDThh:mm:ss[Z|(+|-)hh:mm]

The time zone may be specified as Z (UTC) or (+|-)hh:mm. Time zones that aren't specified are considered undetermined.

http://books.xmlschemata.org/relaxng/ch19-77049.html

Edit:

For reference see XML Schema Part 2: Datatypes Second Edition 3.2.7 dateTime

Leman answered 28/8, 2009 at 11:26 Comment(4)
It is in fact not dateTime what I am using, but in principle you are right (books.xmlschemata.org/relaxng/ch19-77041.html)Yasmeen
another one: the date specification says that the TimeZone is optional. <quote> Time zones that aren't specified are considered undetermined. </quote>Yasmeen
It is possible! See my answer.Madalena
Specification allows timezone to be optional: w3.org/TR/xmlschema-2/#dateObstruct
A
0

There is a Calendar#clear method that will accomplish what you need. To get rid of the timezone offset simply do the following:

cal.clear(Calendar.ZONE_OFFSET);

Note that a time without a timezone offset is ambiguous. It leaves the consumer of the time to guess the UTC offset.

Arruda answered 30/1, 2013 at 17:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.