简体   繁体   English

Axis2 - 日期格式

[英]Axis2 - Date Format

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". 由Axis2作为对Web服务客户端的响应输出的日期格式格式为“2009-08-28 + 01:00”。 I would like to change this to show only the date without the timezone information (eg:"2009-08-28") 我想将此更改为仅显示没有时区信息的日期(例如:“2009-08-28”)

Configuration 组态

Libraries 图书馆

Axis 2 1.4.1 轴2 1.4.1

WSDL 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? 是否可以更改Axis 2用于写入日期信息的输出格式?
  • Can you see any issues for .NET clients reagrding the conversion of this date format? 你能看到.NET客户重新转换这种日期格式有什么问题吗?

Constraints 约束

Unfortunately it is not possible to change the "StartDate" element to a xsd:string or xsd:token 遗憾的是,无法将“StartDate”元素更改为xsd:stringxsd:token


Question refinement 问题改进

As I am using the xsd:date XML Data Type which is defined as 因为我正在使用xsd:date XML Data Type,它被定义为

[-]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 "". 您可以将“UTC”替换为“GMT”或“”。

Can I get rid of the "Z"? 我可以摆脱“Z”吗?

I had the same problem and it is possible to remove timezone from dates! 我遇到了同样的问题,可以从日期中删除时区!

You can use your own created ConvertUtil. 您可以使用自己创建的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 : 然后,您必须将此类设置为SYSTEM_PROPERTY_ADB_CONVERTERUTIL

String convert_class = "com.firm.myConvertUtil";
System.setProperty(ConverterUtil.SYSTEM_PROPERTY_ADB_CONVERTERUTIL, convert_class);

The problem is caused by using Calendar object as a source value for xsd:date field. 使用Calendar对象作为xsd:date字段的源值会导致此问题。 When you get instance of Calendar it always goes with timezone (default timezone is used if not specified explicitly). 当您获得Calendar的实例时,它始终与时区一起使用(如果未明确指定,则使用默认时区)。 To remove timezone use clear() method and restore all fields excluding timezone. 要删除时区,请使用clear()方法并恢复除时区之外的所有字段。 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. 然后XML映射库(我使用XmlBeans测试,但我认为对于Axis支持的其他绑定库也是如此)生成没有时区后缀的XML。

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

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: 它的价值空间被描述为ISO 8601第5.4章中日期和时间的组合。它的词汇空间是扩展格式:

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

The time zone may be specified as Z (UTC) or (+|-)hh:mm. 时区可以指定为Z(UTC)或(+ | - )hh:mm。 Time zones that aren't specified are considered undetermined. 未指定的时区被视为未确定。

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

Edit: 编辑:

For reference see XML Schema Part 2: Datatypes Second Edition 3.2.7 dateTime 有关参考,请参阅XML架构第2部分:数据类型第二版3.2.7 dateTime

There is a Calendar#clear method that will accomplish what you need. 有一个Calendar#clear方法可以满足您的需求。 To get rid of the timezone offset simply do the following: 要摆脱时区偏移,只需执行以下操作:

cal.clear(Calendar.ZONE_OFFSET); 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. 它让消费者有时间猜测UTC偏移。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM