JAX-WS,日期时区被误解

发布于 2024-12-20 04:47:02 字数 420 浏览 5 评论 0原文

我的 Web 服务的客户端正在发送以下格式 (ISO) 的日期属性:

2011-09-12T13:46:00+01:00

对于在当地时间 (CEST) 13:46 发送的请求。我的 Web 服务(Glassfish 上的 JAX-WS)写为:

@WebMethod    
public String getSource(Long id, Date asOfDate) {
    this.log.debug(asOfDate);
    // ...
}

解释为 Mon Sep 12 14:46:00 CEST 2011 (这是上述 log 命令的输出)。为什么不解释为 Mon Sep 12 13:46:00 CEST 2011

A client of my Web service is sending a date attribute in the following format (ISO):

2011-09-12T13:46:00+01:00

for a request sent at 13:46, local time (CEST). My Web service (JAX-WS on Glassfish), written as:

@WebMethod    
public String getSource(Long id, Date asOfDate) {
    this.log.debug(asOfDate);
    // ...
}

interprets as Mon Sep 12 14:46:00 CEST 2011 (that's the output of the above log command). Why is not interpreted as Mon Sep 12 13:46:00 CEST 2011?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

原谅我要高飞 2024-12-27 04:47:02

我不知道为什么它不能正确解析时间,但我会尝试将 Date 参数更改为 Calendar 参数。

I don't know why it doesn't parse the time correctly, but I would try changing the Date parameter to Calendar parameter.

椵侞 2024-12-27 04:47:02

正如 U Mad 所说 - Java 中的日期类型没有时区。如果您要从 Web 服务的 XML 模式开始,然后使用 wsimport 生成 Java 实现,您会注意到它将使用 XMLCalendar(如果您愿意,可以通过 JAXB 自定义映射到常规日历)

As U Mad said - Date type in Java doesn't have timezone. If you were to start with XML schema for your web service and then used wsimport to generate Java implementation you would notice that it would use XMLCalendar for this (which could be custom mapped to regular Calendar via JAXB if you prefer)

若有似无的小暗淡 2024-12-27 04:47:02

这是一个例子。 JAXB XML 文件如下所示:

<xs:schema elementFormDefault="qualified" version="1.0" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:jaxb="http://java.sun.com/xml/ns/jaxb" jaxb:version="2.0" targetNamespace="calendar-schemalet">
    <xs:annotation>
        <xs:appinfo>
            <jaxb:globalBindings>
                <jaxb:javaType name="java.util.Calendar" xmlType="xs:dateTime" parseMethod="javax.xml.bind.DatatypeConverter.parseDate" printMethod="javax.xml.bind.DatatypeConverter.printDate" />
            </jaxb:globalBindings>
        </xs:appinfo>
    </xs:annotation>
</xs:schema>

use 命令
wsimport -p <生成工件的包> -b ./jaxb.xml -d <将生成上述工件的目录> -a -keep;

Here is an example. The JAXB XML file looks as follows:

<xs:schema elementFormDefault="qualified" version="1.0" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:jaxb="http://java.sun.com/xml/ns/jaxb" jaxb:version="2.0" targetNamespace="calendar-schemalet">
    <xs:annotation>
        <xs:appinfo>
            <jaxb:globalBindings>
                <jaxb:javaType name="java.util.Calendar" xmlType="xs:dateTime" parseMethod="javax.xml.bind.DatatypeConverter.parseDate" printMethod="javax.xml.bind.DatatypeConverter.printDate" />
            </jaxb:globalBindings>
        </xs:appinfo>
    </xs:annotation>
</xs:schema>

use command
wsimport -p <package for generated artifacts> -b .<path of jaxb.xml file>/jaxb.xml -d <dir where above artifacts will be generated> -a -keep <wsdl url>

绝不服输 2024-12-27 04:47:02

但这是正确的答案..?

2011-09-12T13:46:00+01:00

时间为 2011 年 9 月 12 日 12:46:00 UTC。

在 CEST(中欧夏令时间,UTC +2 小时)中询问此内容是:

2011-09-12 at 14:46:00

这是有道理的。

But that is the right answer..?

2011-09-12T13:46:00+01:00

Is 2011-09-12 at 12:46:00 UTC.

Asking for this in CEST (Central European Summer Time, UTC +2 hrs) is:

2011-09-12 at 14:46:00

Which makes sense.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文