java Axis2 - 日期格式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1346415/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Axis2 - Date Format
提问by Dynamicbyte
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")
Axis2 作为对 Web Service 客户端的响应输出的日期格式的格式为“2009-08-28+01:00”。我想将其更改为仅显示没有时区信息的日期(例如:“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?
- Can you see any issues for .NET clients reagrding the conversion of this date format?
- 是否可以更改轴 2 用于写入日期信息的输出格式?
- 你能看到 .NET 客户端重新调整这种日期格式的转换有什么问题吗?
Constraints
约束
Unfortunately it is not possible to change the "StartDate" element to a xsd:stringor xsd:token
不幸的是,无法将“StartDate”元素更改为xsd:string或xsd:token
Question refinement
问题细化
As I am using the xsd:dateXML Data Type which is defined as
因为我正在使用xsd:date定义为的XML 数据类型
[-]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”吗?
采纳答案by Artem Zankovich
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 对象作为 xsd:date 字段的源值引起的。当您获得 Calendar 的实例时,它总是与时区一起使用(如果未明确指定,则使用默认时区)。要删除时区,请使用 clear() 方法并恢复除时区之外的所有字段。然后 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)
);
回答by Micha? Niklas
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);
回答by No Spam
There is a Calendar#clear method that will accomplish what you need. To get rid of the timezone offset simply do the following:
有一个 Calendar#clear 方法可以完成您的需求。要摆脱时区偏移,只需执行以下操作:
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 偏移量。
回答by No Spam
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.
它的值空间在 ISO 8601 的第 5.4 章中被描述为日期和时间的组合。它的词法空间是扩展格式:
[-]CCYY-MM-DDThh:mm:ss[Z|(+|-)hh:mm]
时区可以指定为 Z (UTC) 或 (+|-)hh:mm。未指定的时区被视为未确定。
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

